将当前容器保存为本地镜像

网友投稿 262 2022-10-28

将当前容器保存为本地镜像

将容器保存为本地镜像又叫做容器的持久化存储,docker 镜像导入导出有两种方法:一种是使用 save 和 load 命令;一种是使用 export 和 import 命令。二者的区别在于:Save方法,会保存该镜像的所有历史记录Export 方法,不会保留历史记录,即没有commit历史save保存的是镜像(image),export保存的是容器(container);load用来载入镜像包,import用来载入容器包,但两者都会恢复为镜像;load不能对载入的镜像重命名,而 import可以为镜像指定新名称。

本次实验操作的环境:CentOS 7 1804,VMware 15 Pro,4GB内存、4核心CPU,宿主OS为Windows7SP1 64Bit。

操作系统环境准备:[googlebigtable@localhost ~]$ su - rootPassword: Last login: Tue May 19 02:28:07 EDT 2020 on pts/0[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.original[root@localhost ~]# ls -F /etc/yum.repos.d/CentOS-Base.repo.original CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo 03:00:48-- mirrors.163.com (mirrors.163.com)... 59.111.0.251Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 1572 (1.5K) [application/octet-stream]Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[==========================================================================================================>] 1,572 --.-K/s in 0s

2020-05-19 03:00:48 (460 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [1572/1572]

[root@localhost ~]# ls -F /etc/yum.repos.d/CentOS-Base.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repoCentOS-Base.repo.original CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo[root@localhost ~]# cat -n /etc/yum.repos.d/CentOS-Base.repo1 # CentOS-Base.repo2 #3 # The mirror system uses the connecting IP address of the client and the4 # update status of each mirror to pick mirrors that are updated to and5 # geographically close to the client. You should use this for CentOS updates6 # unless you are manually picking other mirrors.7 #8 # If the mirrorlist= does not work for you, as a fall back you can try the 9 # remarked out baseurl= line instead.10 #11 #12 [base]13 name=CentOS-$releasever - Base - 163.com14 #mirrorlist= baseurl= gpgcheck=117 gpgkey= 19 #released updates20 [updates]21 name=CentOS-$releasever - Updates - 163.com22 #mirrorlist= baseurl= gpgcheck=125 gpgkey= 27 #additional packages that may be useful28 [extras]29 name=CentOS-$releasever - Extras - 163.com30 #mirrorlist= baseurl= gpgcheck=133 gpgkey= 35 #additional packages that extend functionality of existing packages36 [centosplus]37 name=CentOS-$releasever - Plus - 163.com38 baseurl= gpgcheck=140 enabled=041 gpgkey=~]# yum clean allLoaded plugins: fastestmirror, langpacksCleaning repos: base extras updatesCleaning up everything[root@localhost ~]# yum makecacheLoaded plugins: fastestmirror, langpacksDetermining fastest mirrorsbase | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/10): base/7/x86_64/group_gz | 153 kB 00:00:00 (2/10): base/7/x86_64/primary_db | 6.1 MB 00:00:04 .................................................................................................................................................urw-fonts.noarch 0:2.4-16.el7

Complete![root@localhost ~]#[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.8.2003 (Core)[root@localhost ~]# hostnamectl statusStatic hostname: localhost.localdomainIcon name: computer-vmChassis: vmMachine ID: b1625b44a4c448b193b2318086920ba8Boot ID: c5894807414c44f8b7878f2412d39436Virtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-862.el7.x86_64Architecture: x86-64[root@localhost ~]# rpm -q centos-releasecentos-release-7-8.2003.0.el7.centos.x86_64[root@localhost ~]# cat /proc/versionLinux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018[root@localhost ~]# uname -aLinux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux[root@localhost ~]#[root@localhost ~]# yum -y updateLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfileNo packages marked for update[root@localhost ~]#[root@localhost ~]# ls -Fanaconda-ks.cfg initial-setup-ks.cfg[root@localhost ~]# cd /home/googlebigtable/[root@localhost googlebigtable]#ls -FDesktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/[root@localhost googlebigtable]# mkdir -p temp/dockerworking[root@localhost googlebigtable]# yum install -y treeLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfileResolving Dependencies--> Running transaction check..................................................................................................................................................Installed:tree.x86_64 0:1.6.0-10.el7

Complete![root@localhost googlebigtable]# tree temp/temp/└── dockerworking

1 directory, 0 files[root@localhost googlebigtable]#

用shell脚本安装Docker[root@localhost dockerworking]# curl -sSL | sh

Executing docker install script, commit: 26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c

sh -c 'yum install -y -q yum-utils'Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version sh -c 'yum-config-manager --add-repo plugins: fastestmirror, langpacksadding repo from: file to /etc/yum.repos.d/docker-ce.reporepo saved to /etc/yum.repos.d/docker-ce.repo '[' stable '!=' stable ']' sh -c 'yum makecache'Loaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfilebase | 3.6 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:05 (2/4): docker-ce-stable/x86_64/primary_db | 42 kB 00:00:01 (3/4): docker-ce-stable/x86_64/other_db | 114 kB 00:00:01 (4/4): docker-ce-stable/x86_64/filelists_db | 20 kB 00:00:16 Metadata Cache Created '[' -n '' ']' sh -c 'yum install -y -q docker-ce'[Errno 12] Timeout on (28, 'Operation timed out after 30002 milliseconds with 0 out of 0 bytes received')Trying other mirror.[Errno 12] Timeout on (28, 'Operation timed out after 30001 milliseconds with 0 out of 0 bytes received')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.2.13-3.2.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEYPublic key for containerd.io-1.2.13-3.2.el7.x86_64.rpm is not installedImporting GPG key 0x621E9F35:Userid : "Docker Release (CE rpm) "Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35From : you would like to use Docker as a non-root user, you should now consideradding your user to the "docker" group with something like: sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to runcontainers which can be used to obtain root privileges on thedocker host.Refer to more information.[root@localhost dockerworking]# 启动docker服务[root@localhost dockerworking]# yum list installed | grep dockercontainerd.io.x86_64 1.2.13-3.2.el7 @docker-ce-stabledocker-ce.x86_64 3:19.03.9-3.el7 @docker-ce-stabledocker-ce-cli.x86_64 1:19.03.9-3.el7 @docker-ce-stable[root@localhost dockerworking]# systemctl enable dockerCreated symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.[root@localhost dockerworking]# systemctl start docker[root@localhost dockerworking]# [root@localhost dockerworking]# ls -F /etc/ | grep dockerdocker/[root@localhost dockerworking]# ls -F /etc/docker/key.json[root@localhost dockerworking]# ttee /etc/docker/daemon.json <<-'EOF'

{"registry-mirrors": ["["dockerworking]# cat -n /etc/docker/daemon.json1 {2 registry-mirrors": [" }[root@localhost dockerworking]#[root@localhost dockerworking]# systemctl daemon-reload[root@localhost dockerworking]#systemctl restart docker[root@localhost dockerworking]# service docker restartRedirecting to /bin/systemctl restart docker.service[root@localhost dockerworking]#

[root@localhost dockerworking]# docker search centos:7.5.1804NAME DESCRIPTION STARS OFFICIAL AUTOMATEDportmone/centos_php7 Centos:7.5.1804, php-7.2.14, oracle-instantc… 0 jellywen/centos75-php7124-nginx114 centos:7.5.1804 php:7.1.24 nginx:1.14.1 swoo… 0 insgeek/centos centos:7.5.1804 0 makayel/supervisor Docker image based on centos:7.5.1804. It i… 0 [OK]jellywen/centos75-php72-nginx114 centos:7.5.1804 php:7.2.12 nginx:1.14.1 swoo… 0 jujumilk4/tpgcentos CentOS:7.5.1804 + PHP 7.2.11 + Apache 2.4.6 0 szf8/c7-systemd Systemd integrated < centos:7.5.1804 0 kingchdu/centos_tomcat centos:7.5.1804, jdk:1.8.0_181, tomcat:8.5.34 0 7inaydas/nwclient Dockerized version of Networker 18.2 Client … 0 [root@localhost dockerworking]# docker pull centos:7.5.18047.5.1804: Pulling from library/centos5ad559c5ae16: Pull complete Digest: sha256:7a45e4a1efbaafc1d9aa89925b6fdb33288a96d35ea0581412316e2f0ad3720aStatus: Downloaded newer image for centos:7.5.1804docker.io/library/centos:7.5.1804[root@localhost dockerworking]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEcentos 7.5.1804 cf49811e3cdb 14 months ago 200MB[root@localhost dockerworking]#[root@localhost dockerworking]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos 7.5.1804 cf49811e3cdb 14 months ago 200MB[root@localhost dockerworking]#

[root@localhost dockerworking]# docker container run -itd --name="centos7.5" cf49811e3cdb89e35ee3bc0efd495a61d80e2c4eb3a57104ad9a919bca0063dbb89cd2ac9965[root@localhost dockerworking]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES89e35ee3bc0e cf49811e3cdb "/bin/bash" 22 seconds ago Up 21 seconds centos7.5[root@localhost dockerworking]#[root@localhost dockerworking]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES89e35ee3bc0e cf49811e3cdb "/bin/bash" 22 seconds ago Up 21 seconds centos7.5[root@localhost dockerworking]# docker container exec -it 89e35ee3bc0e /bin/bash[root@89e35ee3bc0e /]# yum install -y opensshLoaded plugins: fastestmirror, ovlDetermining fastest mirrors

base: mirrors.nju.edu.cn extras: mirrors.nju.edu.cn updates: mirrors.nju.edu.cnbase | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): updates/7/x86_64/primary_db .........................................................................................................................................Installed:openssh.x86_64 0:7.4p1-21.el7

Dependency Installed:fipscheck.x86_64 0:1.4.1-6.el7 fipscheck-lib.x86_64 0:1.4.1-6.el7

Complete![root@89e35ee3bc0e /]#[root@89e35ee3bc0e /]# systemctl start sshdFailed to get D-Bus connection: Operation not permitted[root@89e35ee3bc0e /]# exitexit[root@localhost dockerworking]# docker container stop 89e35ee3bc0e89e35ee3bc0e[root@localhost dockerworking]#[root@localhost dockerworking]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES89e35ee3bc0e cf49811e3cdb "/bin/bash" a minute ago Up 3 minutes centos7.5[root@localhost dockerworking]# docker container run -itd --privileged cf49811e3cdb /usr/sbin/init[root@localhost dockerworking]# docker container exec -it b2dfd487a2f3 /bin/bash[root@b2dfd487a2f3 /]# yum install -y openssh-serverLoaded plugins: fastestmirror, ovlDetermining fastest mirrors

base: mirrors.bfsu.edu.cn extras: mirrors.bfsu.edu.cn updates: mirrors.bfsu.edu.cnbase | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): base/7/x86_64/group_gz.....................................................................................................................................................Installed:openssh-server.x86_64 0:7.4p1-21.el7

Dependency Installed:fipscheck.x86_64 0:1.4.1-6.el7 fipscheck-lib.x86_64 0:1.4.1-6.el7 openssh.x86_64 0:7.4p1-21.el7 tcp_wrappers-libs.x86_64 0:7.6-77.el7

Complete![root@b2dfd487a2f3 /]#[root@8c6eb8f8d4ba /]# systemctl start sshd[root@8c6eb8f8d4ba /]# systemctl status sshd● sshd.service - OpenSSH server daemonLoaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)Active: active (running) since Tue 2020-05-19 11:08:57 UTC; 8s agoDocs: man:sshd(8)man:sshd_config(5)Main PID: 187 (sshd)CGroup: /docker/8c6eb8f8d4ba9c9e6a4a232025a90b7a863858a7ca64df7586b4bf2bf5b752c9/system.slice/sshd.service└─187 /usr/sbin/sshd -D‣ 187 /usr/sbin/sshd -D

May 19 11:08:57 8c6eb8f8d4ba systemd[1]: Starting OpenSSH server daemon...May 19 11:08:57 8c6eb8f8d4ba sshd[187]: Server listening on 0.0.0.0 port 22.May 19 11:08:57 8c6eb8f8d4ba sshd[187]: Server listening on :: port 22.May 19 11:08:57 8c6eb8f8d4ba systemd[1]: Started OpenSSH server daemon.[root@8c6eb8f8d4ba /]#[root@8c6eb8f8d4ba /]# yum install -y vimLoaded plugins: fastestmirror, ovlLoading mirror speeds from cached hostfile

base: mirrors.tuna.tsinghua.edu.cn extras: mirror.bit.edu.cn updates: mirror.bit.edu.cnResolving Dependencies--> Running transaction check........................................................................................................................Complete![root@8c6eb8f8d4ba /]# vim /etc/ssh/sshd_config[root@8c6eb8f8d4ba /]# passwd rootChanging password for user root.New password:BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary wordRetype new password: passwd: all authentication tokens updated successfully.[root@8c6eb8f8d4ba /]#[root@8c6eb8f8d4ba /]# exitexit[root@localhost dockerworking]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8c6eb8f8d4ba cf49811e3cdb "/usr/sbin/init" 15 minutes ago Up 10 minutes 0.0.0.0:2222->22/tcp sshb2dfd487a2f3 cf49811e3cdb "/usr/sbin/init" 28 minutes ago Exited (137) 18 minutes ago great_taussig89e35ee3bc0e cf49811e3cdb "/bin/bash" 37 minutes ago Exited (137) 30 minutes ago centos7.5[root@localhost dockerworking]# docker commit --help

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

Create a new image from a container's changes

Options:-a, --author string Author (e.g., "John Hannibal Smith ")-c, --change list Apply Dockerfile instruction to the created image-m, --message string Commit message-p, --pause Pause container during commit (default true)[root@localhost dockerworking]# docker commit -a "googlebigtable" -m "ssh" 8c6eb8f8d4ba gbtwithssh:v0sha256:a371467d9077891273fd8313c78445a10e3dce5c3b01ae8dee71a02dd9b224c3[root@localhost dockerworking]# ls -F[root@localhost dockerworking]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEgbtwithssh v0 a371467d9077 2 minutes ago 326MBcentos 7.5.1804 cf49811e3cdb 14 months ago 200MB[root@localhost dockerworking]# ls -F /var/lib/docker/builder/ buildkit/ containers/ image/ network/ overlay2/ plugins/ runtimes/ swarm/ tmp/ trust/ volumes/[root@localhost dockerworking]# ls -F /var/lib/docker/image/overlay2/[root@localhost dockerworking]# docker save --help

Usage: docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:-o, --output string Write to a file, instead of STDOUT[root@localhost dockerworking]# docker save -o gbtwithsshv0.tar gbtwithssh:v0[root@localhost dockerworking]# ls -Fgbtwithsshv0.tar[root@localhost dockerworking]#

至此,本次实验结束。

孟伯,20200520

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:kubernetes入门到实战(二)kubeadm部署kubernetes集群
下一篇:SpringBoot自定义maven
相关文章

 发表评论

暂时没有评论,来抢沙发吧~