一、安装nexus3
docker pull sonatype/nexus3
二、使用nexus3镜像启动一个容器
docker run -d -p 8081:8081 -p 8082:8082 -p 8083:8083 --name nexus3 -v /home/nexus/nexus-data:/nexus-data --restart=always sonatype/nexus3
**修改/home/nexus/nexus-data 目录权限:chmod 777 /home/nexus/nexus-data 否则启动容器时会报错**
查看容器启动日志:docker container logs [容器id]
- 参数说明
- 8082端口用于host镜像仓库的服务端口
- 8083端口用于group镜像仓库的服务端口
- 8081端口是nexus3的服务端口
- -v /home/nexus/nexus-data:/nexus-data 为将容器内nexus-data 数据文件夹挂载到宿主机/home/nexus/nexus-data目录下
- --restart=always 为不管退出的状态码是什么,在重启docker时,都将重启容器
三、访问nexus3并创建docker私有仓库
- 通过浏览器访问nexus3:http://ip:80801
- 登录nexus3
- 第一次登录时根据引导获取初始密码及修改登录密码
- 创建repository
- 点击设置界面,选择Repositories
- 选择仓库类型(仓库类型分为三种,分别是:hosted、proxy、group)
- 类型说明:
- hosted:本地存储,即同docker官方仓库一样提供本地私服功能
- proxy:提供代理其他仓库的类型,如docker中央仓库
- group:组类型,实质作用是组合多个仓库为一个地址
- 类型说明:
- 创建hosted仓库
指定docker仓库名称、指定一个端口8082来通过http的方式访问仓库、勾选是否支持docker API VI,然后点击create repository 按钮创建hosted仓库
- 创建proxy仓库
录入proxy仓库名称,然后在 Remote storage中输入需要代理的镜像仓库地址,这里我们选用网易的镜像地址:http://hub-mirror.c.163.com,因为docker 的中国境内的仓库地址有时候https://registry.docker-cn.com 有时不稳定会出现连接超时,Docker Index 选择Use Docker Hub
- 创建group仓库
- 点击设置界面,选择Repositories
录入group组名称,然后录入http端口号 8083,在Member repositories 中将 hosted 和proxy 从左左边移到右边,同时保证hosted在前,这样才能在拉取镜像的时候首先从本地拉取,如果拉取不到才从中央仓库(远程)拉取
- 设Realms
将 Docker Bearer Toker Realm从左边窗口移动到右边窗口,然后保存
四、docker服务的设定
由于我们使用的时http而不是https 故需要在启动参数文件中设置
- vi /etc/docker/daemon.json,将ip:8082和ip:8083 添加到 insecure-registries 参数中,由于我们的远程仓库地址为http://hub-mirror.c.163.com,不为https 故同样需要将该地址添加到insecure-registries参数中:"insecure-registries":["ip:8082","ip:8083","http://hub-mirror.c.163.com"]
- 重启docker
- systemctl daemon-reload
- systemctl restart docker
- 登录验证
- docker login ip:8082
- docker login ip:8083
- 在登录需要输入登录用户名及密码,即为你的nexus3的登录用户名及密码
- 验证proxy
- docker pull ip:8083/redis
- 此docker私服仓库中时没有redis的镜像的,故nexus3会从中央仓库中去拉取镜像,拉取成功之后,查看nexus3的proxy仓库发现已经存在了redis镜像
- 验证hosted
- tag镜像:docker tag nginx:latest ip:8082/nginx:latest
- push 镜像:docker push ip:8082/nginx:latest
- 此时查看hosted仓库发现已经存nginx的镜像了
五、使用maven打包springboot项目为docker镜像并推送到nexus3
- 配置maven插件
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.0</version> <configuration> <!--打包docker镜像的docker服务器--> <dockerHost>http://ip:2375</dockerHost> <!--镜像名称及版本[ip:port/name:tag]--> <imageName>ip:8082/ecif-demo:latest</imageName> <!--nexus3 hosted 仓库地址--> <registryUrl>ip:8082</registryUrl> <!--Dockerfile路径--> <dockerDirectory>src/main/docker</dockerDirectory> <!--是否强制覆盖已有镜像--> <forceTags>true</forceTags> <imageTags> <!--镜像tag--> <imageTag>latest</imageTag> </imageTags> <!--复制jar包到docker容器指定目录配置--> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!--在maven settings.xml中配置的server的id值--> <serverId>nexus-docker-registry</serverId> </configuration> </plugin>
-
配置settings.xml
<servers> <server> <id>nexus-docker-registry</id> <!--nexus3的登录用户名--> <username>admin</username> <!--nexus3的登录密码--> <password>xxxxx</password> </server> </servers>
- 开启docker远程调用
- vim /usr/lib/systemd/system/docker.service
在 ExecStart 追加:-H tcp://0.0.0.0:2375
如:ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock - 重启docker
systemctl daemon-reload
systemclt restart docker
- vim /usr/lib/systemd/system/docker.service
- 编译打包项目并推送镜像到docker及nexus
- 通过cmd 命令窗口进入到项目根目录然后执行:mvn clean compile package docker:build -DpushImage
- 登录nexus 产看hosted仓库发现镜像已被成功推送到仓库
- 使用 docker imges 产看镜像已经被成功拉取
END!