镜像的概念

镜像是一个包含程序运行必要依赖环境和代码的只读文件,它采用分层的文件系统,将每一次改变以读写层的形式增加到原来的只读文件上。镜像是容器运行的基石。

下图展示的是Docker镜像的系统结构。其中,镜像的最底层必须是一个称为启动文件系统(bootfs)的镜像,用户不会与这一层直接打交道。bootfs的上层镜像就是我们熟知的根镜像。

image

镜像的本质是磁盘上一系列文件的集合。

image
image

镜像管理

image
image

查看本地所有镜像

[root@bogon ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             5dedbd63518e        2 weeks ago         194MB
  • REPOSITORY:仓库名称。
    • [namespace/centos]:由命名空间和实际的仓库名称组成。当你再Docker Hub上注册一个账户时,账户名自动成为你的命名空间,该命名空间是用来区分Docke Hub上注册的不同用户或者组织的。
    • [centos]:只有仓库名。属于顶级命名空间,只用于官方镜像。
      [dl.dockerpool.com:5000\centos:7]:指定URL路径的方式。适用于自己搭建的Hub或者第三方Hub上获取镜像。
  • TAG:用于区分同一个仓库中的不同镜像。
  • IMAGE ID:镜像的唯一标识:64位HashID。
  • CREATED:镜像 的创建时间。
  • SIZE:镜像所占用的虚拟大小,该大小包含了所有共享文件的大小。

通配符,找到符合条件的一系列镜像

[root@bogon ~]# docker images ce*
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             5dedbd63518e        2 weeks ago         194MB

docker详细信息

docker inspect [NAME]/[CONTAINER ID]

[root@bogon ~]# docker inspect 5dedbd63518e
[
    {
        "Id": "sha256:5dedbd63518eeb45b6c9740d9ea6dee99a4a4c3d0202eac25ebc5fd43809f0a0",
        "RepoTags": [
            "centos:centos6"
        ],
        "RepoDigests": [
            "centos@sha256:4a39ffcea665ed30a7ce6d1418b7d73a2aca05a97706b81b147a231d79e277e1"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2017-09-13T14:43:07.389546878Z",
        "Container": "4e78ba220ff46b22adabc6ddc29f023b2879e170fa42100bcb57cb8024c67117",
        "ContainerConfig": {
            "Hostname": "4e78ba220ff4",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:c0ebbd2d0382b51e2ba9d70eaac0d11f36c1273028605f7ed9b9285acf461187",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "build-date": "20170801",
                "license": "GPLv2",
                "name": "CentOS Base Image",
                "vendor": "CentOS"
            }
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:c0ebbd2d0382b51e2ba9d70eaac0d11f36c1273028605f7ed9b9285acf461187",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "build-date": "20170801",
                "license": "GPLv2",
                "name": "CentOS Base Image",
                "vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 194318701,
        "VirtualSize": 194318701,
        "GraphDriver": {
            "Data": {
                "RootDir": "/var/lib/docker/overlay/bcd6ac9a7fa2e0c6a0e5e414ff97b8819021dbaac1c3648ad135edecb2d44818/root"
            },
            "Name": "overlay"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:80716a2ba414a02bb63de844acff6578a25b8583b01cf0a4c2653da50a31e305"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

在本地寻找镜像/下载/运行

docker run,命令运行时会在本地寻找镜像,找不到的时候就会去Docker Hub上面搜索并下载后运行。

[root@bogon ~]# docker run centos:centos6 uname -a
Linux 15faceed6d66 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

查找镜像

docker search [NAME]:下载之前可以通过search命令查找搜索符合的镜像

[root@bogon ~]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        6934                [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1132                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   450                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as...   227                                     [OK]
#省略......
  • NAME:镜像名称。
  • DESCRIPTION:镜像的简要描述。
  • STARS:用户对镜像的评分。
  • OFFICIAL:是否为官方镜像。
  • AUTOMATED:是否使用了自动构建。

拉取镜像到本地

docker pull [NAME]:可以预先将镜像拉到本地。镜像名必须完整地包含命名空间和仓库名。如果一个仓库中存在多个镜像,还必须制定TAG,否则使用默认TAG:latest。

[root@bogon ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             5dedbd63518e        2 weeks ago         194MB
[root@bogon ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
065132d9f705: Pull complete 
be9835c27852: Pull complete 
f4a0d1212c38: Pull complete 
43be9e9f0fb9: Pull complete 
a1bca8e532ec: Pull complete 
382eae952932: Pull complete 
Digest: sha256:ebb396dc3ac00e8eb4a64c1c022ef41ef16801f31ff98b16916a77fdc7252e67
Status: Downloaded newer image for redis:latest
[root@bogon ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              b6dddb991dfa        9 days ago          107MB
centos              centos6             5dedbd63518e        2 weeks ago         194MB

删除

docker rmi [NAME]/[CONTAINER ID]

对于不需要的镜像,可以使用rmi命令删除。与移除容器的命令rm相比,删除镜像的命令多了一个i,i即image的意思。

  • 删除多个:多个镜像之间使用空格隔开。
  • -f:强制删除,大部分删不掉的情况可能是因为这个镜像被容器依赖了,可以选择先移除容器。
  • docker rm $(docker ps -a -q):如果本地有很多已经停止运行的容器,一个个删除很麻烦,可以使用下面的命令将这些容器一次性删除,这样就能减少无用容器对镜像的依赖。

列出所有容器的ID

docker ps -a -q

从dockerhub获取docker

浏览器打开网页

http://hub.daocloud.io/

image

image
image

从daocluod的docker hub中拉取镜像

docker pull daocloud.io/library/nginx:1.13.2

镜像命令

添加标签

[root@bogon ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
redis                       latest              b6dddb991dfa        9 days ago          107MB
centos                      centos6             5dedbd63518e        2 weeks ago         194MB
daocloud.io/library/nginx   1.13.2              2f7f7bce8929        2 months ago        107MB
[root@bogon ~]# docker tag daocloud.io/library/nginx:1.13.2 nginx:1.13.2
[root@bogon ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
redis                       latest              b6dddb991dfa        9 days ago          107MB
centos                      centos6             5dedbd63518e        2 weeks ago         194MB
daocloud.io/library/nginx   1.13.2              2f7f7bce8929        2 months ago        107MB
nginx                       1.13.2              2f7f7bce8929        2 months ago        107MB

daocloud.io/library/nginx:1.13.2和nginx:1.13.2的存储都是同一个

删除镜像

[root@bogon ~]# docker rmi nginx:1.13.2
Untagged: nginx:1.13.2
[root@bogon ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
redis                       latest              b6dddb991dfa        9 days ago          107MB
centos                      centos6             5dedbd63518e        2 weeks ago         194MB
daocloud.io/library/nginx   1.13.2              2f7f7bce8929        2 months ago        107MB

删除镜像的时候,删除全部tag之后,才会被删除,如果删除的时候使用img_id,会删除镜像;如果被其他镜像依赖,镜像不能被删除

强制删除运行中的容器

docker rm -f CONTAIN_ID

查看镜像的具体信息

docker inspect f3c5dcc79825

运行一个容器

Docker run –dit nginx
  • name ##容器名称
  • link ##连接某个镜像
  • port ##端口映射
  • volum ##持久化存储

镜像的导入导出

docker save -o file.tar image_id

[root@bogon ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
redis                       latest              b6dddb991dfa        9 days ago          107MB
centos                      centos6             5dedbd63518e        2 weeks ago         194MB
daocloud.io/library/nginx   1.13.2              2f7f7bce8929        2 months ago        107MB
[root@bogon ~]# ls centos_late*
centos_latest.tar

导入

[root@bogon ~]# docker load <  centos_latest.tar
32d75bc97c41: Loading layer [==========================================>] 134.6 MB/134.6 MB
87f743c24123: Loading layer [===========================================>] 15.87 kB/15.87 kB
bbe6cef52379: Loading layer [============================================>] 11.78 kB/11.78 kB
3d515508d4eb: Loading layer [==========================================>] 4.608 kB/4.608 kB
5972ebe5b524: Loading layer [==========================================>] 3.072 kB/3.072 kB
Loaded image ID: sha256:104bec311bcdfc882ea08fdd4f5417ecfb1976adea5a0c237e129c728cb7eada

查看导入的image

[root@bogon ~]#  docker load <  centos_latest.tar
80716a2ba414: Loading layer [==================================================>]  202.4MB/202.4MB
Loaded image ID: sha256:5dedbd63518eeb45b6c9740d9ea6dee99a4a4c3d0202eac25ebc5fd43809f0a0
[root@bogon ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
redis                       latest              b6dddb991dfa        9 days ago          107MB
<none>                      <none>              5dedbd63518e        2 weeks ago         194MB
daocloud.io/library/nginx   1.13.2              2f7f7bce8929        2 months ago        107MB

特别注意:导入导出的时候,容易造成层级的都是,出现各种问题,为了安全,不使用这种方式来导入导出

运行导入的镜像

[root@bogon ~]# docker run -dit 5dedbd63518e
174a93960feccdd77f560b014dd6328e15973505a382e629cd3959407b864b47

进入容器

[root@bogon ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
174a93960fec        5dedbd63518e        "/bin/bash"         5 seconds ago       Up 4 seconds                            suspicious_ptolemy
[root@bogon ~]# docker exec -it 174a93960fec /bin/bash
[root@174a93960fec /]# 

[root@174a93960fec /]# echo "hell"> test.txt
[root@174a93960fec /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  test.txt  tmp  usr  var

[root@174a93960fec /]# cat test.txt 
hello

创建一个已经存在的镜像ID然后用commit提交

用commit 提交相关镜像生成一个新镜像:
[root@bogon ~]# docker commit -m “ADD test.txt” -a “test” 174a93960fec test
sha256:b5e4ce650f0d40da24be8387879bbf58078236fdbc0c41bac7121627c9f7dc16

  • -m:表示备注信息
  • -a:作者相关信息。
  • 174a93960fec:就是刚刚我们创建的容器ID
  • test: 表示生成的镜像名称

    [root@bogon ~]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    test latest b5e4ce650f0d 41 seconds ago 194MB
    redis latest b6dddb991dfa 9 days ago 107MB
    daocloud.io/library/nginx 1.13.2 2f7f7bce8929 2 months ago 107MB

    [root@bogon ~]# docker run test cat test.txt
    hello

对镜像的修改,不推荐这种方式,应该使用Dockerfile来操作的

查看镜像的层级

[root@bogon ~]# docker history test
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
b5e4ce650f0d        3 minutes ago       /bin/bash                                       140B                ADD test.txt
5dedbd63518e        2 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  LABEL name=CentOS Base ...   0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop) ADD file:e253dfb8c4027c8...   194MB     

本地文件

[root@bogon ~]# ll cd /var/lib/docker/
ls: 无法访问cd: 没有那个文件或目录
/var/lib/docker/:
总用量 4
drwx------.  2 root root   24 10月  1 19:42 builder
drwx------.  4 root root  150 10月  1 21:36 containers #容器运行相关信息
drwx------.  3 root root   21 10月  1 19:42 image #各层相关信息
drwxr-x---.  3 root root   19 10月  1 19:42 network
drwx------. 17 root root 4096 10月  1 21:36 overlay
drwx------.  4 root root   32 10月  1 19:42 plugins
drwx------.  2 root root    6 10月  1 19:42 swarm
drwx------.  2 root root    6 10月  1 21:23 tmp
drwx------.  2 root root    6 10月  1 19:42 trust
drwx------.  2 root root   25 10月  1 19:42 volumes #数据卷相关信息

centos使用这个来做级联文件系统,ubuntu使用aofs来做级联联合文件系统

[root@bogon ~]# docker inspect test
[
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:80716a2ba414a02bb63de844acff6578a25b8583b01cf0a4c2653da50a31e305",
                "sha256:2d113ac4862223fde011e2bf9604563f13f630e74fdf72f7d27ca6830a07ad5c"
            ]
        },

]

我们通过层级ID可以看到rootfs的相关信息