码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-05-21 09:53:47      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:anaconda   success   wan   contain   more   oracl   oct   conda   shell   

将容器保存为本地镜像又叫做容器的持久化存储,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 - root
Password:
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 http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2020-05-19 03:00:48-- http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 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.repo
CentOS-Base.repo.original CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
[root@localhost ~]# cat -n /etc/yum.repos.d/CentOS-Base.repo
1 # CentOS-Base.repo
2 #
3 # The mirror system uses the connecting IP address of the client and the
4 # update status of each mirror to pick mirrors that are updated to and
5 # geographically close to the client. You should use this for CentOS updates
6 # 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.com
14 #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
15 baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
16 gpgcheck=1
17 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
18
19 #released updates
20 [updates]
21 name=CentOS-$releasever - Updates - 163.com
22 #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
23 baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
24 gpgcheck=1
25 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
26
27 #additional packages that may be useful
28 [extras]
29 name=CentOS-$releasever - Extras - 163.com
30 #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
31 baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
32 gpgcheck=1
33 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
34
35 #additional packages that extend functionality of existing packages
36 [centosplus]
37 name=CentOS-$releasever - Plus - 163.com
38 baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
39 gpgcheck=1
40 enabled=0
41 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Cleaning up everything
[root@localhost ~]# yum makecache
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
base | 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-release
CentOS Linux release 7.8.2003 (Core)
[root@localhost ~]# hostnamectl status
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: b1625b44a4c448b193b2318086920ba8
Boot ID: c5894807414c44f8b7878f2412d39436
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.el7.x86_64
Architecture: x86-64
[root@localhost ~]# rpm -q centos-release
centos-release-7-8.2003.0.el7.centos.x86_64
[root@localhost ~]# cat /proc/version
Linux 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 -a
Linux 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 update
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No packages marked for update
[root@localhost ~]#
[root@localhost ~]# ls -F
anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]# cd /home/googlebigtable/
[root@localhost googlebigtable]#ls -F
Desktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/
[root@localhost googlebigtable]# mkdir -p temp/dockerworking
[root@localhost googlebigtable]# yum install -y tree
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving 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 https://get.docker.com/ | sh

Executing docker install script, commit: 26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c

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 run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
[root@localhost dockerworking]#
启动docker服务
[root@localhost dockerworking]# yum list installed | grep docker
containerd.io.x86_64 1.2.13-3.2.el7 @docker-ce-stable
docker-ce.x86_64 3:19.03.9-3.el7 @docker-ce-stable
docker-ce-cli.x86_64 1:19.03.9-3.el7 @docker-ce-stable
[root@localhost dockerworking]# systemctl enable docker
Created 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 docker
docker/
[root@localhost dockerworking]# ls -F /etc/docker/
key.json
[root@localhost dockerworking]# ttee /etc/docker/daemon.json <<-‘EOF‘

{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
EOF
{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
[root@localhost dockerworking]# cat -n /etc/docker/daemon.json
1 {
2 registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
3 }
[root@localhost dockerworking]#
[root@localhost dockerworking]# systemctl daemon-reload
[root@localhost dockerworking]#systemctl restart docker
[root@localhost dockerworking]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@localhost dockerworking]#

[root@localhost dockerworking]# docker search centos:7.5.1804
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
portmone/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.1804
7.5.1804: Pulling from library/centos
5ad559c5ae16: Pull complete
Digest: sha256:7a45e4a1efbaafc1d9aa89925b6fdb33288a96d35ea0581412316e2f0ad3720a
Status: Downloaded newer image for centos:7.5.1804
docker.io/library/centos:7.5.1804
[root@localhost dockerworking]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.5.1804 cf49811e3cdb 14 months ago 200MB
[root@localhost dockerworking]#
[root@localhost dockerworking]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.5.1804 cf49811e3cdb 14 months ago 200MB
[root@localhost dockerworking]#

[root@localhost dockerworking]# docker container run -itd --name="centos7.5" cf49811e3cdb
89e35ee3bc0efd495a61d80e2c4eb3a57104ad9a919bca0063dbb89cd2ac9965
[root@localhost dockerworking]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89e35ee3bc0e cf49811e3cdb "/bin/bash" 22 seconds ago Up 21 seconds centos7.5
[root@localhost dockerworking]#
[root@localhost dockerworking]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89e35ee3bc0e 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 openssh
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors

  • base: mirrors.nju.edu.cn
  • extras: mirrors.nju.edu.cn
  • updates: mirrors.nju.edu.cn
    base | 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 sshd
Failed to get D-Bus connection: Operation not permitted
[root@89e35ee3bc0e /]# exit
exit
[root@localhost dockerworking]# docker container stop 89e35ee3bc0e
89e35ee3bc0e
[root@localhost dockerworking]#
[root@localhost dockerworking]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89e35ee3bc0e 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-server
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors

  • base: mirrors.bfsu.edu.cn
  • extras: mirrors.bfsu.edu.cn
  • updates: mirrors.bfsu.edu.cn
    base | 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 daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-05-19 11:08:57 UTC; 8s ago
Docs: 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 vim
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile

  • base: mirrors.tuna.tsinghua.edu.cn
  • extras: mirror.bit.edu.cn
  • updates: mirror.bit.edu.cn
    Resolving Dependencies
    --> Running transaction check
    ........................................................................................................................
    Complete!
    [root@8c6eb8f8d4ba /]# vim /etc/ssh/sshd_config
    [root@8c6eb8f8d4ba /]# passwd root
    Changing password for user root.
    New password:
    BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
    Retype new password:
    passwd: all authentication tokens updated successfully.
    [root@8c6eb8f8d4ba /]#
    [root@8c6eb8f8d4ba /]# exit
    exit
    [root@localhost dockerworking]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    8c6eb8f8d4ba cf49811e3cdb "/usr/sbin/init" 15 minutes ago Up 10 minutes 0.0.0.0:2222->22/tcp ssh
    b2dfd487a2f3 cf49811e3cdb "/usr/sbin/init" 28 minutes ago Exited (137) 18 minutes ago great_taussig
    89e35ee3bc0e 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 <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@localhost dockerworking]# docker commit -a "googlebigtable" -m "ssh" 8c6eb8f8d4ba gbtwithssh:v0
sha256:a371467d9077891273fd8313c78445a10e3dce5c3b01ae8dee71a02dd9b224c3
[root@localhost dockerworking]# ls -F
[root@localhost dockerworking]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gbtwithssh v0 a371467d9077 2 minutes ago 326MB
centos 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 -F
gbtwithsshv0.tar
[root@localhost dockerworking]#

至此,本次实验结束。

孟伯,20200520

交流联系:微信 1807479153 ,QQ 1807479153

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

标签:anaconda   success   wan   contain   more   oracl   oct   conda   shell   

原文地址:https://blog.51cto.com/6286393/2497126

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!