练习-1
通过docker pull拉取centos(最新版本)镜像。
(1)通过交互式的方式运行该镜像使成为容器。通过exit退出容器,通过docker ps -a查看容器的状态。
(2)通过交互式的方式运行该镜像使成为容器。再使用Ctrl+p+q不停止容器临时退出。docker ps -a查看容器的状态。与(1)进行比较。
(3)重新返回到正在运行的容器 docker attach 容器的ID
环境准备
docker仓库、通过docker pull拉取centos(最新版本)镜像
[root@master ~]# tar zxvf Docker.tar.gz
[root@master ~]# vi /etc/yum.repos.d/local.repo
[docker]
name=docker
baseurl=file:///root/Docker
gpgcheck=0
enabled=1
[root@master ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: docker
Cleaning up everything
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
docker | 2.9 kB 00:00:00
docker/primary_db | 851 kB 00:00:00
Determining fastest mirrors
repo id repo name status
docker docker 463
repolist: 463
[root@master ~]# yum install docker-ce -y
[root@master ~]# systemctl enable docker;systemctl start docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to/usr/lib/systemd/system/docker.service.
查看镜像列表
[root@master ~]# ./image.sh
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest d3017f59d5e2 4 years ago 165MB
busybox latest 020584afccce 4 years ago 1.22MB
nginx latest 540a289bab6c 4 years ago 126MB
redis alpine 6f63d037b592 4 years ago 29.3MB
python 3.7-alpine b11d2a09763f 4 years ago 98.8MB
<none> <none> 4cda95efb0e4 4 years ago 80.6MB
centos latest 0f3e07c0138f 4 years ago 220MB
registry latest f32a97de94e1 5 years ago 25.8MB
swarm latest ff454b4a0e84 5 years ago 12.7MB
httpd 2.2.32 c51e86ea30d1 6 years ago 171MB
httpd 2.2.31 c8a7fb36e3ab 7 years ago 170MB
(1)
通过交互式的方式运行该镜像使成为容器。通过exit退出容器,通过docker ps -a查看容器的状态。
[root@master ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
[root@master ~]# docker run -itd centos
80c1a5927c8dc14d061ed07b0e8696ed32d86aeec1b4801d8e86e9e0a569cba4
[root@master ~]# docker run -it centos
[root@afb2882e0b78 /]# exit
exit
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37fd685ee8c9 centos "/bin/bash" 11 minutes ago Up 11 minutes priceless_galois
a473ae03988b centos "/bin/bash" 13 minutes ago Up 12 minutes ecstatic_germain
c1103f6e6925 centos "/bin/bash" 17 minutes ago Exited (0) 17 minutes ago silly_snyder
19b5b8eebfd6 centos "/bin/bash" 19 minutes ago Up 18 minutes thirsty_banzai
8dc7a74d5e90 registry:latest "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp registry
(2)
通过交互式的方式运行该镜像使成为容器。再使用Ctrl+p+q不停止容器临时退出。docker ps -a查看容器的状态。
[root@master ~]# docker run -it centos
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37fd685ee8c9 centos "/bin/bash" 26 seconds ago Up 25 seconds priceless_galois
a473ae03988b centos "/bin/bash" 2 minutes ago Up 2 minutes ecstatic_germain
c1103f6e6925 centos "/bin/bash" 6 minutes ago Exited (0) 6 minutes ago silly_snyder
19b5b8eebfd6 centos "/bin/bash" 8 minutes ago Up 8 minutes thirsty_banzai
8dc7a74d5e90 registry:latest "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp registry
(3)
重新返回到正在运行的容器 docker attach 容器的ID
重新返回到正在运行的容器 docker attach 容器ID或名称
例如,如果你的容器ID是 abcdef123456,你可以使用以下命令:
docker attach abcdef123456
[root@master ~]# docker ps #查看容器id
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37fd685ee8c9 centos "/bin/bash" 5 minutes ago Up 5 minutes priceless_galois
a473ae03988b centos "/bin/bash" 7 minutes ago Up 7 minutes ecstatic_germain
19b5b8eebfd6 centos "/bin/bash" 13 minutes ago Up 13 minutes thirsty_banzai
8dc7a74d5e90 registry:latest "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp registry
[root@master ~]# docker attach 37fd685ee8c9
[root@37fd685ee8c9 /]#