4-Docker命令之docker commit

小黑要上天 2024-07-06 15:07:03 阅读 65

1.docker commit介绍

docker commit命令是用于根据docker容器的改变创建一个新的docker镜像

2.docker commit用法

docker commit [参数] container [repository[:tag]]

<code>[root@centos79 ~]# docker commit --help

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Aliases:

docker container commit, docker commit

Options:

-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")

-c, --change list Apply Dockerfile instruction to the created image

-m, --message string Commit message

-p, --pause Pause container during commit (default true)

[root@centos79 ~]#

3.实例

3.1.基于容器更改创建新镜像

-->进入运行中的docker容器

命令:

docker exec -it centos-ssh /bin/bash

[root@centos79 ~]# docker ps -a | grep centos-ssh

803b6b35366a centos-ssh:1.0 "/bin/sh -c '/usr/sb…" 12 minutes ago Up 12 minutes 0.0.0.0:32768->22/tcp, :::32768->22/tcp centos-ssh

[root@centos79 ~]# docker exec -it centos-ssh /bin/bash

[root@803b6b35366a ansible]#

-->在容器内部使用vim命令

命令:

vim

[root@803b6b35366a ansible]# vim

bash: vim: command not found

[root@803b6b35366a ansible]#

提示没有vim命令

-->docker容器内安装vim命令

命令:

yum install vim -y

[root@803b6b35366a ansible]# yum install vim -y

Loaded plugins: fastestmirror, ovl

http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')

Trying other mirror.

http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')

Trying other mirror.

base | 3.6 kB 00:00:00

extras | 2.9 kB 00:00:00

updates | 2.9 kB 00:00:00

Loading mirror speeds from cached hostfile

Resolving Dependencies

--> Running transaction check

---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed

--> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64

--> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64

--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64

--> Running transaction check

---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed

---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed

--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64

---> Package which.x86_64 0:2.20-7.el7 will be installed

--> Running transaction check

---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================

Package Arch Version Repository Size

=======================================================================================================================================

Installing:

vim-enhanced x86_64 2:7.4.629-8.el7_9 updates 1.1 M

Installing for dependencies:

gpm-libs x86_64 1.20.7-6.el7 base 32 k

vim-common x86_64 2:7.4.629-8.el7_9 updates 5.9 M

vim-filesystem x86_64 2:7.4.629-8.el7_9 updates 11 k

which x86_64 2.20-7.el7 base 41 k

Transaction Summary

=======================================================================================================================================

Install 1 Package (+4 Dependent packages)

Total download size: 7.1 M

Installed size: 23 M

Downloading packages:

(1/5): gpm-libs-1.20.7-6.el7.x86_64.rpm | 32 kB 00:00:08

(2/5): vim-enhanced-7.4.629-8.el7_9.x86_64.rpm | 1.1 MB 00:00:21

(3/5): vim-filesystem-7.4.629-8.el7_9.x86_64.rpm | 11 kB 00:00:03

(4/5): which-2.20-7.el7.x86_64.rpm | 41 kB 00:00:06

vim-common-7.4.629-8.el7_9.x86 FAILED ] 99 B/s | 3.2 MB 11:12:56 ETA

http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: [Errno 12] Timeout on http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')

Trying other mirror.

vim-common-7.4.629-8.el7_9.x86 FAILED

http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: [Errno 12] Timeout on http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')

Trying other mirror.

(5/5): vim-common-7.4.629-8.el7_9.x86_64.rpm | 5.9 MB 00:02:02

---------------------------------------------------------------------------------------------------------------------------------------

Total 33 kB/s | 7.1 MB 00:03:35

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : 2:vim-filesystem-7.4.629-8.el7_9.x86_64 1/5

Installing : 2:vim-common-7.4.629-8.el7_9.x86_64 2/5

Installing : which-2.20-7.el7.x86_64 3/5

install-info: No such file or directory for /usr/share/info/which.info.gz

Installing : gpm-libs-1.20.7-6.el7.x86_64 4/5

Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 5/5

Verifying : gpm-libs-1.20.7-6.el7.x86_64 1/5

Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 2/5

Verifying : which-2.20-7.el7.x86_64 3/5

Verifying : 2:vim-common-7.4.629-8.el7_9.x86_64 4/5

Verifying : 2:vim-filesystem-7.4.629-8.el7_9.x86_64 5/5

Installed:

vim-enhanced.x86_64 2:7.4.629-8.el7_9

Dependency Installed:

gpm-libs.x86_64 0:1.20.7-6.el7 vim-common.x86_64 2:7.4.629-8.el7_9 vim-filesystem.x86_64 2:7.4.629-8.el7_9 which.x86_64 0:2.20-7.el7

Complete!

[root@803b6b35366a ansible]# which vim

/bin/vim

[root@803b6b35366a ansible]#

-->退出docker容器,使用docker commit命令将此已经安装vim命令的docker容器进行commit,以便未来使用

命令:

exit

docker commit centos-ssh centos-vim:1.0

docker images | grep -i centos-vim

[root@803b6b35366a ansible]# exit

exit

[root@centos79 ~]# docker commit centos-ssh centos-vim:1.0

sha256:fc50e118f3914fc6e29c5727b42aaed8bd9589c18fda540bcaae30ee563a906a

[root@centos79 ~]# docker images | grep centos-vim

centos-vim 1.0 fc50e118f391 29 seconds ago 1.01GB

[root@centos79 ~]#

-->基于新的镜像进行验证

命令:

docker run -itd -P --privileged --name centos-vim centos-vim:1.0

docker exec -it db944 /bin/bash

[root@centos79 ~]# docker run -itd -P --privileged --name centos-vim centos-vim:1.0

db944665b1a0596f208b1fa2442bbf9540e6d868a885fb05edc1100f4888aeef

[root@centos79 ~]# docker exec -it db944 /bin/bash

[root@db944665b1a0 ansible]# which vim

/bin/vim

[root@db944665b1a0 ansible]#



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。