千家信息网

Docker系列3:Docker镜像详解

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,一、Docker镜像介绍1、镜像组成介绍分层构建的,底层的是bootfs,上面的是rootfsbootfs的类型可以是btrfs、aufs、lxc,还需要一个内核,但是这个内核仅仅是用于启动容器的用户
千家信息网最后更新 2026年10月31日Docker系列3:Docker镜像详解

一、Docker镜像介绍

1、镜像组成介绍

分层构建的,底层的是bootfs,上面的是rootfs

  • bootfs的类型可以是btrfs、aufs、lxc,还需要一个内核,但是这个内核仅仅是用于启动容器的用户控件的

  • rootfs表现为一个根文件系统,这里面包括很多的文件和目录

  • 在启动容器的时候,这两层都是以只读的方式来挂载的。

构建镜像的基本流程

  1. 先准备一个bootfs

  2. 然后安装一个最小系统(base image)

  3. 在系统安装应用,如果是构建apache的镜像,那么就在base image上安装apache

注意:

  • 镜像都是只读的

  • 当启动容器的时候,会在镜像的基础上再添加一个当前容器的专用层,这层是读写的

  • 在删除容器的时候,这个容器的专属的这个读写层就会被删除,所以默认容器无法实现数据的持久存储。

2、镜像仓库

前面一讲过了,专门用来存储docker iamge的哪个位置称之为 docker registry,在启动容器的时候,本地的docker daemon会从指定的docker registry下载镜像,并完成启动。

docker registry是可以分为多类 的

  • Vendor registry:官方的仓库

  • Mirror registry:像阿里云之类的镜像加速

  • Private registry:用户自己创建镜像仓库,比如企业内部需要大规模部署时候,可以自己定制镜像,并放到自己仓库中

一般的registry有两部分组成:

第一部分:Repository

  • 一个registry可以有多个repository

  • Repository可以分为顶级仓库和用户仓库,用户仓库的命名是:用户名/仓库名

  • Repository的名称一般就是应用的名称,而且在Repository中有应用的多个版本

第二部分:index

  • 维护账户信息

  • 提供检索端口

3、从镜像仓库下载镜像的方法

格式如下

docker pull [:port] /[/]:
  • registry:port 这里是指定从哪个docker服务器来获取镜像,如果用的是docker官方自己的仓库(hub.docker.com),那么这里可以省略。

  • namespace 这里是指定来自于哪个名称空间,也就是哪个用户的仓库, 如果的顶级的仓库,那么这个也可以省略

除了https://hub.docker.com之后,其实还有别的,例如:https://hub.daocloud.io/,再例如CoreOS所维护的:https://quay.io

  • 因为不是默认仓库, 因此在下载镜像的时候,需要指定地址


从quay.io 下载 flannel举例如下

第一步:登录https://quay.io,搜索flannel

第二步:找到项目地址

第三步:查看下载镜像的方法


  • 这种方法是不能用的,因为需要指定标签

第四步:查看具体的标签

第五步:下载镜像

[root@host1 ~]# docker pull quay.io/coreos/flannel:v0.11.0-s390x
[root@host1 ~]# docker image lsREPOSITORY               TAG                 IMAGE ID            CREATED             SIZEbusybox                  latest              b534869c81f0        2 weeks ago         1.22MBnginx                    1.14-alpine         8a2fb25a19f5        8 months ago        16MBquay.io/coreos/flannel   v0.10.0-s390x       463654e4ed2d        23 months ago       47MB


二、制作镜像

1、制作镜像方法种类

  • 基于dockerfile

  • 基于现有的容器:基于容器做镜像,一般就是先安装一个最小容器,然后在这个容器中安装应用程序,然后将这个安装了程序的容器做成镜像就可以了

  • 基于docker hub的自动创建功能

2、基于现有容器做镜像

第一步:启动一个busybox容器,并创建一个html页面

[root@host1 ~]# docker run --name img1 -it busybox/ # mkdir /data/html -p/ # echo "test page[v1.0]">>/data/html/index.html

第二步:再开一个终端,将容器制作成镜像

  • 制作镜像用命令commit

  • 要制作镜像的容器不能停止

  • 将容器制作为镜像的时候,最好让容器暂停一下,这需要用选项-p

  • 默认制作的镜像没有tag,也不属于任何的repository

[root@host1 ~]# docker commit -p img1sha256:cd7cb2a774400c721ed71f62bd20abe2c000f1d0f7d51d3bf025db1239b86b7d
[root@host1 ~]# docker image lsREPOSITORY               TAG                 IMAGE ID            CREATED             SIZE                                 cd7cb2a77440        6 seconds ago       1.22MB

第三步:给镜像打标签

  • 打标签用tag命令

  • 一个镜像可以有多个不同的标签

[root@host1 ~]# docker tag cd7cb2a77440 zxhk/httpd:v1-0[root@host1 ~]# docker image lsREPOSITORY               TAG                 IMAGE ID            CREATED             SIZEzxhk/httpd               v1-0                cd7cb2a77440        2 minutes ago       1.22MB

再打个标签

[root@host1 ~]# docker tag cd7cb2a77440 zxhk/httpd:latest[root@host1 ~]# docker image lsREPOSITORY               TAG                 IMAGE ID            CREATED             SIZEzxhk/httpd               latest              cd7cb2a77440        3 minutes ago       1.22MBzxhk/httpd               v1-0                cd7cb2a77440        3 minutes ago       1.22MB

注意:

  • 一个镜像有多个标签的话,如何进行删除,需要将这多个镜像都删除才行,类似于系统的硬链接

第四步:基于这个进行启动一个容器,并在容器中运行apache

[root@host1 ~]# docker run --name newhttpd -it zxhk/httpd:latest/ # httpd -f -h /data/html
  • 此时,然后apache能运行,但是每次新启动一个容器,都需手动启动apache,接下来对镜像进行调整,实现启动容器后自动运行apache

第五步:升级镜像实现自动运行内部的apache

先看看我们做的镜像的详细信息

[root@host1 ~]# docker inspect zxhk/httpd:latest

其中有一部分是Cmd,其中就是容器运行起来以后要执行的命令,如下

"Cmd": [      "sh"],

commit创建镜像的时候会可以通过选项来设置这些内容

  • -a:指定作者

  • -c:更改基于镜像启动后执行的命令

  • -m:描述系想你

  • -p:暂停

再重新做个镜像

[root@host1 ~]# docker commit \> -a "zxhk<237745635@qq.com>" \> -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' \> -p img3 zxhk/httpd:v2.0

用这个镜像启动一个容器

[root@host1 ~]# docker run --rm --name test-httpd zxhk/httpd:v2.0

看一下容器中执行的命令

[root@host1 ~]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES243f050288bd        zxhk/httpd:v2.0     "/bin/httpd -f -h /d…"   16 seconds ago      Up 15 seconds                           test-httpd

看一下地址信息

[root@host1 ~]# docker inspect 243 | grep IPAddress            "SecondaryIPAddresses": null,            "IPAddress": "172.17.0.4",                    "IPAddress": "172.17.0.4",

在宿主机访问测试容器中的站点

[root@host1 ~]# curl 172.17.0.4test page[v1.0]

至此,镜像创建完成

三、将制作的镜像上传到docker hub中

1、在https://hub.docker.com/注册用户

  • 需要爬过墙头才能注册,你懂的!!!

  • 注册账户过程-略

2、在docker hub上创建repository和registry

注意:

  • 创建的仓库名称必须要和镜像的名称一致

3、向自己的仓库中上传镜像文件

第一步:登陆docker hub【我的用户名是zxhk】

[root@localhost ~]# docker login -uzxhkPassword: WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded

第二步:上传镜像到hub【此处我们上传httpd镜像的二个版本都传上去】

[root@localhost ~]# docker image lsREPOSITORY               TAG                 IMAGE ID            CREATED             SIZEzxhk/httpd               v2.0                89a647171235        18 hours ago        1.22MBzxhk/httpd               latest              cd7cb2a77440        19 hours ago        1.22MBzxhk/httpd               v1-0                cd7cb2a77440        19 hours ago        1.22MB
[root@localhost ~]# docker push zxhk/httpd:v2.0The push refers to repository [docker.io/zxhk/httpd]f577c88ef366: Pushed eac247cb7af5: Mounted from library/busybox v2.0: digest: sha256:c1c3e604e37652595563b8dc2be877620c77314c925115c7ba35f9969b1a77a0 size: 734
[root@localhost ~]# docker push zxhk/httpd:v1-0

第三步:在docker hub上查看一下


第四步:使用docker hub中我们自己的镜像

在docker hub中已经标识了镜像的使用方法,如下:

为了效果,现将本地的镜像删除

[root@localhost ~]# docker rmi 89 zxhk/httpd:v1-0[root@localhost ~]# docker rmi 89 zxhk/httpd:v2.0
[root@localhost ~]# docker image ls REPOSITORY               TAG                 IMAGE ID            CREATED             SIZEbusybox                  latest              b534869c81f0        2 weeks ago         1.22MB

下载镜像启动容器

[root@localhost ~]# docker pull zxhk/httpd:v2.0[root@localhost ~]# docker image lsREPOSITORY               TAG                 IMAGE ID            CREATED             SIZEzxhk/httpd               v2.0                89a647171235        19 hours ago        1.22MBbusybox                  latest              b534869c81f0        2 weeks ago         1.22MB[root@localhost ~]# docker run --rm --name web1 89a

查看一下容器的信息

[root@localhost ~]# docker ps CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES0ec8687bb487        89a                 "/bin/httpd -f -h /d…"   16 seconds ago      Up 15 seconds                           web1
[root@localhost ~]# docker inspect 0ec | grep "IPAddr"            "SecondaryIPAddresses": null,            "IPAddress": "172.17.0.2",                    "IPAddress": "172.17.0.2",
[root@localhost ~]# curl 172.17.0.2test page[v1.0]

四、将制作的镜像上传到阿里云的镜像仓库中

1、在阿里云注册用户

略

2、进入容器镜像仓库

3、使用阿里云做镜像加速的方法

去docker配置文件中添加一个镜像文件

[root@localhost ~]# vim /etc/docker/daemon.json{    "registry-mirrors": [        "https://registry.docker-cn.com",        "https://mzxx8xy8.mirror.aliyuncs.com"    ]}

重启服务

[root@localhost ~]# systemctl daemon-reload[root@localhost ~]# systemctl restart docker

4、使用阿里云创建仓库

看看镜像仓库的使用方法

5、向阿里云仓库上传镜像

第一步:使用凭证登录阿里云

[root@localhost ~]# sudo docker login --username=zxhk registry.cn-hangzhou.aliyuncs.comPassword: WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded

第二步:上传镜像

  • 上传到阿里云的镜像需要进行先打个标签

[root@localhost ~]# docker tag 89a registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0

第三步:从阿里云拉取镜像

[root@localhost ~]# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zxhk1/httpd:v2.0
镜像 容器 仓库 用户 阿里 制作 时候 标签 方法 名称 命令 多个 文件 运行 信息 系统 应用 地址 就是 最小 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 小米移动互联网科技园 亿人帮互联网科技有限公司 互联网科技工作委员会副主任毛军 科技互联网产品 互联网科技博客 顺网科技2021年互联网排名 科技与互联网的利与弊 昭通云南众福互联网科技有限公司 互联网科技有哪些公司 宁夏科技产业互联网案例 互联网和黑科技的看法 昊天互联网科技有限公司 苏州铭阳互联网科技有限公司 深圳市优智互联网科技有限公司 互联网科技人工智能实验室 互联网企业科技类企业如何做账 深圳无忧互联网科技 济南小医互联网科技有限公司招聘 互联网科技手抄报图片 科技互联网经济论坛 中关村互联网科技集团 互联网科技公司发展 小马过河互联网科技 万达互联网科技 港股科技和互联网 智腾互联网科技有限公司 呼和浩特新华互联网科技学院 四川互联网展示科技馆设计 绵阳真晨互联网科技有限公司 武汉亿视互联网科技有限公司
0