一、docker安装
1、下载压缩包,上传,然后解压到/opt/下,配置源
上传
[root@localhost ~]# ll
total 5325352
-rw-------. 1 root root 1390 Nov 6 16:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 4329570304 Jun 7 2019 CentOS-7-x86_64-DVD-1511.iso
-rw-r--r--. 1 root root 1123583789 Dec 6 2019 Docker.tar.gz
[root@localhost ~]# tar -zxvf Docker.tar.gz -C /opt/
配置yum源,命令如下
[root@localhost ~]#mv /etc/yum.repos.d/* /media/
[root@localhost ~]#mkdir /opt/centos
[root@localhost ~]#mount -o loop CentOS-7-x86_64-DVD-1511.iso /opt/centos
[root@localhost ~]#vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[docker]
name=docker
baseurl=file:///opt/Docker
gpgcheck=0
enabled=1
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
centos | 3.6 kB 00:00
docker | 2.9 kB 00:00
(1/3): centos/group_gz | 155 kB 00:00
(2/3): centos/primary_db | 2.8 MB 00:00
(3/3): docker/primary_db | 851 kB 00:00
Determining fastest mirrors
repo id repo name status
centos centos 3,723
docker docker 463
repolist: 4,186
2、然后安装docker
[root@localhost ~]# yum install -y docker-ce
3、开启docker服务
[root@localhost ~]# systemctl start docker
4、可查看docker的基本信息
[root@localhost ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 1
Server Version: 19.03.14
Storage Driver: devicemapper
Pool Name: docker-253:0-33933345-pool
Pool Blocksize: 65.54kB
Base Device Size: 10.74GB
Backing Filesystem: xfs
Udev Sync Supported: true
Data file: /dev/loop0
Metadata file: /dev/loop1
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 259.3MB
Data Space Total: 107.4GB
Data Space Available: 17.09GB
Metadata Space Used: 692.2kB
Metadata Space Total: 2.147GB
Metadata Space Available: 2.147GB
Thin Pool Minimum Free Space: 10.74GB
Deferred Removal Enabled: true
Deferred Deletion Enabled: true
Deferred Deleted Device Count: 0
Library Version: 1.02.107-RHEL7 (2015-10-14)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: ea765aba0d05254012b0b9e595e995c09186427f
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.782GiB
Name: localhost.localdomain
ID: AQPC:NQLF:67LI:TJZK:DCCI:LIT2:CMM2:LMKX:RK4H:F5EK:DLRM:J3PL
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://dhq9bx4f.mirror.aliyuncs.com/
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
WARNING: the devicemapper storage-driver is deprecated, and will be removed in a future release.
WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
二、镜像管理
1、拉取镜像
1)拉取镜像命令
[root@localhost ~]# docker pull centos
2)配置加速器(拉取镜像时,可能会很慢,配置一下加速器可能就比较快一些)(文件是自己新建的,内容也要自己写上去)
[root@localhost ~]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://dhq9bx4f.mirror.aliyuncs.com"]
}
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/docker/daemon.json" [New] 3L, 64C written
3)配置完后要重启docker
[root@localhost ~]# systemctl restart docker
4)然后在重新拉取镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
5)查看本地镜像(centos就是刚刚拉取的镜像)
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 2 months ago 215MB
6)搜索镜像,查看有关docker的文件
后面直接是镜像ID
[root@localhost ~]# docker search 0d120b6ccaa8
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
0d120b6ccaa8/sravan123 0
或者后面是镜像名称
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6300 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 123 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 116 [OK]
centos/systemd systemd enabled base container. 87 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 46
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
guyton/centos6 From official centos6 container with full up… 10 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
drecom/centos-ruby centos ruby 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
2、给镜像打标签
1)首先查看一下本地镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 2 months ago 215MB
2)给镜像打标签
语句格式:docker tag 要打标签的镜像ID 取名
[root@localhost ~]# docker tag 0d120b6ccaa8 haha
3)在查看,发现多了一个镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 2 months ago 215MB
haha latest 0d120b6ccaa8 2 months ago 215MB
3、删除镜像
1)查看之前有的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 2 months ago 215MB
haha latest 0d120b6ccaa8 2 months ago 215MB
2)删除镜像
[root@localhost ~]# docker rmi haha
Untagged: haha:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 2 months ago 215MB
3)再次查看
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0d120b6ccaa8 2 months ago 215MB
4、把镜像启动为容器
1)启动命令
-i 表示让容器的标准输入打开, -t 表示分配一个伪终端,-d 表示后台启动 ,要把这几个镜像放在镜像名字前面
[root@localhost ~]# docker run -itd centos
0a642554da1ec4bcc9d254fc84a50b985bb61797ca9d5d626e61fce70dc32926
2)查看运行镜像运行状态
-a 查看所有的容器包括未运行的
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a642554da1e centos "/bin/bash" 12 seconds ago Up 11 seconds sad_jang
5、删除容器
[root@localhost ~]# docker rm -f dce4a76ff7f6
dce4a76ff7f6
三、通过容器建立镜像
1、进入容器
[root@localhost ~]# docker exec -it 55ef067ca20a /bin/bash
[root@55ef067ca20a /]#
2、对容器做一些改动
1)进入容器
[root@localhost ~]# docker exec -it 617ef442a6c5 /bin/bash
2)看在容器内是否能联网·
[root@617ef442a6c5 /]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=127 time=41.1 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=127 time=27.6 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=3 ttl=127 time=27.3 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6ms
rtt min/avg/max/mdev = 27.289/31.995/41.103/6.441 ms
3)在容器内安装查看端口命令
[root@617ef442a6c5 /]# yum install net-tools -y
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream 1.0 MB/s | 5.8 MB 00:05
CentOS-8 - Base 305 kB/s | 2.2 MB 00:07
CentOS-8 - Extras 451 B/s | 8.6 kB 00:19
Dependencies resolved.
=============================================================
Package Arch Version Repo Size
=============================================================
Installing:
net-tools x86_64 2.0-0.51.20160912git.el8 BaseOS 323 k
Transaction Summary
=============================================================
Install 1 Package
Total download size: 323 k
Installed size: 1.0 M
Downloading Packages:
net-tools-2.0-0.51.20160912g 641 kB/s | 323 kB 00:00
-------------------------------------------------------------
Total 229 kB/s | 323 kB 00:01
warning: /var/cache/dnf/BaseOS-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - Base 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.51.20160912git.el 1/1
Running scriptlet: net-tools-2.0-0.51.20160912git.el 1/1
Verifying : net-tools-2.0-0.51.20160912git.el 1/1
Installed:
net-tools-2.0-0.51.20160912git.el8.x86_64
Complete!
4)退出时要按两下ctrl+D
[root@617ef442a6c5 /]# exit
[root@localhost ~]# logout
5)将容器打包成镜像
-m 加一些改动信息,-a指定作者相关信息 2c74d这一串为容器id,再后面为新镜像的名字
[root@localhost ~]# docker commit -m "install net-tools" -a "haha" 617ef442a6c5 centos
sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
6)查看镜像列表,第一个centos即使刚刚打包出的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 8fce8ec646ba 52 seconds ago 246 MB
centos <none> 0d120b6ccaa8 2 months ago 215 MB
7)运行镜像看是否可以使用刚刚安装的命令
[root@localhost ~]# docker run -it centos #参数d 是在后台运行,不加这个参数则直接进入容器
[root@fd1898dc929b /]# netstat -ntpl #查看端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
四、使用模板创建镜像
1)在电脑浏览器下载tar包,上传到CRT里
下载模板地址
https://download.openvz.org/template/precreated/
2)将tar包导成镜像
[root@localhost ~]# cat centos-7-x86_64-minimal\ \(1\).tar.gz | docker import - centos7
sha256:47fbc555bdd460e01aa411c90b92d6bcab44ddf3963b09c6c03d7221797b98e7
3)查看镜像列表
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 47fbc555bdd4 32 seconds ago 435 MB
centos latest 8fce8ec646ba 57 minutes ago 246 MB
centos <none> 0d120b6ccaa8 2 months ago 215 MB
4)把现有的镜像导出为一个文件
[root@localhost ~]# docker save -o haha.tar centos
5)再把文件恢复为镜像
[root@localhost ~]# docker load --input haha.tar
Loaded image: centos:latest
Loaded image ID: sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
Loaded image ID: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
6)文件导成镜像
[root@localhost ~]# docker load < haha.tar
Loaded image: centos:latest
Loaded image ID: sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
Loaded image ID: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
7)对5、6条把文件导回镜像的理解(把镜像打包成文件后,再把原有的镜像删除后,会更直观的看出来)
[root@localhost ~]# docker rm -f $(docker ps -qa) # 删除所有的容器
fd1898dc929b
617ef442a6c5
[root@localhost ~]# docker ps -a # 查看容器是否删除
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images # 看有哪些镜像,
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 47fbc555bdd4 59 minutes ago 435 MB
centos latest 8fce8ec646ba About an hour ago 246 MB
centos <none> 0d120b6ccaa8 2 months ago 215 MB
[root@localhost ~]# docker rmi 47fbc555bdd4 8fce8ec646ba 0d120b6ccaa8 # 全部删除
Untagged: centos7:latest
Deleted: sha256:47fbc555bdd460e01aa411c90b92d6bcab44ddf3963b09c6c03d7221797b98e7
Deleted: sha256:788edba9eaa8ade63d8ba9d5747281c5da2b34b12a6c80f4dffd8ad9e05f68c1
Untagged: centos:latest
Deleted: sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
Deleted: sha256:7a59197fafc830d74f53c2a4b6489364a2a66a7e84733a3ff53077ce9028ebe9
Untagged: centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Deleted: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
Deleted: sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726
[root@localhost ~]# docker images # 再次查看
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# ll # haha.tar 是我刚刚把镜像导成的的文件
total 656872
-rw-------. 1 root root 1390 Nov 6 16:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 145639219 Dec 3 2020 centos-7-x86_64-minimal (1).tar.gz
-rw-r--r--. 1 root root 19521288 Dec 2 2020 docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root 29108 Dec 2 2020 docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
-rw-------. 1 root root 253716992 Nov 6 17:54 haha
-rw-------. 1 root root 253716992 Nov 6 17:54 haha.tar
[root@localhost ~]# docker load --input haha.tar # 将文件转成镜像
291f6e44771a: Loading layer 222.4 MB/222.4 MB
743375fb01df: Loading layer 31.34 MB/31.34 MB
Loaded image: centos:latest
Loaded image ID: sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
Loaded image ID: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 8fce8ec646ba 2 hours ago 246 MB
[root@localhost ~]# docker rmi centos
Untagged: centos:latest
Deleted: sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
Deleted: sha256:7a59197fafc830d74f53c2a4b6489364a2a66a7e84733a3ff53077ce9028ebe9
Deleted: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
Deleted: sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker load < haha.tar # 将文件转成镜像
291f6e44771a: Loading layer 222.4 MB/222.4 MB
743375fb01df: Loading layer 31.34 MB/31.34 MB
Loaded image: centos:latest
Loaded image ID: sha256:8fce8ec646ba8c1093aadcfd9c7cf95140604d906a7d436fbdf80e6b3dc2ff31
Loaded image ID: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 8fce8ec646ba 2 hours ago 246 MB
8) docker push image_name //可以把自己的镜像传到dockerhub官方网站上去,但前提是需要先注册一个用户,后续如果有需求再研究吧
五、docker容器管理
1、
1)创建容器
[root@localhost ~]# docker create -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
b78b30668903632cbe46d1322830510c2c1a669f90cdbb2529d1f8470545c155
2)开启容器
[root@localhost ~]# docker start eef34aa66c0d
eef34aa66c0d
3)进入容器
[root@localhost ~]# docker exec -it eef34aa66c0d /bin/bash
[root@eef34aa66c0d /]# exit
[root@localhost ~]# logout
4)直接运行镜像为容器,会在创建一个容器
[root@localhost ~]# docker run -it centos
[root@6762db6582a3 /]# exit
[root@localhost ~]# logout
Last login: Fri Nov 6 19:33:47 2020 from 192.168.200.1
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6762db6582a3 centos "/bin/bash" 18 seconds ago Exited (0) 6 seconds ago serene_joliot
b78b30668903 centos "/bin/bash" About a minute ago Created nervous_lumiere
5) 让容器在后台运行
[root@localhost ~]# docker run -d centos
dcdacdf81615a621283607c1d92b2a17e691f37cb1e49e1c5d0a842af4ab2031
[root@localhost ~]# docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"
9d6655e41549f22619cb38b4535bf2773a6393bba632d36659385220db64f535
6)给容器自定义名字(运行镜像(centos)为容器,然后自定义这个容器的名字为haha)
[root@localhost ~]# docker run --name haha centos bash
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1a7facbbc3b centos "bash" 9 seconds ago Exited (0) 8 seconds ago haha
7)让容器退出后自动删除
–rm
[root@localhost ~]# docker rm f $(docker ps -qa) #删除之前所有的容器
f
f355b0a3a5f7
9bfab68bd876
b4a138fe3e6a
d1a7facbbc3b
[root@localhost ~]# docker ps -a # 查看一下
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run --rm -it centos bash #建立一个退出以后就删除的容器
[root@e67d6fc08d3a /]# exit #退出
[root@localhost ~]# logout #再次退出
Last login: Fri Nov 6 22:26:01 2020 from 192.168.200.1
[root@localhost ~]#
[root@localhost ~]# docker ps -a # 没有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8)退出容器后 ,它就停止运行了
2、
1)获取容器运行历史信息. docker logs container_id
[root@localhost ~]# docker run -it centos bash
[root@637ef5e8c9f5 /]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=127 time=34.2 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=127 time=53.9 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 34.219/44.043/53.868/9.826 ms
[root@637ef5e8c9f5 /]# exit
[root@localhost ~]# logout
[root@localhost ~]# docker logs 637ef5e8c9f5 # 获取容器运行历史信息
[root@637ef5e8c9f5 /]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=127 time=34.2 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=127 time=53.9 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 34.219/44.043/53.868/9.826 ms
[root@637ef5e8c9f5 /]# exit
[root@localhost ~]#
2)进入一个后台运行的容器
docker attach container_id//但是attach命令不算好用,比如我们想要退出终端,就得exit了,这样容器也就退出了,还有一种方法
[root@localhost ~]# docker logs 2615e9de062e
3)docker exec -it container_id bash //可以临时打开一个虚拟终端,并且exit后,容器依然运行着,打开的容器必须是在运行的
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
637ef5e8c9f5 centos "bash" 7 minutes ago Exited (0) 7 minutes ago condescending_easley
[root@localhost ~]# docker exec -it 637ef5e8c9f5 bash
Error response from daemon: Container 637ef5e8c9f5f722b04dfc9164a65a1ed40e55948de68839461f248739df0a02 is not running
[root@localhost ~]# docker start 637ef5e8c9f5
637ef5e8c9f5
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
637ef5e8c9f5 centos "bash" 8 minutes ago Up 8 seconds condescending_easley
[root@localhost ~]# docker exec -it 637ef5e8c9f5 bash
[root@637ef5e8c9f5 /]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
637ef5e8c9f5 centos "bash" 9 minutes ago Up 48 seconds condescending_easley
4)docker export container_id > file.tar // 导出容器,可以迁移到其他机器上,需要导入
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
637ef5e8c9f5 centos "bash" 13 minutes ago Up 4 minutes condescending_easley
[root@localhost ~]# docker export 637ef5e8c9f5 > haha.tar
[root@localhost ~]# ll
total 626248
-rw-------. 1 root root 1390 Nov 6 16:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 145639219 Dec 3 2020 centos-7-x86_64-minimal (1).tar.gz
-rw-r--r--. 1 root root 19521288 Dec 2 2020 docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root 29108 Dec 2 2020 docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
-rw-------. 1 root root 253716992 Nov 6 17:54 haha
-rw-------. 1 root root 222357504 Nov 6 23:23 haha.tar
将tar包分享给80节点
[root@localhost ~]# scp haha.tar root@192.168.200.80:/root
The authenticity of host '192.168.200.80 (192.168.200.80)' can't be established.
ECDSA key fingerprint is d8:9b:62:32:21:67:36:9c:5a:bb:4e:32:29:e4:07:bb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.200.80' (ECDSA) to the list of known hosts.
root@192.168.200.80's password:
haha.tar 100% 212MB 30.3MB/s 00:07
80节点也要安装docker,安装后要启动,即可继续下面的操作
5) cat file.tar |docker import - aming_test //这样会生成aming_test的镜像
[root@xserver1 ~]# cat haha.tar | docker import - centos
sha256:fa56f876dcce5e2c7f7be7c9eb7da76e5494f0c16e4aca1fd07bac7371984c0c
[root@xserver1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest fa56f876dcce 28 seconds ago 215 MB