1 案例1:安装Docker
1.1 问题
本案例要求配置yum源并安装Docker:
准备两台虚拟机,IP为192.168.1.31和192.168.1.32
安装docker
关闭防火墙
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:配置yum源
1)配置IP(虚拟机配置静态ip)docker1和docker2主机同样操作
[root@localhost~]# echo docker1>/etc/hostname
[root@localhost~]# hostname docker1
[root@localhost~]# echo docker2>/etc/hostname
[root@localhost~]# hostname docker2
[root@docker1~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
IPV4_FAILURE_FATAL="no"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.1.31"
PREFIX=24
GATEWAY=192.168.1.254
[root@docker1~]# systemctl restart network
[root@docker2~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
IPV4_FAILURE_FATAL="no"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.1.32"
PREFIX=24
GATEWAY=192.168.1.254
[root@docker1~]# systemctl restart network
2)配置yum客户端,配置之前openstack的扩展源(RHEL7-extras.iso)即可(docker1和docker2主机同样操作)
[root@docker1]#vim /etc/yum.repos.d/rhel.repo
...
[local_extras]
name=CentOS-$releasever-Extras
baseurl="ftp://192.168.1.254/extras"
enabled=1
gpgcheck=0
[root@docker2]#vim /etc/yum.repos.d/rhel.repo
...
[local_extras]
name=CentOS-$releasever-Extras
baseurl="ftp://192.168.1.254/extras"
enabled=1
gpgcheck=0
4)安装docker(docker1和docker2主机同样操作)
[root@docker1~]# yum-y install docker
[root@docker1~]# systemctl restart docker
[root@docker1~]# systemctl enable docker
[root@docker1~]# ifconfig//有docker0说明环境部署完成
docker0:flags=4099<UP,BROADCAST,MULTICAST>mtu1500
inet172.17.0.1netmask255.255.0.0broadcast0.0.0.0
ether02:42:3e:e7:3f:6e txqueuelen0(Ethernet)
RX packets0bytes0(0.0B)
RX errors0dropped0overruns0frame0
TX packets0bytes0(0.0B)
TX errors0dropped0overruns0carrier0collisions0
[root@docker1~]# docker version//查看版本
[root@docker2~]# yum-y install docker
[root@docker2~]# systemctl restart docker
[root@docker2~]# systemctl enable docker
[root@docker2~]# ifconfig//有docker0说明环境部署完成
docker0:flags=4099<UP,BROADCAST,MULTICAST>mtu1500
inet172.17.0.1netmask255.255.0.0broadcast0.0.0.0
ether02:42:53:82:b9:d4 txqueuelen0(Ethernet)
RX packets0bytes0(0.0B)
RX errors0dropped0overruns0frame0
TX packets0bytes0(0.0B)
TX errors0dropped0overruns0carrier0collisions0
[root@docker2~]# docker version//查看版本
2 案例2:镜像基本操作
2.1 问题
本案例要求熟悉镜像的基本操作:
导入镜像
导出镜像
启动镜像
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:docker镜像
1)下载镜像
[root@docker1~]# docker pull docker.io/busybox
[root@docker1 bulid]# docker pull docker.io/busybox
Usingdefaulttag:latest
Trying to pull repository docker.io/library/busybox...
latest:Pulling from docker.io/library/busybox
Digest:sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
2)上传镜像
[root@docker1~]# docker push docker.io/busybox
3)查看镜像
[root@docker1~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest d8233ab899d410days ago1.199MB
4)查找busybox镜像
[root@docker1~]# docker search docker.io/busybox
5)导出busybox镜像为busybox.tar
[root@docker1~]# docker save docker.io/busybox:latest-o busybox.tar
[root@docker1~]# ls
busybox.tar
6)导入镜像
[root@docker1~]# scp nginx.tar root@192.168.1.32:/root
[root@docker2~]# ls
nginx.tar
[root@docker2~]# docker load-i nginx.tar
3c816b4ead84:Loading layer58.47MB/58.47MB
787822cf1b17:Loading layer54.44MB/54.44MB
89decbdf7fb7:Loading layer3.584kB/3.584kB
Loaded image:docker.io/nginx:latest/3.584kB
[root@docker2~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 42b4762643dc4weeks ago109.2M e1ddd7948a1c4weeks ago1.163MB
7)删除镜像
[root@docker1 docker]# docker rmi docker.io/nginx
Untagged:docker.io/nginx:latest
Deleted:sha256:42b4762643dcc9bf492b08064b55fef64942f055f0da91289a8abf93c6d6b43c
Deleted:sha256:e0e55dd2303b3e3ec852acae267d1f8a3eea27a22c64a5829304ecee4d3f559c
Deleted:sha256:4062cf272cdd99e83b1c21f712e5e1359c91ecf92925e56c62133c3324b84e45
Deleted:sha256:3c816b4ead84066ec2cadec2b943993aaacc3fe35fcd77ada3d09dc4f3937313
步骤二:一次性导入多个镜像
[student@room9pc0104]$cd /linux-soft/04/
[student@room9pc0104]$ scp-rdocker/root@192.168.1.31:/root/
[root@docker1~]# cd docker
[root@docker1 docker]# ls
centos.tar nginx.tar redis.tar registry.tar ubuntu.tar
[root@docker1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker1 docker]#foriin*;dodocker load-i $i;done
bcc97fbfc9e1:Loading layer208.2MB/208.2MB
Loaded image:docker.io/centos:latest208.2MB
Loaded image:docker.io/nginx:latest
a098f8909d23:Loading layer338.4kB/338.4kB
c3bc3362d4ff:Loading layer3.034MB/3.034MB
bf10d8534af6:Loading layer36.4MB/36.4MB
0949ce28adcc:Loading layer1.536kB/1.536kB
b1f287cac208:Loading layer3.584kB/3.584kB
Loaded image:docker.io/redis:latest/3.584kB
7bff100f35cb:Loading layer4.672MB/4.672MB
6b6e0aba7201:Loading layer1.587MB/1.587MB
18429e86e6ad:Loading layer20.08MB/20.08MB
7b33b7de3d52:Loading layer3.584kB/3.584kB
726e86b708de:Loading layer2.048kB/2.048kB
Loaded image:docker.io/registry:latest048 kB
adcb570ae9ac:Loading layer89.95MB/89.95MB
7604c8714555:Loading layer15.87kB/15.87kB
9e9d3c3a7458:Loading layer11.26kB/11.26kB
27a216ffe825:Loading layer3.072kB/3.072kB
Loaded image:docker.io/ubuntu:latest3.072kB
[root@docker1 docker]# docker images
导入多个镜像如图-1所示: