Docker镜像操作

1、拉取镜像

[root@hosts01 ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
22dc81ace0ea: Pull complete 
1a8b3c87dba3: Pull complete 
91390a1c435a: Pull complete 
07844b14977e: Pull complete 
b78396653dae: Pull complete 
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:16.04

2、运行镜像,关闭后删除容器,

[root@hosts01 ~]# docker run -it --rm ubuntu:16.04 bash
root@5c7f542c2a0f:/# 

–rm:关闭容器后自动删除容器
-i:交互操作
-t:终端
bash:执行命令

3、列出镜像【显示顶层镜像】

[root@hosts01 ~]# docker images ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

4、查看镜像、数据卷、容器所占用的体积

[root@hosts01 ~]# docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              2                   0                   112.4MB             112.4MB (100%)
Containers          0                   0                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Build Cache       

5、显示中间层镜像

[root@hosts01 ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        3 weeks ago         112MB
hello-world         latest              f2a91732366c        4 months ago        1.85kB

6、列出特定的某个镜像,也就是说指定仓库名和标签

[root@hosts01 ~]# docker image ls ubuntu:16.04
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        3 weeks ago         112MB

7、查看虚悬镜像

[root@hosts01 ~]# docker image ls -f dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

8、按指定格式显示【基于Go模板语法】https://gohugo.io/templates/go-templates/

[root@hosts01 ~]# docker image ls --format "{{.ID}}: {{.Repository}}"
4ece83276ddb: mongo
f975c5035748: ubuntu
f2a91732366c: hello-world

9、删除镜像

[root@hosts01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mongo               3.2                 4ece83276ddb        2 weeks ago         300MB
ubuntu              16.04               f975c5035748        3 weeks ago         112MB
hello-world         latest              f2a91732366c        4 months ago        1.85kB
[root@hosts01 ~]# docker image rm f2a91732366c
Untagged: hello-world:latest
Untagged: hello-world@sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
Deleted: sha256:f2a91732366c0332ccd7afd2a5c4ff2b9af81f549370f7a19acd460f87686bc7
Deleted: sha256:f999ae22f308fea973e5a25b57699b5daf6b0f1150ac2a5c2ea9d7fecee50fdf

Untagged:表示删除标签
Deleted:表示删除镜像

有时候会发现删除镜像后还是会残留一些东西占用空间。使用命令:docker system prune -a 彻底清除关闭的容器镜像

10、查看镜像修改内容

[root@hosts01 ~]# docker diff 22078e7ebfda
C /root
A /root/.bash_history
A /test

11、构成新的镜像

[root@hosts01 ~]# docker commit --author "Tao Wang@gmail.com>" \
--message "新增文件" \
dazzling_jones \
ubuntu:v2
sha256:d6d67aa8012322209e03b5c3217f639264ff6fe453f583d798892f262559691b

[root@hosts01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              v2                  d6d67aa80123        6 minutes ago       112MB
mongo               3.2                 4ece83276ddb        2 weeks ago         300MB
ubuntu              16.04               f975c5035748        3 weeks ago         112MB
ubuntu              latest              f975c5035748        3 weeks ago         112MB

注意:生产一般不使用commit制作镜像,使用dockerfile制作

12、查看镜像内历史

[root@hosts01 ~]# docker history ubuntu:v2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d6d67aa80123        8 minutes ago       bash                                            133B                新增文件
f975c5035748        3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
           3 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
           3 weeks ago         /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   2.76kB              
           3 weeks ago         /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
           3 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
           3 weeks ago         /bin/sh -c #(nop) ADD file:c753df38640ab6e24…   112MB 

13、批量停止容器

docker stop $(docker ps -q)
2dce413112fd
d9e1f751edfb
d3856fce8c4c
100201a82554
9f6df613e525

你可能感兴趣的:(云计算)