docker镜像(centos7)

1.获取镜像

docker pull NAME[  :TAG]               NAME:镜像仓库的名称,TAG:镜像的标签

例:docker pull centos:latest

如果不显示指定的TAG,则默认选择latest标签,下载仓库中最新的镜像

例:docker pull centos                 直接下载最新版本的镜像centos:latest

利用该镜像创建一个容器,执行打印" hello world "

[root@localhost ~]# docker run -it centos:latest
[root@14ca038cb2f6 /]# echo "hello world"
hello world

2. 查看镜像信息

1. 使用docker images或者docker image ls命令可以列出已有镜像基本信息

例:[root@localhost ~]# docker images
        REPOSITORY                 TAG                      IMAGE ID               CREATED             SIZE
        centos                                latest                    1e1148e4cc2c       3 weeks ago          202MB

        出自哪个仓库                   镜像标签信息      镜像的ID                 创建时间                镜像大小

2. 使用tag命令添加镜像标签

docker tag centos:latest mycentos:7

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos                        latest              1e1148e4cc2c        3 weeks ago         202MB
mycentos                     7                   1e1148e4cc2c        3 weeks ago         202MB

3.使用inspect命令查看详细信息

docker inspect  镜像名

[root@localhost ~]# docker inspect centos:latest
[
    {
        "Id": "sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb",
        "RepoTags": [
            "centos:latest",
            "mycentos:7"
        ],
        "RepoDigests": [
            "centos@sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-12-06T00:21:07.135655444Z",
        "Container": "1fdbb0fcc184eb795364f7aa5fdc00299d0a2b90d8e26b4696217c22da7f983f",
        "ContainerConfig": {
            "Hostname": "1fdbb0fcc184",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,   


使用-f 获取其中一项内容

例如获得ID:

[root@localhost ~]# docker inspect -f {{".Id"}} centos:latest
sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb

4. 使用history命令来查看镜像历史

[root@localhost ~]# docker history centos:latest
IMAGE                      CREATED             CREATED BY                                                              SIZE                COMMENT
1e1148e4cc2c        3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]                            0B                  
<missing>                3 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema.sc…    0B                  
<missing>                3 weeks ago         /bin/sh -c #(nop) ADD file:6f877549795f4798a…   202MB      

3. 删除和清理镜像

使用docker rmi或docker image rm 命令删除镜像

1. 删除镜像使用标签

[root@localhost ~]# docker rmi mycentos:7
Untagged: mycentos:7
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos                         latest              1e1148e4cc2c        3 weeks ago         202MB

只是删除了多个标签里的指定标签,原来的不受影响

2.强制删除使用ID

注:如果docker rmi 镜像ID  不管有几个相同的标签,全部都会被删除

碰到这样的情况

[root@localhost ~]# docker rmi 1e1148e4cc2c
Error response from daemon: conflict: unable to delete 1e1148e4cc2c (must be forced) - image is referenced in multiple repositories

可以加上--force进行强制删除

[root@localhost ~]# docker rmi --force 1e1148e4cc2c
Untagged: centos:latest
Untagged: centos@sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Untagged: mycentos:7
Deleted: sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb

为什么出现这种情况呢,docker ps -a运行后,出现了几个依赖centos:latest镜像的容器

正确的做法是删除依赖该镜像的所有容器,然后再使用docker rmi 镜像的ID

2.1  先查看

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                         PORTS               NAMES
a0523e5575c9        centos:latest       "echo 'hello world'"   12 seconds ago      Exited (0) 11 seconds ago                          heuristic_brown
14ca038cb2f6        centos:latest       "bash"                 About an hour ago   Exited (0) About an hour ago                       youthful_chandrasekhar
665b2a382132        centos:latest       "/bin/bash"            About an hour ago   Exited (0) About an hour ago                       festive_antonelli

2.2 删除依赖
[root@localhost ~]# docker rm 665b2a382132
665b2a382132
[root@localhost ~]# docker rm 14ca038cb2f6 
14ca038cb2f6
[root@localhost ~]# docker rm a0523e5575c9 
a0523e5575c9

2.3 删除镜像
[root@localhost ~]# docker rmi centos:latest
Untagged: centos:latest
Untagged: centos@sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Deleted: sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb
Deleted: sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956

3.清理镜像

使用docker image prune命令进行清理

4. 创建镜像  

基于已有容器创建 docker commit ID NEWNAME

先启动容器记住ID

[root@localhost ~]# docker run -it centos:latest
[root@6453477322d5 /]#

然后基于已有容器创建

[root@localhost ~]# docker commit 6453477322d5 centos:new
sha256:5b0ecb828a266ab0ef8e6e10419fecddde82d9c0ab3c978a47af3c83b9c41ba2

猜你喜欢

转载自blog.csdn.net/uuicon/article/details/85474504