0%

Docker私服Sonatype Nexus的搭建与使用

为什么要建docker私服

  • 官服在国外,速度慢
  • 创建的镜像是私有的,不想公开传到外部服务器上

Sonatype Nexus介绍

Sonatype Nexus是一个仓库管理软件,支持Maven、Docker、Bower、PyPI、Yum等类型仓库。使用Maven管理依赖的Java程序员一定不陌生,搭建Maven私服基本是用这个。注意:3.x版本才支持docker。 今天我们就用Nexus作Docker的私服,管理我们自己的私有镜像。

Sonatype Nexus安装

很简单,下载,解压,我的路径是
/usr/local/nexus-3.11.0-01
创建开机启动脚本
vim /etc/systemd/system/nexus.service
内容如下:

[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/local/nexus-3.11.0-01/bin/nexus start
ExecStop=/usr/local/nexus-3.11.0-01/bin/nexus stop
Restart=on-abort

[Install]
WantedBy=multi-user.target

设置开机启动
systemctl enable nexus
启动
systemctl start nexus nexus默认端口是8081, 现在打开浏览器,访问http://ip:8081, 应该能看到如下界面
 点击右上角Sign in,账号admin 密码admin123,登录
 点击admin ,把默认的密码改掉,安装完成。

创建仓库

点击顶部配置图标,切到配置页面,左侧菜单选Repositories, 打开仓库配置,点击Create repository 创建仓库。
 我之前已经创建好,配置如下图
 注意,这里HTTP设置了一个10008端口,仅供docker使用,找个不冲突的就行。

docker配置

docker仓库默认需要https支持,但nexus没有配ssl,需要将服务器加到insecure-registries。
vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://registry.docker-cn.com"],
  "insecure-registries":["192.168.0.201:10008"]
}

我的nexus部署在192.168.0.201 后面的端口10008与创建仓库时对应。 登录仓库:
docker login 192.169.0.201:10008
按提示输入nexus的账号密码。登录一次以后,会自动保存,以后pull/push都不需要再登录。

docker 提交镜像

  1. 将正在运行的container打包成image

    如图,nginx2容器基于nginx镜像,但是改了一些配置, 现在我们将其打包成image
    docker commit -m "nginx modified" -a "Exception" cb4cbbcde646 nginx-modified -m 描述
    -a 作者
    cb4cbbcde646 container id
    nginx-modified 生成的image名
    现在再看镜像列表:
    
    发现我们刚刚commit的image已经在里面了。

  2. 给image打标签
    docker tag f3b408f36cf7 192.168.0.201:10008/nginx-modified f3b408f36cf7 image id
    192.168.0.201:10008/nginx-modified 仓库地址和保存路径

  3. 推送镜像
    docker push 192.168.0.201:10008/nginx-modified

  4. 下载镜像
    在另一台服务器上,如果要下载镜像,重复上面的docker配置步骤,然后pull
    docker pull 192.168.0.201:10008/nginx-modified