千家信息网

Docker镜像的使用方法

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章将为大家详细讲解有关Docker镜像的使用方法,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。当运行容器时,使用的镜像如果在本地中不存在,doc
千家信息网最后更新 2026年11月04日Docker镜像的使用方法

这篇文章将为大家详细讲解有关Docker镜像的使用方法,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
下面我们来学习:
1、管理和使用本地 Docker 主机镜像
2、创建镜像


列出镜像列表
我们可以使用 docker images 来列出本地主机上的镜像。
[root@huixuan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest e38bc07ac18e 2 weeks ago 1.85 kB
docker.io/ubuntu 15.10 9b9cb95443b5 21 months ago 137 MB
docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@huixuan ~]#


各个选项说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:
[root@huixuan ~]# docker run -t -i ubuntu:15.10 /bin/bash
root@38a4ad967a4a:/#


如果要使用版本为14.04的ubuntu系统镜像来运行容器时,命令如下:
[root@huixuan ~]# docker run -t -i ubuntu:14.04 /bin/bash


如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。


获取一个新的镜像
当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。
[root@huixuan ~]# docker pull ubuntu:13.10


下载完成后,我们可以直接使用这个镜像来运行容器。


查找镜像
我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个httpd的镜像来作为我们的web服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。


[root@huixuan ~]# docker search httpd
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/httpd The Apache HTTP Server Project 1668 [OK]
docker.io docker.io/hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with ... 40
docker.io docker.io/centos/httpd 17 [OK]
docker.io docker.io/centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or b... 12
docker.io docker.io/armhf/httpd The Apache HTTP Server Project 8
docker.io docker.io/macadmins/netboot-httpd use in combination with bruienne/bsdpy 5 [OK]
docker.io docker.io/lolhens/httpd Apache httpd 2 Server 2 [OK]
docker.io docker.io/salim1983hoop/httpd24 Dockerfile running apache config 2 [OK]
docker.io docker.io/epflidevelop/os-wp-httpd WP httpd 1 [OK]
docker.io docker.io/fboaventura/dckr-httpd Small footprint http server to use with ot... 1 [OK]
docker.io docker.io/lead4good/httpd-fpm httpd server which connects via fcgi proxy... 1 [OK]
docker.io docker.io/rgielen/httpd-image-simple Docker image for simple Apache httpd based... 1 [OK]
docker.io docker.io/tplatform/aws-linux-httpd24-php71-fpm aws-linux-httpd24-php71-fpm 1 [OK]
docker.io docker.io/buzzardev/httpd Based on the official httpd image 0 [OK]
docker.io docker.io/cilium/demo-httpd 0
docker.io docker.io/dockerpinata/httpd 0
docker.io docker.io/fintaffy/fintaffy-httpd 0
docker.io docker.io/interlutions/httpd httpd docker image with debian-based confi... 0 [OK]
docker.io docker.io/manageiq/httpd Container with httpd, built on CentOS for ... 0 [OK]
docker.io docker.io/manasip/httpd 0
docker.io docker.io/ppc64le/httpd The Apache HTTP Server Project 0
docker.io docker.io/publici/httpd httpd:latest 0 [OK]
docker.io docker.io/tplatform/aws-linux-httpd24-php70 aws-linux-httpd24-php70 0 [OK]
docker.io docker.io/tplatform/aws-linux-httpd24-php71 aws-linux-httpd24-php71 0 [OK]
docker.io docker.io/trollin/httpd 0
[root@huixuan ~]#


NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否docker官方发布


拖取镜像
我们决定使用上图中的httpd 官方版本的镜像,使用命令 docker pull 来下载镜像。
[root@huixuan ~]# docker pull httpd


下载完成后,我们就可以使用这个镜像了。
[root@huixuan ~]# docker run httpd


创建镜像
当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像


更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。
[root@huixuan ~]# docker run -t -i ubuntu:15.10 /bin/bash
root@493da1af5d63:/#


在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit命令来退出这个容器。
此时ID为493da1af5d63的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit来提交容器副本。
[root@huixuan ~]# docker commit -m="has update" -a="tonykorn97" 493da1af5d63 tonykorn97/ubuntu:v2
sha256:6bb0f7a6d1e68fa93197761f3fa86a18397b1dfff563a0405e48cc833d0b3112


各个参数说明:
-m:提交的描述信息
-a:指定镜像作者
493da1af5d63:容器ID
tonykorn97/ubuntu:v2:指定要创建的目标镜像名
我们可以使用 docker images 命令来查看我们的新镜像 tonykorn97/ubuntu:v2:


[root@huixuan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tonykorn97/ubuntu v2 6bb0f7a6d1e6 2 seconds ago 137 MB
3248dea71f18 About a minute ago 137 MB
docker.io/ubuntu 14.04 8cef1fa16c77 3 days ago 223 MB
docker.io/hello-world latest e38bc07ac18e 2 weeks ago 1.85 kB
docker.io/ubuntu 15.10 9b9cb95443b5 21 months ago 137 MB
docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@huixuan ~]#



使用我们的新镜像 tonykorn97/ubuntu 来启动一个容器
[root@huixuan ~]# docker run -t -i tonykorn97/ubuntu:v2 /bin/bash
root@694ecac26845:/#


构建镜像
我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
[root@huixuan ~]# cat Dockerfile
FROM centos
MAINTAINER Fisher "tonykorn97@sudops.com"


RUN /bin/echo 'root:123456' |chpasswd
RUN useradd tonykorn97
RUN /bin/echo 'tonykorn97:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
[root@huixuan ~]#


每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。
第一条FROM,指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。
然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。


[root@huixuan ~]# docker build -t docker.io/centos .
Sending build context to Docker daemon 42.5 kB
Step 1/9 : FROM centos
---> e934aafc2206
Step 2/9 : MAINTAINER Fisher "tonykorn97@sudops.com"
---> Running in e8fba274a04f
---> a957802c0f10
Removing intermediate container e8fba274a04f
Step 3/9 : RUN /bin/echo 'root:123456' |chpasswd
---> Running in 9b6d82512651


---> 91895d25b7ca
Removing intermediate container 9b6d82512651
Step 4/9 : RUN useradd tonykorn97
---> Running in ca2a783aa01f


---> da8e8eb44cb9
Removing intermediate container ca2a783aa01f
Step 5/9 : RUN /bin/echo 'tonykorn97:123456' |chpasswd
---> Running in bcfd276a69cc


---> e9bef27837e4
Removing intermediate container bcfd276a69cc
Step 6/9 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
---> Running in fb07b34deefd

---> b34b2ad1c65b
Removing intermediate container fb07b34deefd
Step 7/9 : EXPOSE 22
---> Running in a580e3c0df37
---> ae38f15b66c3
Removing intermediate container a580e3c0df37
Step 8/9 : EXPOSE 80
---> Running in b3e04bf32806
---> 8964d002a4a9
Removing intermediate container b3e04bf32806
Step 9/9 : CMD /usr/sbin/sshd -D
---> Running in 804574e55fdc
---> 3a42651974ec
Removing intermediate container 804574e55fdc
Successfully built 3a42651974ec
[root@huixuan ~]#

参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在,镜像ID为3a42651974ec
[root@huixuan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 3a42651974ec 42 seconds ago 199 MB
tonykorn97/ubuntu v2 6bb0f7a6d1e6 27 minutes ago 137 MB
3248dea71f18 29 minutes ago 137 MB
docker.io/ubuntu 14.04 8cef1fa16c77 3 days ago 223 MB
docker.io/hello-world latest e38bc07ac18e 2 weeks ago 1.85 kB
docker.io/centos e934aafc2206 3 weeks ago 199 MB
docker.io/ubuntu 15.10 9b9cb95443b5 21 months ago 137 MB
docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@huixuan ~]#

我们可以使用新的镜像来创建容器
[root@huixuan ~]# docker run -t -i centos /bin/bash
[root@6263f2eb5f6c /]# id tonykorn97
uid=1000(tonykorn97) gid=1000(tonykorn97) groups=1000(tonykorn97)
[root@6263f2eb5f6c /]#

从上面看到新镜像已经包含我们创建的用户tonykorn97

设置镜像标签
我们可以使用 docker tag 命令,为镜像添加一个新的标签。
[root@huixuan ~]# docker tag 3a42651974ec tonykorn97/centos:dev


docker tag 镜像ID,这里是 3a42651974ec ,用户名称、镜像源名(repository name)和新的标签名(tag)。
使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。


[root@huixuan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 3a42651974ec 3 minutes ago 199 MB
tonykorn97/centos dev 3a42651974ec 3 minutes ago 199 MB
tonykorn97/ubuntu v2 6bb0f7a6d1e6 30 minutes ago 137 MB
3248dea71f18 32 minutes ago 137 MB
docker.io/ubuntu 14.04 8cef1fa16c77 3 days ago 223 MB
docker.io/hello-world latest e38bc07ac18e 2 weeks ago 1.85 kB
docker.io/centos e934aafc2206 3 weeks ago 199 MB
docker.io/ubuntu 15.10 9b9cb95443b5 21 months ago 137 MB
docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@huixuan ~]#

关于Docker镜像的使用方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

镜像 命令 容器 仓库 版本 指令 标签 运行 更新 不同 可以通过 文件 搜索 使用方法 方法 内容 参数 名称 多个 官方 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网是第几次科技革命的成果 新华互联网科技学校 科技企业和互联网企业 腾讯阿里互联网科技 互联网科技公司发展 狮子座互联网科技 互联网科技媒体 天津摩尔互联网科技 盛创互联网科技有限公司 博兴的互联网电子科技公司 互联网汽车科技小镇 顶级互联网科技成果有哪些 黑科技闪耀互联网之光 江阴互联网智能科技产品进口 洛阳昕诺飞互联网科技有限公司 深圳艾果互联网通讯科技 中科明德北京互联网科技 互联网科技年终奖 陕西云淼互联网科技公司 三亚科技创新互联网基地 互联网科技会 广州互联网络科技有限公司 北京禾田雨橡互联网科技 互联网金融科技委员会 创业天下互联网科技有限公司 宜昌元轩互联网科技有限公司 佛山市翊博互联网络科技有限公司 互联网领先科技宣传片 数字科技和互联网金融哪个好 科技互联网企业在行动
0