设置 Docker 镜像加速

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:

中国科技大学镜像:https://docker.mirrors.ustc.edu.cn/
网易:https://hub-mirror.c.163.com/
阿里云:https://<你的ID>.mirror.aliyuncs.com
七牛云加速器:https://reg-mirror.qiniu.com

在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件):

{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn/"]}

我们配置的是中国科技大学的镜像地址

重启docker

$ systemctl restart docker

如果报如下错误:

Job for docker.service failed because the control process exited with error code. 
See "systemctl status docker.service" and "journalctl -xe" for details.

请将daemon.json改名为daemon.conf

列出镜像列表

[test@localhost ~]$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

docker images 列出的是本地的镜像列表

按名称和标签列出镜像

docker images [REPOSITORY[:TAG]]

[test@localhost ~]$ docker images nginx:latest
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    0e901e68141f   3 weeks ago   142MB

注:[REPOSITORY[:TAG]]值必须是“完全匹配”。例如, docker images ngx不能列出nginx的镜像

命令选项

名称

简写

默认

描述

–all

-a

显示所有图像(默认隐藏中间图像)

–digests

显示摘要

–filter

-f

根据提供的条件过滤输出

–format

使用 Go 模板打印漂亮的图像

–no-trunc

不要截断输出

–quiet

-q

仅显示图像 ID

查找镜像

我们可以在docker hub上查找镜像,官网地址为:https://hub.docker.com/ 也可以通过命令 docker search ,比如我们要查询nginx的镜像

[test@localhost ~]$ docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        16959     [OK]
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   169
bitnami/nginx                                     Bitnami nginx Docker Image                      131                  [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   52
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   18                   [OK]
rancher/nginx-ingress-controller                                                                  10
clearlinux/nginx                                  Nginx reverse proxy server with the benefits…   4
ibmcom/nginx-ingress-controller                   Docker Image for IBM Cloud Private-CE (Commu…   4
bitnami/nginx-ldap-auth-daemon                                                                    3
rancher/nginx-ingress-controller-defaultbackend                                                   2
bitnami/nginx-exporter                                                                            2
vmware/nginx                                                                                      2
circleci/nginx                                    This image is for internal use                  2
bitnami/nginx-intel                                                                               1
vmware/nginx-photon                                                                               1
rancher/nginx                                                                                     1
rapidfort/nginx                                   RapidFort optimized, hardened image for NGINX   1
wallarm/nginx-ingress-controller                  Kubernetes Ingress Controller with Wallarm e…   1
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   1
rancher/nginx-conf                                                                                0
ibmcom/nginx-ingress-controller-ppc64le           Docker Image for IBM Cloud Private-CE (Commu…   0
continuumio/nginx-ingress-ws                                                                      0
rancher/nginx-ingress-controller-amd64                                                            0
ibmcom/nginx-ppc64le                              Docker image for nginx-ppc64le                  0
rancher/nginx-ssl                                                                                 0
  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否 docker 官方发布
  • stars: 类似 Github 里面的 star,越大表示越受欢迎。
  • AUTOMATED: 自动构建

一般官方发布的镜像没有像xxx/nginx这样的前缀

命令选项

名称

简写

默认

描述

–filter

-f

根据提供的条件过滤输出

–format

使用 Go 模板进行漂亮的打印搜索

–limit

25

最大搜索结果数

–no-trunc

不要截断输出

下载镜像到本地

语法:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
[test@localhost ~]$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
42c077c10790: Pull complete
62c70f376f6a: Pull complete
75a963e94de0: Download complete
7b1fab684d70: Download complete
db24d06d5af4: Download complete

注意我们是下载的官方nginx镜像,所以pull命令后面是nginx,此为镜像的名称。没有加tag,那么默认使用:latest为tag

再次列出本地镜像列表,此时便有了我们刚刚现在的nginx镜像了

[test@localhost ~]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    0e901e68141f   2 weeks ago   142MB

删除镜像

通过image id删除

[test@localhost ~]$ docker rmi 0e901e68141f

id可以是短id也可以是全长度的id

通过镜像名称删除

[test@localhost ~]$ docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:2bcabc23b45489fb0885d69a06ba1d648aeda973fae7bb981bafbb884165e514
Deleted: sha256:0e901e68141fd02f237cf63eb842529f8a9500636a9419e3cf4fb986b8fe3d5d
Deleted: sha256:1e877fb1acf761377390ab38bbad050a1d5296f1b4f51878c2695d4ecdb98c62
Deleted: sha256:834e54d50f731515065370d1c15f0ed47d2f7b6a7b0452646db80f14ace9b8de
Deleted: sha256:d28ca7ee17ff94497071d5c075b4099a4f2c950a3471fc49bdf9876227970b24
Deleted: sha256:096f97ba95539883af393732efac02acdd0e2ae587a5479d97065b64b4eded8c
Deleted: sha256:de7e3b2a7430261fde88313fbf784a63c2229ce369b9116053786845c39058d5
Deleted: sha256:ad6562704f3759fb50f0d3de5f80a38f65a85e709b77fd24491253990f30b6be

通过镜像名称+tag删除

[test@localhost ~]$ docker rmi nginx:latest
[test@localhost ~]$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

命令很好记,rm是linux的删除命令,i是image的头一个字母