目录
- 1. 镜像简介
- 2. 获取镜像
- 3. 查看镜像信息
- 4. 搜索镜像
- 5. 创建镜像
- 5.1 基于已有镜像创建
- 5.2 使用Dockerfile 新建镜像
- 6. 删除镜像
1. 镜像简介
Docker 镜像(Image)是构建 Docker 容器的基础模板,其结构类似于一个根文件系统。例如官方提供的 ubuntu:16.04 镜像就包含了完整的 Ubuntu 16.04 最小系统根文件系统。在实际应用中,镜像将应用程序代码、运行所需的库、环境变量及配置文件等打包成一个可交付的运行环境,这个打包好的文件即为镜像。

镜像仓库服务(Image Registry)由多个镜像仓库(Image Repository)组成,而每个镜像仓库中可以存储多个不同的镜像。
对用户而言,镜像操作主要指 Docker 客户端的一系列行为,涵盖获取、创建、修改、运行、删除、搜索及查看等。其中,docker build、docker pull 和 docker run 是最核心的三个操作命令。
2. 获取镜像
Docker 采用镜像仓库集中存放镜像,用户既可以使用公共 Registry,也可以自建私有 Registry,这与代码库的管理方式类似。
仓库中通常存放有多个镜像,每个镜像又包含多个标签(Tag),每个标签对应一个具体的镜像版本。指定镜像时可使用 <仓库名>:<标签> 格式,若省略标签,系统将默认使用 Latest 作为版本号。
通过 docker pull 命令可从仓库下载镜像,其完整语法格式如下:
docker pull [OPTIONS] NAME[:TAG]
参数 OPTIONS 说明如下:
-a:--all-tags=true|false,用于控制是否获取仓库中的所有镜像,默认为否。
--disable-content-trust:用于忽略镜像的内容校验,该选项默认处于开启状态。
NAME:指定镜像仓库的名称,用于区分不同的镜像来源。
TAG:指定镜像标签,通常代表软件的版本信息。
对于 Docker 镜像,若未显式指定 TAG,系统将默认选择 latest 标签,从而下载仓库中最新版本的镜像。
一般而言,latest 标签意味着镜像内容会随最新版本变更而更新,因此内容具有不稳定性。出于生产环境的稳定性考虑,建议不要忽略 TAG 而直接使用默认的 latest 标记镜像。
可以直接从官方 Docker Hub 镜像源下载镜像,例如执行以下命令:
$ 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:0f71fa8d4d2d4292c3c617fda2b36f6dabe5c8b6e34c3dc5b0d17d4e704bd39c
Status: Downloaded newer image for ubuntu:16.04
docker.io/library/ubuntu:16.04
若从非官方仓库下载,则需在仓库名称前指定完整的仓库地址,例如:
$ docker pull hub.c.163.com/public/ubuntu:16.04
此外,有时需借助镜像代理服务来加速 Docker 镜像的获取过程,相关配置方法可在《Docker安装及加速》一文中查阅。
3. 查看镜像信息
使用 docker images 或 docker image ls 命令可以列出本地主机上已存在镜像的基本信息。
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
ubuntu 16.04 b6f507652425 11 months ago 135MB
输出结果中的各字段含义如下:
- REPOSITORY:镜像仓库名,例如 ubuntu 表示 ubuntu 系列的基础镜像。
- TAG:镜像标签,例如 18.04 是以 ubuntu 版本号作为镜像标签。
- IMAGE ID:镜像 ID,每个镜像拥有唯一的 ID。若两个镜像 ID 相同,说明它们指向同一个镜像,仅标签名称不同。
- CREATED:创建时间,显示的是镜像最后更新的时间点。
- SIZE:镜像的大小。
使用 docker inspect 命令可获取镜像的详细信息,包括创建者及各层的数字摘要等,其格式为:
docker inspect [IMAGE_NAME|ID]
具体示例如下:
$ docker inspect ubuntu:18.04
[
{
"Id": "sha256:5a214d77f5d747e6ed81632310baa6190301feeb875cf6bf9da560108fa09972",
"RepoTags": [
"ubuntu:18.04"
],
"RepoDigests": [
"ubuntu@sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6"
],
"Parent": "",
"Comment": "",
"Created": "2026-10-01T02:23:24.179667784Z",
"Container": "20d614d2eca1b5a9ad6d5a56a80efce44096b87ca76a98256eb51f8dbaf7a8d2",
"ContainerConfig": {
"Hostname": "20d614d2eca1",
......
4. 搜索镜像
使用 docker search 命令可以搜索 Docker Hub 官方仓库中的镜像,返回的信息包括镜像名字、描述、收藏数、是否官方创建及是否自动创建等。命令格式为:
docker search [option] keyword
可用的 option 选项主要包括:
- -f, --filter filter:用于过滤输出内容。
- --format string:用于格式化输出内容。
- --limit int:限制输出结果的个数,默认为 25 个。
- --automated:仅列出 automated build 类型的镜像。
- --no-trunc:显示完整的镜像描述信息。
例如,从官方库中搜索 mysql 相关镜像的操作如下:
$ docker search -f stars=10 mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12976 [OK]
mariadb MariaDB Server is a high performing open sou… 4972 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 594 [OK]
percona Percona Server is a fork of the MySQL relati… 583 [OK]
bitnami/mysql Bitnami MySQL Docker Image 72 [OK]
linuxserver/mysql-workbench 40
ubuntu/mysql MySQL open source fast, stable, multi-thread… 36
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 36
circleci/mysql MySQL is a widely used, open-source relation… 26
google/mysql MySQL server for Google Compute Engine 21 [OK]
rapidfort/mysql RapidFort optimized, hardened image for MySQL 13
搜索结果参数说明:
- NAME:镜像仓库源的名称。
- DESCRIPTION:镜像的描述信息。
- OFFICIAL:标识是否为 docker 官方发布。
- stars:类似于 Github 中的 star,表示点赞或喜欢的数量。
- AUTOMATED:表示是否为自动构建。
5. 创建镜像
镜像的来源主要有两种:一是从镜像库下载现有镜像,二是自行创建新镜像。创建新镜像的方法也分为两种:基于已有镜像创建,或使用 Dockerfile 从头构建。
5.1 基于已有镜像创建
首先,需使用目标镜像创建一个容器以运行它,并在容器内创建一个测试文件 test.txt。完成操作后,输入 exit 命令退出该容器。
$ docker run -t -i ubuntu:18.04 /bin/bash
root@49152c044299:/# touch test.txt
root@49152c044299:/# exit
此时,ID 为 49152c044299 的容器即为已完成更改的容器,可通过 docker commit 命令提交容器副本以生成新镜像。
$ docker container commit -m "add test.txt" -a "testdocker" 49152c044299 testdocker/ubuntu:test
sha256:9c44392a137352f2b22acbe322543eb6f30970b9bd2edf9145d19c28d5fe21fc
相关参数说明:
- -m:提供提交的描述信息。
- -a:指定镜像的作者。
- 49152c044299:容器的 ID。
- testdocker/ubuntu:test:指定要创建的目标镜像名称及标签。
随后可使用 docker image ls 命令查看新创建的镜像 testdocker/ubuntu:test:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
testdocker/ubuntu test 9c44392a1373 19 seconds ago 63.1MB
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
ubuntu 16.04 b6f507652425 11 months ago 135MB
5.2 使用Dockerfile 新建镜像
使用 docker build 命令可以从零开始构建一个新镜像。为此,需要创建一个包含一组指令的 Dockerfile 文件,告知 Docker 如何构建镜像。

首先使用 vim 编辑器编写一个 Dockerfile 文件:
FROM ubuntu:18.04
MAINTAINER test
WORKDIR /home
RUN touch testImage.txt
EXPOSE 80
文件中包含的关键指令如下:
- FROM:指定构建镜像所基于的基础镜像源。
- RUN:告诉 Docker 在镜像构建过程中执行的具体命令。
随后,利用 Dockerfile 文件,通过 docker build 命令来构建镜像:
$ docker build -t testimage/ubuntu:testimage1 .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM ubuntu:18.04
---> 5a214d77f5d7
Step 2/5 : MAINTAINER test
---> Running in 92f0afdaf3bf
Removing intermediate container 92f0afdaf3bf
---> 88d4b9bc0c0e
Step 3/5 : WORKDIR /home
---> Running in ec9fd2b1cbcc
Removing intermediate container ec9fd2b1cbcc
---> 6a91a52c5589
Step 4/5 : RUN touch testImage.txt
---> Running in d60d6a8de1db
Removing intermediate container d60d6a8de1db
---> 1a7c4f1cd570
Step 5/5 : EXPOSE 80
---> Running in 7c2922e1bb55
Removing intermediate container 7c2922e1bb55
---> f3fdbec0b0f9
Successfully built f3fdbec0b0f9
Successfully tagged testimage/ubuntu:testimage1
参数说明:
- -t:指定要创建的目标镜像名称。
- .:Dockerfile 文件所在的目录,也可指定 Dockerfile 的绝对路径。
使用 docker images 查看,创建的镜像已存在于列表中,其镜像 ID 为 f3fdbec0b0f9:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
testimage/ubuntu testimage1 f3fdbec0b0f9 38 seconds ago 63.1MB
testdocker/ubuntu test 9c44392a1373 45 minutes ago 63.1MB
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
ubuntu 16.04 b6f507652425 11 months ago 135MB
运行新创建的镜像,可以看到 /home 目录下已成功创建了测试文件 testImage.txt:
$ docker run -t -i testimage/ubuntu:testimage1 /bin/bash
root@952d273dc630:/home# ls
testImage.txt
6. 删除镜像
当不再需要某个镜像时,可通过 docker image rm 或 docker rmi 命令从 Docker 主机中删除该镜像。其格式为:
docker rmi [image]
docker image rm [image]
删除操作会在当前主机上移除该镜像及其相关的镜像层。这意味着该镜像将无法再通过 docker image ls 命令看到,且对应的包含镜像层数据的目录也会被删除。然而,如果某个镜像层被多个镜像共享,则只有当所有依赖该镜像层的镜像都被删除后,该共享的镜像层才会被真正删除。
例如,删除前面创建的测试镜像 testimage/ubuntu:testimage1:
$ docker rmi testimage/ubuntu:testimage1
Untagged: testimage/ubuntu:testimage1
Deleted: sha256:f3fdbec0b0f96a8f3bb2e6a4b07ba04dc45b9c59618c2b376ebea7b68ed53971
Deleted: sha256:1a7c4f1cd57038116b7a5eff05a323fc244a135741c2fd69ad89f21569f00afb
Deleted: sha256:22997b7cad7e4294d4eb9b02bc475e2bcc94c2dd168b8a8499adb02cfc4cb573
Deleted: sha256:6a91a52c5589c29f6439bf6c7def37942037a5346a2fd8520f3d8a1fc3d49ccb
Deleted: sha256:88d4b9bc0c0ee678efad3b8192d3c0cf017dfae142dbc7e87af4fecaafb4f847
删除完成后,再次执行 docker image ls 查看当前镜像列表,可以看到 testimage/ubuntu:testimage1 已不在列表中:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
testdocker/ubuntu test 9c44392a1373 About an hour ago 63.1MB
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
ubuntu 16.04 b6f507652425 11 months ago 135MB
如果被删除的镜像上存在正在运行状态的容器,删除操作将被禁止。在执行删除镜像命令之前,必须先停止并删除该镜像相关的全部容器。例如删除时遇到如下错误提示:
$ docker rmi testimage/ubuntu:testimage1
Error response from daemon: conflict: unable to remove repository reference "testimage/ubuntu:testimage1" (must force) - container 952d273dc630 is using its referenced image f3fdbec0b0f9
此时可先使用 docker ps -a 查看依赖的容器 ID,然后执行 docker rm [容器ID] 删除容器,最后再执行 docker rmi [镜像名] 来成功删除镜像。
到此这篇关于Docker超详细讲解镜像操作的文章就介绍到这了,更多相关Docker镜像操作内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!