新项目构建自动化docker部署第一步
1. 将打包的docker镜像包发布到nexus私服上
需要准备
- nexus3
- docker
- maven
上述安装就不涉及了,直接进入主题
2. nexus上配置docker仓库
- hosted:本地存储,即同docker官方仓库一样提供本地私服功能
- proxy:提供代理其他仓库的类型,如docker中央仓库
-
group:组类型,实质作用是组合多个仓库为一个地址
repositories
-
指定docker仓库名称、指定一个端口8083来通过http的方式访问仓库、勾选是否支持docker API VI,然后点击create repository 按钮创建hosted仓库
hosted
- 录入proxy仓库名称,然后在 Remote storage中输入需要代理的镜像仓库地址,这里我们选用网易的镜像地址:http://hub-mirror.c.163.com,因为docker 的中国境内的仓库地址有时候https://registry.docker-cn.com 有时不稳定会出现连接超时,Docker Index 选择Use Docker Hub
proxy
-
录入group组名称,然后录入http端口号 8082,在Member repositories 中将 hosted 和proxy 从左左边移到右边,同时保证hosted在前,这样才能在拉取镜像的时候首先从本地拉取,如果拉取不到才从中央仓库(远程)拉取
WeChatdf0c7c43a7a919280065cdd90c54435d.png
由于我们使用的时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
3. 验证
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的镜像了
4. 使用maven打包docker镜像并推送到nexus3
- 配置maven插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<configuration>
<imageName>${docker.registry.url}:8083/bull3d/${project.artifactId}:${project.version}</imageName>
<dockerDirectory>${project.basedir}</dockerDirectory>
<serverId>docker-hub</serverId>
<dockerHost>${docker.registry.host}</dockerHost>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<registryUrl>${docker.registry.url}:8083</registryUrl>
<!-- <serverId>${docker.registry.url}</serverId>-->
<pushImage>true</pushImage>
</configuration>
</plugin>
- 配置settings.xml
<server>
<id>docker-hub</id>
<username>admin</username>
<password>123456</password>
</server>
在<pluginGroups>节点中添加
<pluginGroup>com.spotify</pluginGroup>
- 开启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
- 编译打包项目并推送镜像到docker及nexus
- 通过cmd 命令窗口进入到项目根目录然后执行:mvn clean compile package docker:build -DpushImage
- 登录nexus 产看hosted仓库发现镜像已被成功推送到仓库
- 使用 docker imges 产看镜像已经被成功拉取
网友评论