docker镜像管理

121 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第12天,点击查看活动详情

1.docker镜像管理

1.1.搜索镜像

语法格式

docker search 镜像名

选镜像的建议:

  1. 优先考虑官方
  2. starts数量多(使用的人越多)
[root@192.168.146.112~]# docker search tomcat
NAME                          DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   3070      [OK]
tomee                         Apache TomEE is an all-Apache Java EE certif…   91        [OK]
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   57                   [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      30
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              18                   [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                   [OK]
arm32v7/tomcat                Apache Tomcat is an open source implementati…   11
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl…   6                    [OK]

NAME:镜像名称,一般镜像名称前面没有/的表示官方
OESCRIPTION:镜像描述
STARS:点赞数量
OFFICIAL:是否是官方
AUTOMATEO:是否自动化安装

1.2.获取镜像

docker pull 镜像名(push)

docker pull 第三方镜像连接

[root@192.168.146.112~]# docker pull httpd:2.2
2.2: Pulling from library/httpd
f49cf87b52c1: Pull complete
24b1e09cbcb7: Pull complete
8a4e0d64e915: Pull complete
bcbe0eb4ca51: Pull complete
16e370c15d38: Pull complete
Digest: sha256:9784d70c8ea466fabd52b0bc8cde84980324f9612380d22fbad2151df9a430eb
Status: Downloaded newer image for httpd:2.2
docker.io/library/httpd:2.2

docker pull 镜像:版本
如果直接写镜像,那下载的镜像是当前时间最新的版本

1.3.镜像加速

1.配置加速器地址,根据自己的环境来配置
[root@192.168.146.112~]# vim /etc/docker/daemon.json
{
        "registry-mirrors": ["https://registry.docker-cn.com"]
}

2.重载服务
[root@192.168.146.112~]# systemctl restart docker

3.下载一个镜像看下是否加速
[root@192.168.146.112~]# time docker pull httpd:2.4
2.4: Pulling from library/httpd
b4d181a07f80: Already exists
4b72f5187e6e: Pull complete
12b2c44d04b2: Pull complete
35c238b46d30: Pull complete
1adcec05f52b: Pull complete
Digest: sha256:1fd07d599a519b594b756d2e4e43a72edf7e30542ce646f5eb3328cf3b12341a
Status: Downloaded newer image for httpd:2.4
docker.io/library/httpd:2.4

real    2m42.165s
user    0m0.024s
sys     0m0.105s

1.4.查看所有镜像

两种方式

docker image ls

docker images

[root@192.168.146.112~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        2.4       bd29370f84ea   6 days ago    138MB
nginx        latest    4cdc5dd7eaad   8 days ago    133MB
tomcat       latest    36ef696ea43d   12 days ago   667MB
httpd        2.2       e06c3dbbfe23   3 years ago   171MB
[root@192.168.146.112~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        2.4       bd29370f84ea   6 days ago    138MB
nginx        latest    4cdc5dd7eaad   8 days ago    133MB
tomcat       latest    36ef696ea43d   12 days ago   667MB
httpd        2.2       e06c3dbbfe23   3 years ago   171MB

REPOSITORY:镜像名称
TAG:版本
IMAGE ID:镜像id
CREATED:创建时间
SIZE:大小

1.5.备份镜像

语法格式

docker save 镜像名:版本 > xxx.tar.gz

[root@192.168.146.112~]# docker save nginx:latest > docker_nginx_latest.tar.gz

1.6.删除镜像

语法格式:

docker image rm 镜像:版本

[root@192.168.146.112~]# docker image rm nginx:latest
Error response from daemon: conflict: unable to remove repository reference "nginx:latest" (must force) - container a9e8cd73690d is using its referenced image 4cdc5dd7eaad
删除报错表示docker中的nginx开启,需要强制删除

强制删除
[root@192.168.146.112~]# docker image rm nginx:latest --force
Untagged: nginx:latest
Untagged: nginx@sha256:353c20f74d9b6aee359f30e8e4f69c3d7eaea2f610681c4a95849a2fd7c497f9

图片.png

1.7.恢复镜像

语法格式:docker load -i xxx.tar.gz

[root@192.168.146.112~]# docker load -i docker_nginx_latest.tar.gz
Loaded image: nginx:latest

[root@192.168.146.112~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        2.4       bd29370f84ea   6 days ago    138MB
nginx        latest    4cdc5dd7eaad   8 days ago    133MB
tomcat       latest    36ef696ea43d   12 days ago   667MB
httpd        2.2       e06c3dbbfe23   3 years ago   171MB

启动镜像
[root@192.168.146.112~]# docker run -d -p 81:80 nginx:latest

1.8.镜像硬连接

语法格式:docker image tag 原镜像 新镜像

[root@192.168.146.112~]# docker image tag httpd:2.2 http2.2_test
[root@192.168.146.112~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED       SIZE
httpd          2.4       bd29370f84ea   6 days ago    138MB
nginx          latest    4cdc5dd7eaad   8 days ago    133MB
tomcat         latest    36ef696ea43d   12 days ago   667MB
http2.2_test   latest    e06c3dbbfe23   3 years ago   171MB
httpd          2.2       e06c3dbbfe23   3 years ago   171MB

删除原来的新的还在
[root@192.168.146.112~]# docker image rm httpd:2.2
Untagged: httpd:2.2
Untagged: httpd@sha256:9784d70c8ea466fabd52b0bc8cde84980324f9612380d22fbad2151df9a430eb
[root@192.168.146.112~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED       SIZE
httpd          2.4       bd29370f84ea   6 days ago    138MB
nginx          latest    4cdc5dd7eaad   8 days ago    133MB
tomcat         latest    36ef696ea43d   12 days ago   667MB
http2.2_test   latest    e06c3dbbfe23   3 years ago   171MB