五、【Docker笔记】Dockers仓库

仓库是集中存放镜像的地方,仓库的概念不要与注册服务器做混淆。注册服务器是存放仓库的具体服务器,每个服务器上可能有多个仓库,一个仓库有多个镜像。

仓库又可分为共有仓库和私有仓库,最大的共有仓库即Docker官方提供的DockerHub。

一、Docker Hub

此为官方提供的和维护的最大公共仓库,地址为 https://hub.docker.com

1、登录

我们可以使用 docker login 命令来登录,该命令过程需要输入用户名、密码和邮箱来完成注册和登录,之后这些信息保存在本地的 .dockercfg 文件中。

# 0. 使用 docker login 命令登录DockerHub  $ sudo docker login  Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.  Username: cq1415583094  Password:  WARNING! Your password will be stored unencrypted in /root/.docker/config.json.  Configure a credential helper to remove this warning. See  https://docs.docker.com/engine/reference/commandline/login/#credentials-store  Login Succeeded    # 1. 查看本地配置文件    

2、基本操作

登录之后,我们即可使用 docker search 命令来查找官方仓库中的镜像文件,找到后使用 docker pull 命令将镜像下载到本地。

# 0. 从官方仓库中查找 centos 镜像  $ sudo docker search centos  NAME                   DESCRIPTION                 STARS    OFFICIAL            AUTOMATED  centos           The official build of CentOS.      5925       [OK]  ansible/centos7-ansible  Ansible on Centos7          128      					 [OK]  ...    # 1. 将官方提供的镜像下载到本地  $ sudo docker pull centos  Using default tag: latest  latest: Pulling from library/centos  8a29a15cefae: Pull complete  Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700  Status: Downloaded newer image for centos:latest  docker.io/library/centos:latest    # 2. 将刚才下载的上传到仓库  	# 2.1 将刚才的镜像tag做修改  	$ sudo docker tag centos:latest cq1415583094/centos:latest  	# 2.2 上传自己的镜像  	$ sudo docker push cq1415583094/centos:latest  	The push refers to repository [docker.io/cq1415583094/centos]  0683de282177: Mounted from library/centos  latest: digest: sha256:9e0c275e0bcb495773b10a18e499985d782810e47b4fce076422acb4bc3da3dd size: 529    # 3. 搜索自己的centos  $ sudo docker search cq1415583094/centos  

3、自动创建

当我们创建了镜像,安装了软件,当此时软件发布了新的版本,若要更新则需要我们手动更新镜像。而Docker为我们提供了自动创建的过程。

不过在使用该功能时,需要我们通过DockerHub指定跟踪一个目标网站(支持GitHub/BitBucket)上的项目,若发现项目有新的提交则自动创建。关于配置自动创建步骤如下:

  1. 创建并登录DockerHub以及目标网站。并在目标网站连接账户到DockerHub。

  2. 在DockerHub上配置一个自动创建。

  3. 选取一个目标网站上的项目和分支。当然该项目需要含Dockerfile。

  4. 指定Dockerfile的位置,并提交创建。

二、DaoCloud

Docker官方仓库有时访问比较慢,此时可以使用国内提供的仓库,目前国内的仓库有DaoCloud,地址为 https://hub.daocloud.io/ ,在仓库中可以搜索到centos的各个版本。

# 从DaoCloud仓库拉取centos7镜像  $ sudo docker pull daocloud.io/library/centos:7  ab5ef0e58194: Pull complete  Digest: sha256:285bc3161133ec01d8ca8680cd746eecbfdbc1faa6313bd863151c4b26d7e5a5  Status: Downloaded newer image for daocloud.io/library/centos:7  daocloud.io/library/centos:7    # 查看镜像  $ sudo docker images  REPOSITORY                   TAG   IMAGE ID            CREATED             SIZE  daocloud.io/library/centos   7     5e35e350aded        4 months ago        203MB  

三、创建和使用私有仓库

公司中一般都有自己的一个私有仓库,对于私有仓库中的镜像也只是公司内部使用。那么我们就来创建一个私有仓库。

1、使用registry镜像创建私有仓库

# 0. 使用 registry 镜像来搭建私有仓库, 并将容器中的 5000端口对应系统的 5000端口  $ sudo docker run -d -p 5000:5000 registry  

这样我们的私有仓库即搭建完成,对应的地址为 192.168.0.128:5000

2、管理私有仓库镜像

2.1、上传镜像到私有仓库

# 0. 查看本地cnetos镜像  $ sudo docker images  REPOSITORY        TAG       IMAGE ID            CREATED             SIZE  centos             7       5e35e350aded        4 months ago         203MB    # 1. 修改tag为私有仓库的tag  $ sudo docker tag centos:7 192.168.0.128:5000/centos:7  $ sudo docker images  REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE  test/ubuntu                  v1.0                0f5b15a68f76        14 hours ago        64.2MB  test                         latest              e5bc93985af0        2 days ago          64.2MB  user/test                    latest              e5bc93985af0        2 days ago          64.2MB  ubuntu                       latest              4e5021d210f6        2 weeks ago         64.2MB  registry                     latest              708bc6af7e5e        2 months ago        25.7MB  cq1415583094/centos          latest              470671670cac        2 months ago        237MB  centos                       latest              470671670cac        2 months ago        237MB  daocloud.io/library/centos   7                   5e35e350aded        4 months ago        203MB  centos                       7                   5e35e350aded        4 months ago        203MB  [root@localhost opt]# sudo docker tag centos:7 192.168.0.128:5000/centos:7  [root@localhost opt]# docker images  REPOSITORY                   TAG         IMAGE ID            CREATED             SIZE  192.168.0.128:5000/centos    7         5e35e350aded        4 months ago         203MB    # 2. 使用 docker push 命令上传  $ sudo docker push 192.168.0.128:5000/centos    # 说明:有时在push中会报https错 这因为是docker私有仓库服务器,默认是基于https传输的,所以我们需要在客户端做相关设置,不使用https传输。  $ vim /etc/docker/daemon.json  {          "registry-mirrors": ["https://hub.docker.com"],          "insecure-registries":["192.168.0.128:5000"]  }  $ sudo systemctl daemon-reload  $ sudo systemctl restart docker