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所示:
图-1
步骤三:启动镜像
1)启动centos镜像生成一个容器
启动镜像时若不知道后面的命令加什么:
1、可以猜(如:/bin/bash、/bin/sh)
2、可以不加后面的命令,默认启动
[root@docker1 docker_images]# docker run-it docker.io/centos /bin/bash
[root@7a652fc72a9f /]#ls /
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usrvar
[root@7a652fc72a9f /]#cd /etc/yum.repos.d/
[root@7a652fc72a9f yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Sources.repo CentOS-fasttrack.repo
CentOS-CR.repo CentOS-Media.repo CentOS-Vault.repo
[root@7a652fc72a9f yum.repos.d]# rm-rf C*
[root@7a652fc72a9f yum.repos.d]# ls
[root@7a652fc72a9f yum.repos.d]#vi dvd.repo//在容器里面配置一个yum源
[local]
name=local
baseurl=ftp://192.168.1.254/system
enable=1
gpgcheck=0
[root@7a652fc72a9f yum.repos.d]# yum-y install net-tools//安装软件
[root@7a652fc72a9f yum.repos.d]# exit
exit
3 案例3:镜像与容器常用指令
3.1 问题
本案例要求掌握镜像与容器的常用命令:
镜像常用指令练习
容器常用指令练习
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:镜像常用命令
1)查看后台运行的容器
[root@docker1 docker]# docker run-d docker.io/nginx//启动nginx的镜像
[root@docker1 docker]# docker ps//查看后台运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81458156f6e8 docker.io/nginx"nginx -g 'daemon off"9seconds ago Up8seconds80/tcp pedantic_goldberg
2)只显示容器ID
[root@docker1 docker]# docker ps-q
81458156f6e8
3)显示所有的容器,包括没有启动的
[root@docker1 docker]# docker ps-a
4)显示所有的容器ID
[root@docker1 docker]# docker ps-qa
81458156f6e8
3656f1978967
5)查看centos镜像历史(制作过程),如图-2所示:
[root@docker1 docker_images]# docker history docker.io/centos
图-2
7)删除镜像,启动容器时删除镜像会失败,先删除容器,再删除镜像
格式:docker rmi 镜像名
[root@docker1 docker]# docker rmi nginx//nginx为镜像名
Errorresponse from daemon:conflict:unable to remove repository reference"nginx"(must force)-container 81458156f6e8 is using its referenced image 42b4762643dc
//删除时报错
[root@docker1 docker]# docker stop81
81
[root@docker1 docker]# docker rm81
81
[root@docker2~]# docker rmi docker.io/nginx//删除nginx镜像
Untagged:docker.io/nginx:latest
Deleted:sha256:42b4762643dcc9bf492b08064b55fef64942f055f0da91289a8abf93c6d6b43c
Deleted:sha256:e0e55dd2303b3e3ec852acae267d1f8a3eea27a22c64a5829304ecee4d3f559c
Deleted:sha256:4062cf272cdd99e83b1c21f712e5e1359c91ecf92925e56c62133c3324b84e45
Deleted:sha256:3c816b4ead84066ec2cadec2b943993aaacc3fe35fcd77ada3d09dc4f3937313
8)修改镜像的名称和标签,默认标签为latest
[root@docker1 docker_images]# docker tag docker.io/centos:latest docker.io/cen:v1
[root@docker2~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 42b4762643dc4weeks ago109.2MB
docker.io/cen v1 42b4762643dc4weeks ago109.2MB
9)查看镜像的底层信息,如图-3所示:
[root@docker1 docker_images]# docker inspect docker.io/centos
图-3
10)修改镜像的标签
[root@docker1 docker_images]# docker tag docker.io/centos:latest docker.io/cen:v1
[root@docker1 docker_images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/cen v1 e934aafc22065months ago198.6MB
[root@docker1 docker_images]# docker rmi docker.io/centos//删除centos
[root@localhost~]# docker run-it centos
//启动的时候,因为是用标签标签启动的,所以会重新通过ID下载
[root@docker1 docker]# docker run-it docker.io/centos
Unable to find image'centos:latest'locally
Trying to pull repository registry.access.redhat.com/centos...
Trying to pull repository docker.io/library/centos...
latest:Pulling from docker.io/library/centos
a02a4930cb5d:Pull complete
Digest:sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
[root@b6c5f16461f5 /]#
[root@localhost~]# docker run-it docker.io/cen:v1
//通过新建的标签启动docker.io/cen:v1
步骤二:容器命令
1)关闭容器
命令:docker stop 容器ID
[root@docker1 docker]# docker stop 0f//0f为容器ID
0f
2)启动容器
[root@docker1 docker]# docker start 0f
0f
3)重启容器
[root@docker1 docker]# docker restart 0f
0f
4)删除容器
运行中删除不掉,先关闭容器
[root@docker1 docker]# docker rm 0f//删除失败
Errorresponse from daemon:You cannot remove a running container 0f63706692e15134a8f07655a992771b312b8eb01554fc37e1a39b03b28dd05c.Stop the container before attempting removal or use-f
[root@docker1 docker]# docker stop 0f//关闭容器
0f
[root@docker1 docker]# docker rm 0f//删除成功
0f
[root@docker1 docker]#
5)连接容器attach|exec
[root@docker1 docker]# docker attach 0f
[root@docker1 docker]# docker ps//容器关闭
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker1 docker]# docker exec-it 0f /bin/bash
[root@docker1 docker]# docker ps//容器不会关闭
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b3c50284a1c docker.io/centos"/bin/bash"15minutes ago Up15minutes tiny_lamarr
[root@docker1 docker]# docker top f7//查看容器进程列表
[root@docker1 docker]# docker run-itd docker.io/centos:latest
76e7577cff5d8a3ec877d3ea564fed2fb502bd4a0447705bec269a646d414d07
[root@docker1 docker]# ps
PID TTY TIME CMD
5552pts/000:00:00bash
6739pts/000:00:00ps
[root@docker1 docker]# docker exec-it76 /bin/bash
[root@76e7577cff5d /]# sleep50&
[1]30
[root@76e7577cff5d /]# exit
exit
[root@docker1 docker]# docker top76
UID PID PPID C STIME TTY TIME CMD
root67086691017:00pts/100:00:00 /bin/bash
UID PID PPID C STIME TTY TIME CMD
root27442729018:01pts/400:00:00 /bin/bash
6)过滤查看mac和ip地址
[root@docker1 docker]# docker inspect-f'{{.NetworkSettings.MacAddress}}'76
02:42:ac:11:00:04
[root@docker1 docker]# docker inspect-f'{{.NetworkSettings.IPAddress}}'76
172.17.0.4
7)修改nginx的显示内容
[root@docker1 docker_images]# docker run-it docker.io/nginx:latest
[root@docker1 docker_images]# docker exec-it56 /bin/bash
root@56ec8154f8e0:/# nginx -T /usr/share/nginx/html/
nginx:invalid option:"/usr/share/nginx/html/"//查找并显示结果
root@56ec8154f8e0:/# echo aaa > /usr/share/nginx/html/index.html
//修改主页显示的内容
root@56ec8154f8e0:/# nginx-T
root@56ec8154f8e0:/# cat /usr/share/nginx/html/index.html
aaa
8)过滤查看nginx的ip地址
[root@docker1 docker]# docker inspect-f'{{.NetworkSettings.IPAddress}}'a6
172.17.0.3
[root@docker1 docker]# curl172.17.0.3
aaa