docker_自建私有仓库

将一个 几百兆 的镜像 push 到 docker hub 不太现实
官方提供了一个docker镜像的私有仓库,可以在自己内网的另一台机子做为仓库存储镜像
(下面的 11.22.33.44 代表 docker 镜像仓库的 服务器ip)


创建镜像仓库

  1. pull 官方镜像:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    root@ubuntu:~# docker search registry # 可以搜一下,基本第一个就是了
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    registry The Docker Registry 2.0 implementation for... 1549 [OK]
    konradkleine/docker-registry-frontend Browse and modify your Docker registry in ... 152 [OK]

    root@ubuntu:~# docker pull registry # 拉下来
    ...

    root@ubuntu:~# docker images # 查看
    REPOSITORY TAG IMAGE ID CREATED SIZE
    registry latest c2a449c9f834 5 days ago 33.2MB
  2. run 这个镜像

    1
    2
    root@ubuntu:~# mkdir /opt/docker/registry 
    root@ubuntu:~# docker run -d -p 5000:5000 -v /opt/docker/registry:/var/lib/registry registry

    可以去浏览器上看下,访问 11.22.33.44:5000/v2/ ,显示这样就表示成功运行了

  3. 将本机上的镜像推到仓库中,需要注意命名规则,因为推送的 ip 为 localhost:5000,必须将待推送的镜像名为 localhost:5000/xxx:zzz

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    root@ubuntu:~# docker tag yangxuan0261/tensorflow_rc1:16.04 localhost:5000/tensorflow_rc1:16.04 # 重命名镜像
    root@ubuntu:# docker push localhost:5000/tensorflow_rc1
    The push refers to a repository [localhost:5000/tensorflow_rc1]
    ce84932fa5d8: Pushed
    47a8182444dd: Pushed

    root@ubuntu:~# ls /opt/docker/registry/ # 查看 run 时把 容器容器存储镜像的目录 挂在到了宿主的 /opt/docker/registry 目录下
    docker
    root@ubuntu:~# ls /opt/docker/registry/docker/registry/v2/repositories/ # 进一步查看仓库中的镜像
    tensorflow_rc1

    也可以在浏览器中访问 11.22.33.44:5000/v2/_catalog

  4. 从一个远端机子 pull 这个镜像下来,需要在配置 /etc/docker/daemon.json 加上 ip地址,重启 docker 服务

    1
    2
    3
    4
    5
    6
    7
    8
    wilker@ubuntu:~$ sudo vi /etc/docker/daemon.json

    {
    "registry-mirrors": ["http://aaa.m.daocloud.io"], # 这个是做镜像加速的,与本文无关
    "insecure-registries":["http://11.22.33.44:5000"] # 加上的是这个行内容
    }

    wilker@ubuntu:~$ sudo service docker restart # 重启 docker 服务
  5. 把仓库中的镜像 pull 下来

    1
    2
    3
    4
    5
    6
    7
    8
    wilker@ubuntu:~$ docker pull 11.22.33.44:5000/tensorflow_rc1:16.04 # 需要加上服务器ip和端口
    16.04: Pulling from tensorflow_rc1
    Digest: sha256:0403f2e38eb34265c395d80b660426f384a4852becd38ba9a2b23dd487693ae1
    Status: Downloaded newer image for 11.22.33.44:5000/tensorflow_rc1:16.04

    wilker@ubuntu:~$ docker images # 查看一下
    REPOSITORY TAG IMAGE ID CREATED SIZE
    11.22.33.44:5000/tensorflow_rc1 16.04 77c4075548aa 20 hours ago 1.25GB
  6. ok, done

  7. 添加基础的认证

    1. 使用 htpasswd 生成一个用户密码文件,科普一下:htpasswd是Apache附带的程序,htpasswd生成包含用户名和密码的文本文件,每行内容格式为“用户名:密码”,用于用户文件的基本身份认证。
    • 使用 容器 里的 htpasswd 程序生成用户密码,不推荐

      1
      2
      $ mkdir auth 
      $ docker run --entrypoint htpasswd registry -Bbn yangx 123456 >> auth/htpasswd # 这是使用容器里面的 htpasswd 程序生成 用户密码 到 宿主 auth/htpwasswd 文件中,会生成一个 容器实例,还需要去删掉,不建议这样做
    • 直接使用 宿主 的 htpasswd 程序生成用户密码,推荐

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      $ mkdir auth
      root@ubuntu:~# htpasswd # 不存在这个程序
      The program 'htpasswd' is currently not installed. You can install it by typing:
      apt install apache2-utils
      root@ubuntu:~# apt install apache2-utils # 安装这个Apache工具,里面包含了 htpasswd
      Reading package lists... Done
      ...
      root@ubuntu:~# htpasswd -Bbn hello world >> auth/htpasswd # 以追加方式添加
      root@ubuntu:~# vi auth/htpasswd

      # 文件里的内容
      yangx:$2y$05$laiKKd//g6.jp5Da0CzqPOjOnGH466CpjZxmULxGjyfJKgtrDkblu

      hello:$2y$05$ojHEaJj43bzfYwsaqh7mPeRrALLfQ2QhB/0PjXXtZyzy0cwU75r.W
    • username : yangx

    • password : 123456

    • auth/htpasswd 宿主机上保存用户密码的文件,vi打开来看看

      1
      2
      3
      4
      5
      6
      root@ubuntu:~# vi auth/htpasswd

      # 文件内容,不知道用啥方式加密的
      yangx:$2y$05$laiKKd//g6.jp5Da0CzqPOjOnGH466CpjZxmULxGjyfJKgtrDkblu

      hello:$2y$05$ojHEaJj43bzfYwsaqh7mPeRrALLfQ2QhB/0PjXXtZyzy0cwU75r.W
    1. run 一下把 用户密码文件所在的目录 挂在到 容器中 的配置的目录 -v /root/auth:/auth
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ docker run -d -p 5000:5000 \
    --restart=always \
    --name my_registry \
    -v /opt/docker/registry:/var/lib/registry \
    -v /root/auth:/auth \
    -e "REGISTRY_AUTH=htpasswd" \
    -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
    -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
    registry
    1. 服务器上的 push
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    root@ubuntu:~# docker push localhost:5000/docker-shadowsocks:lastet 没有登录直接 push 会报错
    The push refers to a repository [localhost:5000/docker-shadowsocks]
    c7dba563ea0e: Preparing
    28443786b82d: Preparing
    c1bd37d01c89: Preparing
    943edb549a83: Preparing
    bf6751561805: Preparing
    f934e33a54a6: Preparing
    e7ebc6e16708: Waiting
    no basic auth credentials

    root@ubuntu:~# docker login localhost:5000 # 登录
    Username: yangx
    Password:
    Login Succeeded

    root@ubuntu:~# docker push localhost:5000/docker-shadowsocks:lastet # 再 push 就没问题了
    The push refers to a repository [localhost:5000/docker-shadowsocks]
    c7dba563ea0e: Pushed
    28443786b82d: Pushed
    1. 远程机子 pull
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    wilker@ubuntu:~$ docker pull 11.22.33.44:5000/docker-shadowsocks:lastet # 没有登录直接 pull 会报错
    Pulling repository 11.22.33.44:5000/docker-shadowsocks
    Error: image docker-shadowsocks:lastet not found

    wilker@ubuntu:~$ docker login 11.22.33.44:5000 # 登录
    Username: yangx
    Password:
    Login Succeeded

    wilker@ubuntu:~$ docker pull 11.22.33.44:5000/docker-shadowsocks:lastet # 再 pull 就没问题了
    lastet: Pulling from docker-shadowsocks
    ff02118a7a7f: Downloading 3.601MB/51.36MB
    2a423f32facf: Download complete

    wilker@ubuntu:~$ docker logout 11.22.33.44:5000 # 登出
    Removing login credentials for 11.22.33.44:5000

删除仓库镜像

  • 简单粗暴版,直接在服务器上删除仓库中的镜像目录

    1
    2
    3
    4
    root@ubuntu:~# ls /opt/docker/registry/docker/registry/v2/repositories/ # 删除前有三个镜像
    docker-shadowsocks gitlab-ce tensorflow_rc1

    root@ubuntu:~# rm -fr /opt/docker/registry/docker/registry/v2/repositories/docker-shadowsocks # 删除映射到宿主中的仓库的镜像 docker-shadowsocks

    可以在浏览器上看仓库中的镜像 :http://11.22.33.44:5000/v2/_catalog

    删除完执行垃圾回收

    1
    root@ubuntu:~# docker exec my_registry bin/registry garbage-collect /etc/docker/registry/config.yml
  • 复杂版


修改配置文件

  • 容器运行是,直接进去 vi 编辑

    1
    root@ubuntu:~# docker exec -it 4a2f34997bc9 vi /etc/docker/registry/config.yml
  • run 镜像生成容器实例的时候 挂载 指定配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ docker run -d -p 5000:5000 \
    --restart=always \
    --name my_registry22 \
    -v /opt/docker/registry:/var/lib/registry \
    -v /opt/docker/config.yml:/etc/docker/registry/config.yml
    -v /root/auth:/auth \
    -e "REGISTRY_AUTH=htpasswd" \
    -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
    -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
    registry

参考资料: