mirror of
				https://github.com/yeasy/docker_practice.git
				synced 2025-10-31 02:03:11 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			99 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| # Docker Hub
 | ||
| 
 | ||
| 目前 Docker 官方维护了一个公共仓库 [Docker Hub](https://hub.docker.com/),其中已经包括了数量超过 [2,650,000](https://hub.docker.com/search/?type=image) 的镜像。大部分需求都可以通过在 Docker Hub 中直接下载镜像来实现。
 | ||
| 
 | ||
| ## 注册
 | ||
| 
 | ||
| 你可以在 https://hub.docker.com 免费注册一个 Docker 账号。
 | ||
| 
 | ||
| ## 登录
 | ||
| 
 | ||
| 可以通过执行 `docker login` 命令交互式的输入用户名及密码来完成在命令行界面登录 Docker Hub。
 | ||
| 
 | ||
| 你可以通过 `docker logout` 退出登录。
 | ||
| 
 | ||
| ## 拉取镜像
 | ||
| 
 | ||
| 你可以通过 `docker search` 命令来查找官方仓库中的镜像,并利用 `docker pull` 命令来将它下载到本地。
 | ||
| 
 | ||
| 例如以 `centos` 为关键词进行搜索:
 | ||
| 
 | ||
| ```bash
 | ||
| $ docker search centos
 | ||
| NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
 | ||
| centos                             The official build of CentOS.                   6449      [OK]
 | ||
| ansible/centos7-ansible            Ansible on Centos7                              132                  [OK]
 | ||
| consol/centos-xfce-vnc             Centos container with "headless" VNC session…   126                  [OK]
 | ||
| jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   117                  [OK]
 | ||
| centos/systemd                     systemd enabled base container.                 96                   [OK]
 | ||
| ```
 | ||
| 
 | ||
| 可以看到返回了很多包含关键字的镜像,其中包括镜像名字、描述、收藏数(表示该镜像的受关注程度)、是否官方创建(`OFFICIAL`)、是否自动构建 (`AUTOMATED`)。
 | ||
| 
 | ||
| 根据是否是官方提供,可将镜像分为两类。
 | ||
| 
 | ||
| 一种是类似 `centos` 这样的镜像,被称为基础镜像或根镜像。这些基础镜像由 Docker 公司创建、验证、支持、提供。这样的镜像往往使用单个单词作为名字。
 | ||
| 
 | ||
| 还有一种类型,比如 `ansible/centos7-ansible` 镜像,它是由 Docker Hub 的注册用户创建并维护的,往往带有用户名称前缀。可以通过前缀 `username/` 来指定使用某个用户提供的镜像,比如 ansible 用户。
 | ||
| 
 | ||
| 另外,在查找的时候通过 `--filter=stars=N` 参数可以指定仅显示收藏数量为 `N` 以上的镜像。
 | ||
| 
 | ||
| 下载官方 `centos` 镜像到本地。
 | ||
| 
 | ||
| ```bash
 | ||
| $ docker pull centos
 | ||
| Using default tag: latest
 | ||
| latest: Pulling from library/centos
 | ||
| 7a0437f04f83: Pull complete
 | ||
| Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
 | ||
| Status: Downloaded newer image for centos:latest
 | ||
| docker.io/library/centos:latest
 | ||
| ```
 | ||
| 
 | ||
| ## 推送镜像
 | ||
| 
 | ||
| 用户也可以在登录后通过 `docker push` 命令来将自己的镜像推送到 Docker Hub。
 | ||
| 
 | ||
| 以下命令中的 `username` 请替换为你的 Docker 账号用户名。
 | ||
| 
 | ||
| ```bash
 | ||
| $ docker tag ubuntu:18.04 username/ubuntu:18.04
 | ||
| 
 | ||
| $ docker image ls
 | ||
| 
 | ||
| REPOSITORY                                               TAG                    IMAGE ID            CREATED             SIZE
 | ||
| ubuntu                                                   18.04                  275d79972a86        6 days ago          94.6MB
 | ||
| username/ubuntu                                          18.04                  275d79972a86        6 days ago          94.6MB
 | ||
| 
 | ||
| $ docker push username/ubuntu:18.04
 | ||
| 
 | ||
| $ docker search username
 | ||
| 
 | ||
| NAME                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
 | ||
| username/ubuntu
 | ||
| ```
 | ||
| 
 | ||
| ## 自动构建
 | ||
| 
 | ||
| > 2021 年 7 月 26 日之后,该项功能仅限[付费用户](https://www.docker.com/blog/changes-to-docker-hub-autobuilds/)使用。
 | ||
| 
 | ||
| 自动构建(`Automated Builds`)可以自动触发构建镜像,方便升级镜像。
 | ||
| 
 | ||
| 有时候,用户构建了镜像,安装了某个软件,当软件发布新版本则需要手动更新镜像。
 | ||
| 
 | ||
| 而自动构建允许用户通过 Docker Hub 指定跟踪一个目标网站(支持 [GitHub](https://github.com) 或 [BitBucket](https://bitbucket.org))上的项目,一旦项目发生新的提交 (`commit`)或者创建了新的标签(`tag`),Docker Hub 会自动构建镜像并推送到 Docker Hub 中。
 | ||
| 
 | ||
| 要配置自动构建,包括如下的步骤:
 | ||
| 
 | ||
| * 登录 Docker Hub;
 | ||
| 
 | ||
| * 在 Docker Hub 点击右上角头像,在账号设置(`Account Settings`)中关联(`Linked Accounts`)目标网站;
 | ||
| 
 | ||
| * 在 Docker Hub 中新建或选择已有的仓库,在 `Builds` 选项卡中选择 `Configure Automated Builds`;
 | ||
| 
 | ||
| * 选取一个目标网站中的项目(需要含 `Dockerfile`)和分支;
 | ||
| 
 | ||
| * 指定 `Dockerfile` 的位置,并保存。
 | ||
| 
 | ||
| 之后,可以在 Docker Hub 的仓库页面的 `Timeline` 选项卡中查看每次构建的状态。
 |