docker-镜像注册中心-nexus实践

docker-镜像注册中心-nexus实践

前言

  • 一般情况下私有镜像是不会发布到公网仓库的,都是自己搭建私库,而docker官方的registry没有交互界面,使用起来略感尴尬,harbor虽然功能强大,交互友好,但感觉非常庞大,部署也相对复杂,这时发现nexus3已经支持docker仓库,果断用起来
  • 使用nexus作为镜像仓库,需要先搭建nexus3仓库,存在旧的nexus的话那就只好升级到3了

nexus部署

  • sonatype/nexus3 - Docker Hub:https://hub.docker.com/r/sonatype/nexus3

  • 部署起来相对简单,如下shell脚本

    #!/bin/sh
    docker run -it -d \
    -p 8081:8081 \
    --restart always \
    --name nexus \
    -v /etc/localtime:/etc/localtime:ro \
    -v $PWD/nexus-data:/nexus-data \
    sonatype/nexus3
    
  • 另外提供docker-compose.yml

    version: '3'
    services:
       nexus3:
          container_name: nexus3
          network_mode: bridge
          restart: always
          image: sonatype/nexus3
          ports:
             - "8081:8081"
          volumes:
             - /etc/localtime:/etc/localtime:ro
             - $PWD/nexus-data:/nexus-data
    
  • 以上启动后看日志会发现权限拒绝的错误日志,解决办法就是为nexus-data目录授权,执行"chown -R 200 nexus-data"再次重新run即可

  • 启动后就可以通过 http://localhost:8081 (默认端口是8081)访问了,登录的话会看到提示密码放置在nexus-data/admin.password,打开文件复制输入即可正常登录,登录后会让你修改密码
    在这里插入图片描述

  • 登录后创建docker仓库
    在这里插入图片描述
    在这里插入图片描述

  • 记住上面的端口,比如这里是40080,后面此端口作为仓库的pull和push使用

镜像推送

  • 在镜像推送之前,请在insecure-registries中加入docker仓库的地址

     {
        "insecure-registries": ["192.168.25.215:40080" ]
        }
    
  • 保存后要重启docker

    扫描二维码关注公众号,回复: 8751116 查看本文章
    systemctl daemon-reload
    systemctl restart docker
    
  • 这里假设你已经pull了hello-world镜像(使用docker的都知道这个镜像),为hello-world打一个tag,之后就可以推送到nexus了,其中的ip地址是nexus的宿主机的地址,端口就是刚刚在nexus上建仓库配置的

    docker login -u admin -p admin123 192.168.25.215:40080
    docker tag hello-world 192.168.25.215:40080/hwj/helloworld:1
    docker push 192.168.25.215:40080/hwj/helloworld:1
    
  • 上面在登录时可能就会报以下错误

    Error response from daemon: login attempt to http://192.168.25.215:40080/v2/ failed with status: 401 Unauthorized
    
  • 咋一看,还以为是用户或密码错误,但仔细检查发现没错啊,其实是缺少了Docker Bearer Token Realm,在nexus中补上即可,如下图

在这里插入图片描述

  • 再次执行命令就会发现已经成功上传到nexus仓库了,鼓掌鼓掌
  • 另外,如果发现创建的仓库只能本账号看见即私有了,那么请参照下面配置改为公开的
  • 在这里插入图片描述在这里插入图片描述

参考

使用docker搭建nexus并配置docker私有仓库 - 简书
https://www.jianshu.com/p/77af52a75ad8

发布了126 篇原创文章 · 获赞 37 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/huweijian5/article/details/90166824