美文网首页
Docker 镜像

Docker 镜像

作者: _于曼丽_ | 来源:发表于2020-01-27 14:57 被阅读0次

    介绍

    镜像是文件系统,例如一个 centos:7 的镜像,提供了一个基本的 centos:7 的发行版,当然此镜像是不包含操作系统 Linux 内核的。

    镜像是分层的文件系统,例如我们在 centos:7 镜像上创建了一个 mysql:5.6 镜像,那么 mysql:5.6 镜像就分为两层。

    最底层的镜像叫做 base 镜像,其他镜像都建立在 base 镜像之上。base 镜像通常是 Linux 发行版的 Docker 镜像,例如 centos 镜像、ubuntu 镜像。base 镜像不包括 Linux 内核、软件、服务、图形桌面等,只包括最基本的命令、工具和程序库,因此 base 镜像的大小比完整的 Linux 系统小很多。base 镜像使用主机系统的内核。

    各层镜像文件都是只读的。从镜像启动一个容器的时候,会在镜像的最上一层创建一个可写层。容器对于文件系统的写操作只会修改可写层,不会修改镜像。

    镜像利用了 linux 环境中的联合文件系统,将不同目录下的文件挂载到同一个虚拟目录下。一个镜像就是一个虚拟目录,该目录下包含了该镜像的所有文件。

    命令

    docker build
    docker search
    docker pull
    docker push
    docker tag
    docker rmi
    docker images
    docker inspect
    

    docker search

    使用 docker search 命令搜索镜像。

    $ docker search centos
    NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    centos                             The official build of CentOS.                   5790                [OK]                
    ansible/centos7-ansible            Ansible on Centos7                              126                                     [OK]
    

    docker pull

    使用 docker pull 命令下载镜像。

    $ docker pull centos
    

    docker images

    使用 docker images 命令查看本地镜像。

    $ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              5ad3bd0e67a9        5 days ago          127MB
    centos              latest              470671670cac        9 days ago          237MB
    

    docker inspect

    使用 docker inspect 命令查看镜像信息,其中通过 Layers 字段反应了镜像的分层信息。

    $ docker inspect nginx
    "Layers": [
       "sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10",
       "sha256:92a699bf44ed64deef63c0f24cdaf143560e9008e6ceb9827e1305306ab6dc27",
       "sha256:667896a5895991a60d2e9fc6847bfc6efa481fae4a0957296f976be0f6bffa72"
    ]
    

    docker tag

    使用 docker tag 命令重命名本地镜像。

    $ docker tag centos yumanli/centos:latest
    $ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              5ad3bd0e67a9        5 days ago          127MB
    centos              latest              470671670cac        9 days ago          237MB
    yumanli/centos      latest              470671670cac        9 days ago          237MB
    

    docker build

    首先创建并编辑一个 Dockerfile 文件。参考 Dockerfile用法详解

    $ vi Dockerfile
    
    FROM nginx
    COPY data /usr/share/nginx/html
    

    在 Dockerfile 文件所在目录使用 docker build 命令构建一个镜像,-t 参数指定了生成的镜像的名字,如果不加 -t 参数,则生成的镜像没有名字。

    $ docker build -t yumanli/nginx ./
    Sending build context to Docker daemon  3.584kB
    Step 1/2 : FROM nginx
     ---> 5ad3bd0e67a9
    Step 2/2 : COPY data /usr/share/nginx/html
     ---> cb185e3e8153
    Successfully built cb185e3e8153
    Successfully tagged yumanli/nginx:latest
    $ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    yumanli/nginx       latest              cb185e3e8153        6 seconds ago       127MB
    nginx               latest              5ad3bd0e67a9        5 days ago          127MB
    

    docker rmi

    使用 docker rmi 命令删除镜像。使用 docker rmi -f 命令删除有容器的镜像。

    $ docker rmi yumanli/nginx
    Untagged: yumanli/nginx:latest
    Deleted: sha256:cb185e3e8153dd176176e4d53890e588ee79184a3febaff35f859648779c9156
    Deleted: sha256:d82b9413cb541a0d3dad837cd737c2704427b7c3ff30f9367d1f7a1871742f53
    

    相关文章

      网友评论

          本文标题:Docker 镜像

          本文链接:https://www.haomeiwen.com/subject/tyqethtx.html