欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker镜像的基本操作

程序员文章站 2024-03-12 11:17:38
...

获取镜像

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]
docker pull hello-world
localhost:my_test sukangshen$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
Status: Downloaded newer image for hello-world:latest
localhost:my_test sukangshen$

查看镜像列表

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]
docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
<none>                         <none>              599e3b0290cd        4 days ago          421MB
<none>                         <none>              6998f5aabe68        5 days ago          484MB
<none>                         <none>              12f997a68afb        5 days ago          484MB
<none>                         <none>              4f229677daca        6 days ago          484MB
<none>                         <none>              f6ed0b7852c5        6 days ago          484MB
<none>                         <none>              39c63c52fc6a        6 days ago          484MB
dnmp_php72                     latest              1a5f62f4795f        4 weeks ago         611MB
hello-world         latest              fce289e99eb9        4 months ago        1.84kB
虚悬镜像

上面的镜像列表中,还可以看到一个特殊的镜像,这个镜像既没有仓库名,也没有标签,均为 <none>。:

<none>                         <none>              4f229677daca        6 days ago          484MB

这个镜像原本是有镜像名和标签的,原来为 mongo:3.2,随着官方镜像维护,发布了新版本后,重新 docker pull mongo:3.2 时,mongo:3.2 这个镜像名被转移到了新下载的镜像身上,而旧的镜像上的这个名称则被取消,从而成为了 <none>。除了 docker pull 可能导致这种情况,docker build 也同样可以导致这种现象。由于新旧镜像同名,旧镜像名称被取消,从而出现仓库名、标签均为 <none> 的镜像。这类无标签镜像也被称为 虚悬镜像(dangling image) ,可以用下面的命令专门显示这类镜像:

docker image ls -f dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              00285df0df87        5 days ago          342 MB

一般来说,虚悬镜像已经失去了存在的价值,是可以随意删除的,可以用下面的命令删除。

docker image prune
中间层镜像

为了加速镜像构建、重复利用资源,Docker 会利用 中间层镜像。所以在使用一段时间后,可能会看到一些依赖的中间层镜像。默认的 docker image ls 列表中只会显示顶层镜像,如果希望显示包括中间层镜像在内的所有镜像的话,需要加 -a 参数。

 docker image ls -a

这样会看到很多无标签的镜像,与之前的虚悬镜像不同,这些无标签的镜像很多都是中间层镜像,是其它镜像所依赖的镜像。这些无标签镜像不应该删除,否则会导致上层镜像因为依赖丢失而出错。实际上,这些镜像也没必要删除,因为之前说过,相同的层只会存一遍,而这些镜像是别的镜像的依赖,因此并不会因为它们被列出来而多存了一份,无论如何你也会需要它们。只要删除那些依赖它们的镜像后,这些依赖的中间层镜像也会被连带删除。

查看镜像信息

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]
docker inspect hello-world
localhost:my_test sukangshen$ docker inspect hello-world
[
    {
        "Id": "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "[email protected]:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-01-01T01:29:27.650294696Z",
        "Container": "8e2caa5a514bb6d8b4f2a2553e9067498d261a0fd83a96aeaaf303943dff6ff9",
        "ContainerConfig": {
            "Hostname": "8e2caa5a514b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/hello\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:a6d1aaad8ca65655449a26146699fe9d61240071f6992975be7e720f1cd42440",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:a6d1aaad8ca65655449a26146699fe9d61240071f6992975be7e720f1cd42440",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1840,
        "VirtualSize": 1840,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/90fa938dbfd9549051616136abb31b97d883f81715b52ac31be844525dab0dc7/merged",
                "UpperDir": "/var/lib/docker/overlay2/90fa938dbfd9549051616136abb31b97d883f81715b52ac31be844525dab0dc7/diff",
                "WorkDir": "/var/lib/docker/overlay2/90fa938dbfd9549051616136abb31b97d883f81715b52ac31be844525dab0dc7/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
localhost:my_test sukangshen$

查找镜像

Usage:  docker search [OPTIONS] TERM
docker search hello-world

使用docker search命令可以搜索远端仓库*享的镜像,默认搜索Docker hub官方仓库中的镜像。

localhost:my_test sukangshen$ docker search hello-world
NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   929                 [OK]
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   124
tutum/hello-world                          Image to test docker deployments. Has Apache…   60                                      [OK]
dockercloud/hello-world                    Hello World!                                    15                                      [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          6                                       [OK]
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2
markmnei/hello-world-java-docker           Hello-World-Java-docker                         1                                       [OK]
souravpatnaik/hello-world-go               hello-world in Golang                           1
carinamarina/hello-world-app               This is a sample Python web application, run…   1                                       [OK]
kevindockercompany/hello-world                                                             0
koudaiii/hello-world                                                                       0
burdz/hello-world-k8s                      To provide a simple webserver that can have …   0                                       [OK]
infrastructureascode/hello-world           A tiny "Hello World" web server with a healt…   0                                       [OK]
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   0                                       [OK]
uniplaces/hello-world                                                                      0
ansibleplaybookbundle/hello-world          Simple containerized application that tests …   0
nirmata/hello-world                                                                        0                                       [OK]
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   0                                       [OK]
stumacsolutions/hello-world-container                                                      0
mbrainar/hello-world                       Python-based hello-world web service            0
sharor/hello-world                                                                         0
kousik93/hello-world                                                                       0
ebenjaminv9/hello-world                    Hello-world                                     0
s390x/hello-world                          Hello World! (an example of minimal Dockeriz…   0
jensendw/hello-world                                                                       0
localhost:my_test sukangshen$

删除镜像

使用docker rmi命令可以删除镜像,其中image可以为标签或ID。

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]
docker rmi hello-world
localhost:my_test sukangshen$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: [email protected]:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
localhost:my_test sukangshen$

注意:
当同一个镜像拥有多个标签,docker rmi只是删除该镜像多个标签中的指定标签而已,而不影响镜像文件。
当有该镜像创建的容器存在时,镜像文件默认是无法被删除的。
如果一个镜像就有一个tag的话,删除tag就删除了镜像的本身。

停止、删除所有的docker容器和镜像

列出所有的容器 ID

docker ps -aq

停止所有的容器

docker stop $(docker ps -aq)

删除所有的容器

docker rm $(docker ps -aq)

删除所有的镜像

docker rmi $(docker images -q)