1.镜像的基本信息
[root@host1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 802541663949 2 weeks ago 78.1MB
hello-world latest 1b44b5a3e06a 4 weeks ago 10.1kB
执行 docker images 命令时加上 --no-trunc 选项会显示完整的镜像 ID
例如,查看上述 Ubuntu 镜像的完整信息如下
[root@host1 ~]# docker images ubuntu --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest sha256:802541663949fbd5bbd8f35045af10005f51885164e798e2ee8d1dc39ed8888d 2 weeks ago 78.1MB
结果表明该镜像的完整 ID 如下
sha256:802541663949fbd5bbd8f35045af10005f51885164e798e2ee8d1dc39ed8888d
执行 docker images 命令时加上 -digests 选项即可获取镜像摘要值
[root@host1 ~]# docker images ubuntu --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
ubuntu latest sha256:9cbed754112939e914291337b5e554b07ad7c392491dba6daf25eef1332a22e8 802541663949 2 weeks ago 78.1MB
2.掌握镜像的基本操作
1.拉取镜像(例如拉取16.04版本的 Ubuntu)
[root@host1 ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
58690f9b18fc: Pull complete
b51569e7c507: Pull complete
da8ef40b9eca: Pull complete
fb15d46c38dc: Pull complete
Digest: sha256:1f1a2d56de1d604801a9671f301190704c25d604a416f59e03c04f5c6ffee0d6
Status: Downloaded newer image for ubuntu:16.04
docker.io/library/ubuntu:16.04
2.显示本地的镜像列表
[root@host1 ~]# docker images -f "before=ubuntu:18.04"
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 b6f507652425 4 years ago 135MB
3.查看镜像的详细信息
[root@host1 ~]# docker inspect --format='{{.Architecture}}' ubuntu
amd64
通过 JSON 格式的 RootFS 子节点查看镜像的根文件系统信息
[root@host1 ~]# docker inspect --format '{{json .RootFS}}' ubuntu
{"Type":"layers","Layers":["sha256:9d592720ced4a7a4ddf16adef8a126e4c8c49f22114de769343320b37674321e"]}
4.查看镜像的历史信息
[root@host1 ~]# docker history ubuntu:16.04
IMAGE CREATED CREATED BY SIZE COMMENT
b6f507652425 4 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 4 years ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 4 years ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 4 years ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 4 years ago /bin/sh -c #(nop) ADD file:11b425d4c08e81a3e… 135MB
5.查找镜像
[root@host1 ~]# docker search httpd #学校无法访问外网,具体信息跳过
6.删除本地镜像
[root@host1 ~]# docker rmi ubuntu:16.04
Untagged: ubuntu:16.04
Untagged: ubuntu@sha256:1f1a2d56de1d604801a9671f301190704c25d604a416f59e03c04f5c6ffee0d6
Deleted: sha256:b6f50765242581c887ff1acc2511fa2d885c52d8fb3ac8c4bba131fd86567f2e
Deleted: sha256:0214f4b057d78b44fd12702828152f67c0ce115f9346acc63acdf997cab7e7c8
Deleted: sha256:1b9d0485372c5562fa614d5b35766f6c442539bcee9825a6e90d1158c3299a61
Deleted: sha256:3c0f34be6eb98057c607b9080237cce0be0b86f52d51ba620dc018a3d421baea
Deleted: sha256:be96a3f634de79f523f07c7e4e0216c28af45eb5776e7a6238a2392f71e01069
3.验证镜像的分层结构
1.建立项目目录,存放 Dockerfile 及其相关文件
[root@host1 ~]# mkdir -p ch02/img-layers && cd ch02/img-layers
2.在项目目录中创建 app.py 文件,加入以下内容
[root@host1 img-layers]# vi app.py
[root@host1 img-layers]# cat app.py
#!/user/bin/python
print("Hello, World")
3.在项目目录中创建 Dockerfile,加入以下内容
[root@host1 img-layers]# vi Dockerfile
[root@host1 img-layers]# cat Dockerfile
FROM ubuntu:16.04
COPY ./ /app
RUN apt-get -y update && apt-get install -y python
CMD python /app/app.py
4.检验
[root@host1 img-layers]# ls
app.py Dockerfile
4.基于 Dockerfile 构建一个镜像
[root@host1 img-layers]# docker build -t="img-layers-test" .
[+] Building 276.6s (7/7) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 202B 0.0s=> [internal] load metadata for docker.io/library/ubuntu:16.04 5.1s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [internal] load build context 0.0s=> => transferring context: 338B 0.0s=> [1/3] FROM docker.io/library/ubuntu:16.04@sha256:1f1a2d56de1d604801a9671f301190704c25d604a41 9.2s=> => resolve docker.io/library/ubuntu:16.04@sha256:1f1a2d56de1d604801a9671f301190704c25d604a41 0.0s=> => sha256:58690f9b18fca6469a14da4e212c96849469f9b1be6661d2342a4bf01774aa50 46.50MB / 46.50MB 7.9s=> => sha256:b51569e7c50720acf6860327847fe342a1afbe148d24c529fb81df105e3eed01 857B / 857B 2.4s=> => sha256:da8ef40b9ecabc2679fe2419957220c0272a965c5cf7e0269fa1aeeb8c56f2e1 528B / 528B 0.8s=> => sha256:1f1a2d56de1d604801a9671f301190704c25d604a416f59e03c04f5c6ffee0d6 1.42kB / 1.42kB 0.0s=> => sha256:a3785f78ab8547ae2710c89e627783cfa7ee7824d3468cae6835c9f4eae23ff7 1.15kB / 1.15kB 0.0s=> => sha256:b6f50765242581c887ff1acc2511fa2d885c52d8fb3ac8c4bba131fd86567f2e 3.36kB / 3.36kB 0.0s=> => sha256:fb15d46c38dcd1ea0b1990006c3366ecd10c79d374f341687eb2cb23a2c8672e 170B / 170B 2.3s=> => extracting sha256:58690f9b18fca6469a14da4e212c96849469f9b1be6661d2342a4bf01774aa50 1.3s=> => extracting sha256:b51569e7c50720acf6860327847fe342a1afbe148d24c529fb81df105e3eed01 0.0s=> => extracting sha256:da8ef40b9ecabc2679fe2419957220c0272a965c5cf7e0269fa1aeeb8c56f2e1 0.0s=> => extracting sha256:fb15d46c38dcd1ea0b1990006c3366ecd10c79d374f341687eb2cb23a2c8672e 0.0s=> [2/3] COPY ./ /app 0.1s=> ERROR [3/3] RUN apt-get -y update && apt-get install -y python 262.1s
------ > [3/3] RUN apt-get -y update && apt-get install -y python:
2.137 Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
4.892 Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [106 kB]
6.192 Get:3 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [106 kB]
7.172 Get:4 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
14.91 Get:5 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
15.42 Get:6 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
19.29 Get:7 http://security.ubuntu.com/ubuntu xenial-security InRelease [106 kB]
21.08 Get:8 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [1151 kB]
27.02 Get:9 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [15.9 kB]
27.06 Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [928 kB]
31.02 Get:11 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [8820 B]
36.69 Get:12 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
36.91 Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1608 kB]
38.82 Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [16.4 kB]
38.83 Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [1483 kB]
41.10 Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [25.0 kB]
41.10 Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [11.3 kB]
41.10 Get:18 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [12.9 kB]
41.16 Fetched 17.4 MB in 41s (424 kB/s)
41.16 Reading package lists...
41.64 Reading package lists...
42.21 Building dependency tree...
42.28 Reading state information...
42.34 The following additional packages will be installed:
42.34 file libexpat1 libffi6 libmagic1 libpython-stdlib libpython2.7-minimal
42.34 libpython2.7-stdlib libsqlite3-0 libssl1.0.0 mime-support python-minimal
42.34 python2.7 python2.7-minimal
42.34 Suggested packages:
42.34 python-doc python-tk python2.7-doc binutils binfmt-support
42.34 The following NEW packages will be installed:
42.34 file libexpat1 libffi6 libmagic1 libpython-stdlib libpython2.7-minimal
42.34 libpython2.7-stdlib libsqlite3-0 libssl1.0.0 mime-support python
42.34 python-minimal python2.7 python2.7-minimal
43.11 0 upgraded, 14 newly installed, 0 to remove and 2 not upgraded.
43.11 Need to get 5717 kB of archives.
43.11 After this operation, 25.7 MB of additional disk space will be used.
43.11 Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7-minimal amd64 2.7.12-1ubuntu0~16.04.18 [338 kB]
55.69 Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python2.7-minimal amd64 2.7.12-1ubuntu0~16.04.18 [1260 kB]
65.18 Get:3 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python-minimal amd64 2.7.12-1~16.04 [28.1 kB]
92.63 Get:4 http://archive.ubuntu.com/ubuntu xenial/main amd64 mime-support all 3.59ubuntu1 [31.0 kB]
213.7 Err:4 http://archive.ubuntu.com/ubuntu xenial/main amd64 mime-support all 3.59ubuntu1
213.7 Connection failed [IP: 91.189.91.81 80]
254.0 Get:5 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libexpat1 amd64 2.1.0-7ubuntu0.16.04.5 [71.5 kB]
255.3 Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 libffi6 amd64 3.2.1-4 [17.8 kB]
255.7 Get:7 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libsqlite3-0 amd64 3.11.0-1ubuntu1.5 [398 kB]
257.6 Get:8 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libssl1.0.0 amd64 1.0.2g-1ubuntu4.20 [1083 kB]
259.5 Get:9 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7-stdlib amd64 2.7.12-1ubuntu0~16.04.18 [1883 kB]
260.7 Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python2.7 amd64 2.7.12-1ubuntu0~16.04.18 [224 kB]
261.0 Get:11 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython-stdlib amd64 2.7.12-1~16.04 [7768 B]
261.4 Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 python amd64 2.7.12-1~16.04 [137 kB]
261.7 Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libmagic1 amd64 1:5.25-2ubuntu1.4 [216 kB]
262.1 Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 file amd64 1:5.25-2ubuntu1.4 [21.2 kB]
262.1 Fetched 5686 kB in 3min 39s (25.9 kB/s)
262.1 E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/m/mime-support/mime-support_3.59ubuntu1_all.deb Connection failed [IP: 91.189.91.81 80]
262.1
262.1 E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
------1 warning found (use docker --debug to expand):- JSONArgsRecommended: JSON arguments recommended for CMD to prevent unintended behavior related to OS signals (line 4)
Dockerfile:3
--------------------1 | FROM ubuntu:16.042 | COPY ./ /app3 | >>> RUN apt-get -y update && apt-get install -y python4 | CMD python /app/app.py5 |
--------------------
ERROR: failed to build: failed to solve: process "/bin/sh -c apt-get -y update && apt-get install -y python" did not complete successfully: exit code: 100
报错修改:
从构建日志来看,核心错误是 Ubuntu 16.04 镜像的软件源连接失败(无法下载 mime-support
包),且存在一个 CMD 命令格式的警告。从构建日志来看,核心错误是 Ubuntu 16.04 镜像的软件源连接失败(无法下载 mime-support
包),且存在一个 CMD 命令格式的警告
一、错误原因分析
Ubuntu 16.04 已停止官方支持(EOL)
Ubuntu 16.04 早在 2021 年就结束了长期支持,其默认软件源(archive.ubuntu.com
)不再维护,导致 apt-get 下载包时频繁失败(连接超时或拒绝),这是本次构建失败的根本原因。CMD 命令格式警告
Docker 建议CMD
使用 JSON 数组格式(而非字符串格式),避免与 OS 信号处理相关的意外问题(日志中JSONArgsRecommended
警告)。二、解决方案:修改 Dockerfile直接使用官方 Python 镜像(更简洁)
修改后的 Dockerfile 内容:
[root@host1 img-layers]# vi Dockerfile [root@host1 img-layers]# cat Dockerfile FROM python:3.9-slim WORKDIR /app COPY ./ /app CMD ["python", "/app/app.py"]
三、重新构建并测试
1.清理之前的构建缓存(可选,避免旧镜像干扰)
[root@host1 img-layers]# docker builder prune -f IDRECLAIMABLESIZELAST ACCESSED zlm34izyjiinlnjyjdkduu8sx* true 105B 3 minutes ago 23890v1vbf75tfjlrr7nycue2* true 0B 3 minutes ago taqlvmzw5ty0ju3xxlgadqiw2* true 146B 3 minutes ago esiimfsl59rnov42lqy9rnlet true 146B 3 minutes ago z91huwmp1ndhf13s7rozh90uk true 0B 3 minutes ago enk8gcxrzj3j0n0c10hyp24z0 true 0B 7 minutes ago svngceqpst73s4n5aq62eonba true 0B 7 minutes ago v6ex3r460c17eko42w83xsbkt true 0B 7 minutes ago Total:397B
2.重新构建镜像
[root@host1 img-layers]# docker build -t="img-layers-test" . [+] Building 19.0s (8/8) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 174B 0.0s=> [internal] load metadata for docker.io/library/python:3.9-slim 4.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [1/3] FROM docker.io/library/python:3.9-slim@sha256:914169c7c8398b1b90c0b0ff921c8027445e39d7 9.7s=> => resolve docker.io/library/python:3.9-slim@sha256:914169c7c8398b1b90c0b0ff921c8027445e39d7 0.0s=> => sha256:914169c7c8398b1b90c0b0ff921c8027445e39d7c25dc440337e56ce0f2566e6 10.36kB / 10.36kB 0.0s=> => sha256:213766eae7e1ad5da6140428e7f15db89f2c83caf906cc06fc9c5c8a0028e3b6 1.74kB / 1.74kB 0.0s=> => sha256:28f8802246faa922c08dd76e3ec467e3cb4278af72e99e1afa2f68dfb9ea991d 5.30kB / 5.30kB 0.0s=> => sha256:396b1da7636e2dcd10565cb4f2f952cbb4a8a38b58d3b86a2cacb172fb70117c 29.77MB / 29.77MB 8.3s=> => sha256:0219e1e5e6ef3ef9d91f78826576a112b1c20622c10c294a4a105811454d1cb1 1.29MB / 1.29MB 1.3s=> => sha256:5ec99fe17015e703c289d110b020e4e362d5b425be957d68bfb400d56d83f234 13.37MB / 13.37MB 4.8s=> => sha256:ea3499df304f0a84e9f076a05f0cfe2a64d8fcb884894ce682df9204c6a18a91 249B / 249B 2.1s=> => extracting sha256:396b1da7636e2dcd10565cb4f2f952cbb4a8a38b58d3b86a2cacb172fb70117c 0.8s=> => extracting sha256:0219e1e5e6ef3ef9d91f78826576a112b1c20622c10c294a4a105811454d1cb1 0.1s=> => extracting sha256:5ec99fe17015e703c289d110b020e4e362d5b425be957d68bfb400d56d83f234 0.5s=> => extracting sha256:ea3499df304f0a84e9f076a05f0cfe2a64d8fcb884894ce682df9204c6a18a91 0.0s=> [internal] load build context 0.0s=> => transferring context: 310B 0.0s=> [2/3] WORKDIR /app 0.1s=> [3/3] COPY ./ /app 5.2s=> exporting to image 0.0s=> => exporting layers 0.0s=> => writing image sha256:f3993039c06a09804164873d3c78a4d6695fbc7e140e755ea1875cd191a4ec25 0.0s=> => naming to docker.io/library/img-layers-test
3.运行镜像验证结果
[root@host1 img-layers]# docker run img-layers-test Hello, World
4.查看该镜像的分层信息
[root@host1 img-layers]# docker history img-layers-test
IMAGE CREATED CREATED BY SIZE COMMENT
f3993039c06a 10 minutes ago CMD ["python" "/app/app.py"] 0B buildkit.dockerfile.v0
<missing> 10 minutes ago COPY ./ /app # buildkit 118B buildkit.dockerfile.v0
<missing> 10 minutes ago WORKDIR /app 0B buildkit.dockerfile.v0
<missing> 4 weeks ago CMD ["python3"] 0B buildkit.dockerfile.v0
<missing> 4 weeks ago RUN /bin/sh -c set -eux; for src in idle3 p… 36B buildkit.dockerfile.v0
<missing> 4 weeks ago RUN /bin/sh -c set -eux; savedAptMark="$(a… 38.1MB buildkit.dockerfile.v0
<missing> 4 weeks ago ENV PYTHON_SHA256=61a42919e13d539f7673cf11d1… 0B buildkit.dockerfile.v0
<missing> 4 weeks ago ENV PYTHON_VERSION=3.9.23 0B buildkit.dockerfile.v0
<missing> 4 weeks ago ENV GPG_KEY=E3FF2839C048B25C084DEBE9B26995E3… 0B buildkit.dockerfile.v0
<missing> 4 weeks ago RUN /bin/sh -c set -eux; apt-get update; a… 3.8MB buildkit.dockerfile.v0
<missing> 4 weeks ago ENV LANG=C.UTF-8 0B buildkit.dockerfile.v0
<missing> 4 weeks ago ENV PATH=/usr/local/bin:/usr/local/sbin:/usr… 0B buildkit.dockerfile.v0
<missing> 4 weeks ago # debian.sh --arch 'amd64' out/ 'trixie' '@1… 78.6MB debuerreotype 0.15
至此,该镜像的分层结构及构建方式就非常清楚明了了。