docker系统修改后保存为新镜像的方法

环境:Fedora release 32 (Thirty Two)

方法一:通过export命令,用export导出后,再创建新的镜像。
当修改完系统的配置后,查看目前的容器名或id,并执行如下命令,生成新的镜像。

[root@Centos7 ~]# docker export 120f93e71bb1 >  myfedora.tar
[root@Centos7 ~]# cat myfedora.tar |docker import - mynewfedora:latest
sha256:349fbf8ea74700ce6fd0cc3f903ddcae7626cc5e3aeb9f89d415985fca6d8127
[root@Centos7 ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mynewfedora           latest              349fbf8ea747        8 seconds ago       403MB

方法二:通过commit命令,生成新的容器镜像。

[root@Centos7 ~]# docker commit 120f93e71bb1 mynewfedora2
sha256:68ea6f3e4e0b8d0abedaba056d27944687d7cae842850111dffe3bddf908dba3
[root@Centos7 ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mynewfedora2          latest              68ea6f3e4e0b        7 seconds ago       414MB
mynewfedora           latest              349fbf8ea747        3 minutes ago       403MB

TIPS:在通过第一种方法创建的镜像运行容器时,有一个如下的报错:

[root@Centos7 ~]# docker run -it mynewfedora
docker: Error response from daemon: No command specified.
See 'docker run --help'.

通过比较刚才的另一个镜像mynewfedora2或者官网的fedora镜像,发现这种方法生成的镜像里面,Cmd中的参数为null,缺少其它镜像的/bin/bash参数:

[root@Centos7 ~]# docker inspect mynewfedora|grep -A 5 Cmd
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
--
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,

对比:

[root@Centos7 ~]# docker inspect mynewfedora2|grep -A 5 Cmd
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "fedora",
            "Volumes": null,
--
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "fedora",
            "Volumes": null,

所以用这个镜像运行容器的时候,需要在命令中加上/bin/bash:

[root@Centos7 ~]# docker run -it mynewfedora /bin/bash
[root@515b2b933bb4 /]# 

参考文档:
https://blog.csdn.net/shenghuiping2001/article/details/93378185

猜你喜欢

转载自blog.csdn.net/szuwangjl/article/details/107735905