访问 Docker 仓库

    xiaoxiao2022-07-13  166

    仓库(Repository)是集中存放镜像的地方。

    一个容易混淆的概念是注册服务器(Registry)。实际上注册服务器是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。从这方面来说,仓库可以被认为是一个具体的项目或目录。例如对于仓库地址 dl.dockerpool.com/ubuntu 来说,dl.dockerpool.com 是注册服务器地址,ubuntu 是仓库名。

    Docker Hub

    目前 Docker 官方维护了一个公共仓库Docker Hub,其中已经包括了数量超过 15,000 的镜像。大部分需求都可以通过在 Docker Hub 中直接下载镜像来实现。

    注册:

    可以在 https://cloud.docker.com 免费注册一个 Docker 账号。

    登录

    可以通过执行 docker login 命令交互式的输入用户名及密码来完成在命令行界面登录 Docker Hub,可以通过 docker logout 退出登录。

    拉取镜像

    可以通过 docker search 命令来查找官方仓库中的镜像,并利用 docker pull 命令来将它下载到本地。

    例如以 centos 为关键词进行搜索:

    $ docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 465 [OK] tianon/centos CentOS 5 and 6, created using rinse instea... 28 blalor/centos Bare-bones base CentOS 6.5 image 6 [OK] saltstack/centos-6-minimal 6 [OK] tutum/centos-6.4 DEPRECATED. Use tutum/centos:6.4 instead. ... 5 [OK]

    下载官方 centos 镜像到本地。

    $ docker pull centos Pulling repository centos 0b443ba03958: Download complete 539c0211cd76: Download complete 511136ea3c5a: Download complete 7064731afe90: Download complete

    推送镜像

    用户也可以在登录后通过 docker push 命令来将自己的镜像推送到 Docker Hub。 以下命令中的 username 请替换为你的 Docker 账号用户名。

    $ docker tag ubuntu:17.10 username/ubuntu:17.10 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 17.10 275d79972a86 6 days ago 94.6MB username/ubuntu 17.10 275d79972a86 6 days ago 94.6MB $ docker push username/ubuntu:17.10 $ docker search username NAME DESCRIPTION STARS OFFICIAL AUTOMATED username/ubuntu

    自动创建

    自动创建(Automated Builds)功能对于需要经常升级镜像内程序来说,十分方便。 要配置自动创建,包括如下的步骤:

    创建并登录 Docker Hub,以及目标网站;在目标网站中连接帐户到 Docker Hub;在 Docker Hub 中 配置一个自动创建;选取一个目标网站中的项目(需要含 Dockerfile)和分支;指定 Dockerfile 的位置,并提交创建。

    之后,可以在 Docker Hub 的 自动创建页面 中跟踪每次创建的状态。

    Docker 私有仓库

    有时候使用 Docker Hub 这样的公共仓库可能不方便,用户可以创建一个本地仓库供私人使用。 docker-registry是官方提供的工具,可以用于构建私有的镜像仓库。本文内容基于 docker-registry v2.x版本。

    安装运行 docker-registry

    容器运行

    可以通过获取官方 registry 镜像来运行

    $ docker run -d -p 5000:5000 --restart=always --name registry registry

    这将使用官方的 registry 镜像来启动私有仓库。默认情况下,仓库会被创建在容器的 /var/lib/registry 目录下。你可以通过 -v参数来将镜像文件存放在本地的指定路径。例如下面的例子将上传的镜像放到本地的 /opt/data/registry 目录。

    $ docker run -d \ -p 5000:5000 \ -v /opt/data/registry:/var/lib/registry \ registry

    在私有仓库上传、搜索、下载镜像

    创建好私有仓库之后,就可以使用 docker tag 来标记一个镜像,然后推送它到仓库。例如私有仓库地址为 127.0.0.1:5000。 先在本机查看已有的镜像。

    $ docker image ls REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB

    使用 docker tag 将 ubuntu:latest 这个镜像标记为127.0.0.1:5000/ubuntu:latest。 格式为 docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]。

    $ docker tag ubuntu:latest 127.0.0.1:5000/ubuntu:latest $ docker image ls REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB 127.0.0.1:5000/ubuntu:latest latest ba5877dc9bec 6 weeks ago 192.7 MB

    使用 docker push 上传标记的镜像。

    $ docker push 127.0.0.1:5000/ubuntu:latest The push refers to repository [127.0.0.1:5000/ubuntu] 373a30c24545: Pushed a9148f5200b0: Pushed cdd3de0940ab: Pushed fc56279bbb33: Pushed b38367233d37: Pushed 2aebd096e0e2: Pushed latest: digest: sha256:fe4277621f10b5026266932ddf760f5a756d2facd505a94d2da12f4f52f71f5a size: 1568

    用 curl 查看仓库中的镜像。

    $ curl 127.0.0.1:5000/v2/_catalog {"repositories":["ubuntu"]}

    这里可以看到{"repositories":["ubuntu"]},表明镜像已经被成功上传了。

    先删除已有镜像,再尝试从私有仓库中下载这个镜像。

    $ docker image rm 127.0.0.1:5000/ubuntu:latest $ docker pull 127.0.0.1:5000/ubuntu:latest Pulling repository 127.0.0.1:5000/ubuntu:latest ba5877dc9bec: Download complete 511136ea3c5a: Download complete 9bad880da3d2: Download complete 25f11f5fb0cb: Download complete ebc34468f71d: Download complete 2318d26665ef: Download complete $ docker image ls REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 127.0.0.1:5000/ubuntu:latest latest ba5877dc9bec 6 weeks ago 192.7 MB ```
    最新回复(0)