Docker学习(6) Docker镜像

2019-10-26  本文已影响0人  August________

Docker学习(5) Docker镜像

简介

详解

镜像和容器

lhf@lhf-virtual-machine:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
613dad3cfc8d        ubuntu:latest       "/bin/bash"         17 seconds ago      Up 16 seconds                           beautiful_leakey
lhf@lhf-virtual-machine:~$ docker image rm ubuntu:latest
Error response from daemon: conflict: unable to remove repository reference "ubuntu:latest" (must force) - container 613dad3cfc8d is using its referenced image cf0f3ca922e0

镜像通常比较小

拉取镜像

lhf@lhf-virtual-machine:~$ sudo ls /var/lib/docker
[sudo] lhf 的密码: 
builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
lhf@lhf-virtual-machine:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              e63fd667d16a        2 days ago          71.4MB
alpine              latest              965ea09ff2eb        4 days ago          5.55MB
ubuntu              latest              cf0f3ca922e0        7 days ago          64.2MB

lhf@lhf-virtual-machine:~$ docker pull centos:latest
latest: Pulling from library/centos
729ec3a6ada3: Pull complete 
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

镜像仓库服务

官方和非官方镜像仓库

镜像命名和标签

lhf@lhf-virtual-machine:~$ docker pull alpine:latest
latest: Pulling from library/alpine
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Image is up to date for alpine:latest
docker.io/library/alpine:latest
lhf@lhf-virtual-machine:~$ docker pull ubuntu:latest
latest: Pulling from library/ubuntu
Digest: sha256:a7b8b7b33e44b123d7f997bd4d3d0a59fafc63e203d17efedf09ff3f6f516152
Status: Image is up to date for ubuntu:latest
docker.io/library/ubuntu:latest

为镜像打多个标签

过滤docker image ls 的输出的内容

lhf@lhf-virtual-machine:~$ docker image ls --filter dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>  <none>

docker支持过滤方式:

  1. dangling:可以指定true后false,返回悬虚镜像(true)和非悬虚镜像(false).
  2. before:需要镜像名称和ID作为参数,返回在之前被创建的镜像。
  3. since:与before类似:返回是指定镜像之后创建的全部镜像。
  4. label:根据标注(label)的名称或者值。
  5. reference:过滤标签为latest的镜像
lhf@lhf-virtual-machine:~$ docker image ls --filter=reference="*:latest"
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              e63fd667d16a        2 days ago          71.4MB
alpine              latest              965ea09ff2eb        4 days ago          5.55MB
ubuntu              latest              cf0f3ca922e0        7 days ago          64.2MB
centos              latest              0f3e07c0138f        3 weeks ago         220MB

lhf@lhf-virtual-machine:~$ docker image ls --format "{{.Size}}"
71.4MB
5.55MB
64.2MB
220MB

lhf@lhf-virtual-machine:~$ docker image ls --format "{{.Repository}}:{{.Tag}}:{{.Size}}"
test:latest:71.4MB
alpine:latest:5.55MB
ubuntu:latest:64.2MB
centos:latest:220MB

通过CLI方式搜索Docker Hub

lhf@lhf-virtual-machine:~$ docker search nigelpoulton
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nigelpoulton/pluralsight-docker-ci   Simple web app used in my Pluralsight videos…   18                                      [OK]
nigelpoulton/tu-demo                                                                 11                                      
nigelpoulton/k8sbook                 Simple web app used for demos in The Kuberne…   2
<snip>

lhf@lhf-virtual-machine:~$ docker search alpine
NAME                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
alpine                                 A minimal Docker image based on Alpine Linux…   5757                [OK]                
mhart/alpine-node                      Minimal Node.js built on Alpine Linux           444                                     
anapsix/alpine-java                    Oracle Java 8 (and 7) with GLIBC 2.28 over A…   427 
<snip>


lhf@lhf-virtual-machine:~$ docker search alpine --filter "is-official=true"
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
alpine              A minimal Docker image based on Alpine Linux…   5757                [OK] 

镜像和分层

lhf@lhf-virtual-machine:~$ docker image inspect ubuntu:latest
<snip>
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:a090697502b8d19fbc83afb24d8fb59b01e48bf87763a00ca55cfff42423ad36",
                "sha256:97e6b67a30f1efeb050ada13c2afa1afd748e175ae744027dd0cce1f2931a594",
                "sha256:100ef12ce3a46c3242d186dbbadedff1638dc1f69cab4e1fbf73489049c01c25",
                "sha256:19331eff40f01dd084a3f966cc6939e828d617d777163706b8a13d0f972704d1"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
   

共享镜像层

根据摘要拉取镜像

lhf@lhf-virtual-machine:~$ docker image ls --digests alpine
REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
alpine              latest              sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a   965ea09ff2eb        4 days ago          5.55MB

镜像散列值(摘要)

多层架构的镜像

删除镜像

lhf@lhf-virtual-machine:~$ docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              e63fd667d16a        3 days ago          71.4MB
alpine              latest              965ea09ff2eb        4 days ago          5.55MB
ubuntu              latest              cf0f3ca922e0        7 days ago          64.2MB
lhf@lhf-virtual-machine:~$ docker image rm cf0f3ca922e0
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:a7b8b7b33e44b123d7f997bd4d3d0a59fafc63e203d17efedf09ff3f6f516152
Deleted: sha256:cf0f3ca922e08045795f67138b394c7287fbc0f4842ee39244a1a1aaca8c5e1c
Deleted: sha256:c808877c0adcf4ff8dcd2917c5c517dcfc76e9e8a035728fd8f0eae195d11908
Deleted: sha256:cdf75cc6b4d28e72a9931be2a88c6c421ad03cbf984b099916a74f107e6708ff
Deleted: sha256:b9997ded97a1c277d55be0d803cf76ee6e7b2e8235d610de0020a7c84c837b93
Deleted: sha256:a090697502b8d19fbc83afb24d8fb59b01e48bf87763a00ca55cfff42423ad36

lhf@lhf-virtual-machine:~$ docker image rm $(docker image ls -q) -f
Untagged: test:latest
Deleted: sha256:e63fd667d16a799a73adf65c4882461171125f811326827b867eb5acca9d89b5
Deleted: sha256:a3656648909cd134aa602878560463d4e4b4ee48b84cf10d33b93617f98b5beb
Deleted: sha256:677c10bc62019a183422f8142accbcc09d9b65b317421dbbcd1b0935a0ca7179
Deleted: sha256:4f515774097fccbbec3608cee499fa51f1b0e9b70d2b1fdc1af6f3e2d725baf2
Deleted: sha256:6c293be80bd10ff2bdc72e3833be631bf9e734b0abbed353858f4ed04d6e3779
Deleted: sha256:dc254500dcdad9c1092f8e5a37ee6378a7df9320a84767933cf6c8692005d753
Deleted: sha256:5935d67da60e681d0ef3bedd55bde06a54e60f2c2adc8e73640943dcef8ef521
Untagged: alpine:latest
Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652
lhf@lhf-virtual-machine:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

Docker 镜像——命令

上一篇 下一篇

猜你喜欢

热点阅读