docker下安装nexus3,并通过maven打包springboot项目为docker镜像上传到nexus3

3 篇文章 0 订阅
1 篇文章 0 订阅

一、安装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]

  1. 参数说明
    1. 8082端口用于host镜像仓库的服务端口
    2. 8083端口用于group镜像仓库的服务端口
    3. 8081端口是nexus3的服务端口
    4. -v /home/nexus/nexus-data:/nexus-data 为将容器内nexus-data 数据文件夹挂载到宿主机/home/nexus/nexus-data目录下
    5. --restart=always 为不管退出的状态码是什么,在重启docker时,都将重启容器

三、访问nexus3并创建docker私有仓库

  1. 通过浏览器访问nexus3:http://ip:80801
  2. 登录nexus3
  3. 第一次登录时根据引导获取初始密码及修改登录密码
  4. 创建repository
    1. 点击设置界面,选择Repositories
    2. 选择仓库类型(仓库类型分为三种,分别是:hosted、proxy、group)
      1. 类型说明:
        1. hosted:本地存储,即同docker官方仓库一样提供本地私服功能
        2. proxy:提供代理其他仓库的类型,如docker中央仓库
        3. group:组类型,实质作用是组合多个仓库为一个地址
    3. 创建hosted仓库指定docker仓库名称、指定一个端口8082来通过http的方式访问仓库、勾选是否支持docker API VI,然后点击create repository 按钮创建hosted仓库
    4. 创建proxy仓库录入proxy仓库名称,然后在 Remote storage中输入需要代理的镜像仓库地址,这里我们选用网易的镜像地址:http://hub-mirror.c.163.com,因为docker 的中国境内的仓库地址有时候https://registry.docker-cn.com 有时不稳定会出现连接超时,Docker Index 选择Use Docker Hub
    5. 创建group仓库

录入group组名称,然后录入http端口号 8083,在Member repositories 中将 hosted 和proxy 从左左边移到右边,同时保证hosted在前,这样才能在拉取镜像的时候首先从本地拉取,如果拉取不到才从中央仓库(远程)拉取

  • 设Realms 

将 Docker Bearer Toker Realm从左边窗口移动到右边窗口,然后保存

四、docker服务的设定

          由于我们使用的时http而不是https 故需要在启动参数文件中设置

  1. 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"]
  2. 重启docker
    1. systemctl daemon-reload
    2. systemctl restart docker
  3. 登录验证
    1. docker login ip:8082
    2. docker login ip:8083
    3. 在登录需要输入登录用户名及密码,即为你的nexus3的登录用户名及密码
  4. 验证proxy
    1. docker pull ip:8083/redis
    2. 此docker私服仓库中时没有redis的镜像的,故nexus3会从中央仓库中去拉取镜像,拉取成功之后,查看nexus3的proxy仓库发现已经存在了redis镜像
  5. 验证hosted
    1. tag镜像:docker tag nginx:latest ip:8082/nginx:latest
    2. push 镜像:docker push ip:8082/nginx:latest
    3. 此时查看hosted仓库发现已经存nginx的镜像了

五、使用maven打包springboot项目为docker镜像并推送到nexus3

  1. 配置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>

     

  2. 配置settings.xml

    <servers>
    	<server>
          <id>nexus-docker-registry</id>
          <!--nexus3的登录用户名-->
          <username>admin</username>
          <!--nexus3的登录密码-->
          <password>xxxxx</password>
        </server>
      </servers>

     

  3. 开启docker远程调用
    1. 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
    2. 重启docker
      systemctl daemon-reload
      systemclt restart docker
  4. 编译打包项目并推送镜像到docker及nexus
    1. 通过cmd 命令窗口进入到项目根目录然后执行:mvn clean compile package docker:build -DpushImage
  5. 登录nexus 产看hosted仓库发现镜像已被成功推送到仓库
  6. 使用 docker imges 产看镜像已经被成功拉取

END!

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值