Docker概述 、 部署Docker 、 Docker镜像 、 Docker基本命令案例
NSD CLOUD DAY04
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主机同样操作
- [[email protected] ~]# echo docker1 > /etc/hostname
- [[email protected] ~]# hostname docker1
- [[email protected]st ~]# echo docker2 > /etc/hostname
- [[email protected] ~]# hostname docker2
- [[email protected] ~]# 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
- [[email protected] ~]# systemctl restart network
- [[email protected] ~]# 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
- [[email protected] ~]# systemctl restart network
2)配置yum客户端,配置之前openstack的扩展源(RHEL7-extras.iso)即可(docker1和docker2主机同样操作)
- [[email protected] ]# vim /etc/yum.repos.d/rhel.repo
- ...
- [local_extras]
- name=CentOS-$releasever - Extras
- baseurl="ftp://192.168.1.254/extras"
- enabled=1
- gpgcheck=0
- [[email protected] ]# 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主机同样操作)
- [[email protected] ~]# yum -y install docker
- [[email protected] ~]# systemctl restart docker
- [[email protected] ~]# systemctl enable docker
- [[email protected] ~]# ifconfig //有docker0说明环境部署完成
- docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
- inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
- ether 02:42:3e:e7:3f:6e txqueuelen 0 (Ethernet)
- RX packets 0 bytes 0 (0.0 B)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 0 bytes 0 (0.0 B)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- [[email protected] ~]# docker version //查看版本
- [[email protected] ~]# yum -y install docker
- [[email protected] ~]# systemctl restart docker
- [[email protected] ~]# systemctl enable docker
- [[email protected] ~]# ifconfig //有docker0说明环境部署完成
- docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
- inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
- ether 02:42:53:82:b9:d4 txqueuelen 0 (Ethernet)
- RX packets 0 bytes 0 (0.0 B)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 0 bytes 0 (0.0 B)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- [[email protected] ~]# docker version //查看版本
2 案例2:镜像基本操作
2.1 问题
本案例要求熟悉镜像的基本操作:
- 导入镜像
- 导出镜像
- 启动镜像
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:docker镜像
1)下载镜像
- [[email protected] ~]# docker pull docker.io/busybox
- [[email protected] bulid]# docker pull docker.io/busybox
- Using default tag: latest
- Trying to pull repository docker.io/library/busybox ...
- latest: Pulling from docker.io/library/busybox
- Digest: sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
2)上传镜像
- [[email protected] ~]# docker push docker.io/busybox
3)查看镜像
- [[email protected] ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- docker.io/busybox latest d8233ab899d4 10 days ago 1.199 MB
4)查找busybox镜像
- [[email protected] ~]# docker search docker.io/busybox
5)导出busybox镜像为busybox.tar
- [[email protected] ~]# docker save docker.io/busybox:latest -o busybox.tar
- [[email protected] ~]# ls
- busybox.tar
6)导入镜像
- [[email protected] ~]# scp nginx.tar [email protected]:/root
- [[email protected] ~]# ls
- nginx.tar
- [[email protected] ~]# docker load -i nginx.tar
- 3c816b4ead84: Loading layer 58.47 MB/58.47 MB
- 787822cf1b17: Loading layer 54.44 MB/54.44 MB
- 89decbdf7fb7: Loading layer 3.584 kB/3.584 kB
- Loaded image: docker.io/nginx:latest/3.584 kB
- [[email protected] ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- docker.io/nginx latest 42b4762643dc 4 weeks ago 109.2 M e1ddd7948a1c 4 weeks ago 1.163 MB
7)删除镜像
- [[email protected] docker]# docker rmi docker.io/nginx
- Untagged: docker.io/nginx:latest
- Deleted: sha256:42b4762643dcc9bf492b08064b55fef64942f055f0da91289a8abf93c6d6b43c
- Deleted: sha256:e0e55dd2303b3e3ec852acae267d1f8a3eea27a22c64a5829304ecee4d3f559c
- Deleted: sha256:4062cf272cdd99e83b1c21f712e5e1359c91ecf92925e56c62133c3324b84e45
- Deleted: sha256:3c816b4ead84066ec2cadec2b943993aaacc3fe35fcd77ada3d09dc4f3937313
步骤二:一次性导入多个镜像
- [[email protected] 04]$ cd /linux-soft/04/
- [[email protected] 04]$ scp -r docker/ [email protected]:/root/
- [[email protected] ~]# cd docker
- [[email protected] docker]# ls
- centos.tar nginx.tar redis.tar registry.tar ubuntu.tar
- [[email protected] docker]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- [[email protected] docker]# for i in * ; do docker load -i $i; done
- bcc97fbfc9e1: Loading layer 208.2 MB/208.2 MB
- Loaded image: docker.io/centos:latest208.2 MB
- Loaded image: docker.io/nginx:latest
- a098f8909d23: Loading layer 338.4 kB/338.4 kB
- c3bc3362d4ff: Loading layer 3.034 MB/3.034 MB
- bf10d8534af6: Loading layer 36.4 MB/36.4 MB
- 0949ce28adcc: Loading layer 1.536 kB/1.536 kB
- b1f287cac208: Loading layer 3.584 kB/3.584 kB
- Loaded image: docker.io/redis:latest/3.584 kB
- 7bff100f35cb: Loading layer 4.672 MB/4.672 MB
- 6b6e0aba7201: Loading layer 1.587 MB/1.587 MB
- 18429e86e6ad: Loading layer 20.08 MB/20.08 MB
- 7b33b7de3d52: Loading layer 3.584 kB/3.584 kB
- 726e86b708de: Loading layer 2.048 kB/2.048 kB
- Loaded image: docker.io/registry:latest048 kB
- adcb570ae9ac: Loading layer 89.95 MB/89.95 MB
- 7604c8714555: Loading layer 15.87 kB/15.87 kB
- 9e9d3c3a7458: Loading layer 11.26 kB/11.26 kB
- 27a216ffe825: Loading layer 3.072 kB/3.072 kB
- Loaded image: docker.io/ubuntu:latest3.072 kB
- [[email protected] docker]# docker images
导入多个镜像如图-1所示:
图-1
步骤三:启动镜像
1)启动centos镜像生成一个容器
启动镜像时若不知道后面的命令加什么:
1、可以猜(如:/bin/bash、/bin/sh)
2、可以不加后面的命令,默认启动
- [[email protected] docker_images]# docker run -it docker.io/centos /bin/bash
- [[email protected] /]# ls /
- anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
- [[email protected] /]# cd /etc/yum.repos.d/
- [[email protected] 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
- [[email protected] yum.repos.d]# rm -rf C*
- [[email protected] yum.repos.d]# ls
- [[email protected] yum.repos.d]#vi dvd.repo //在容器里面配置一个yum源
- [local]
- name=local
- baseurl=ftp://192.168.1.254/system
- enable=1
- gpgcheck=0
- [[email protected] yum.repos.d]# yum -y install net-tools //安装软件
- [[email protected] yum.repos.d]# exit
- exit
3 案例3:镜像与容器常用指令
3.1 问题
本案例要求掌握镜像与容器的常用命令:
- 镜像常用指令练习
- 容器常用指令练习
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:镜像常用命令
1)查看后台运行的容器
- [[email protected] docker]# docker run -d docker.io/nginx //启动nginx的镜像
- [[email protected] docker]# docker ps //查看后台运行的容器
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 81458156f6e8 docker.io/nginx "nginx -g 'daemon off" 9 seconds ago Up 8 seconds 80/tcp pedantic_goldberg
2)只显示容器ID
- [[email protected] docker]# docker ps -q
- 81458156f6e8
3)显示所有的容器,包括没有启动的
- [[email protected] docker]# docker ps -a
4)显示所有的容器ID
- [[email protected] docker]# docker ps -qa
- 81458156f6e8
- 3656f1978967
5)查看centos镜像历史(制作过程),如图-2所示:
- [[email protected] docker_images]# docker history docker.io/centos
图-2
7)删除镜像,启动容器时删除镜像会失败,先删除容器,再删除镜像
格式:docker rmi 镜像名
- [[email protected] docker]# docker rmi nginx //nginx为镜像名
- Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 81458156f6e8 is using its referenced image 42b4762643dc
- //删除时报错
- [[email protected] docker]# docker stop 81
- 81
- [[email protected] docker]# docker rm 81
- 81
- [[email protected] ~]# 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
- [[email protected] docker_images]# docker tag docker.io/centos:latest docker.io/cen:v1
- [[email protected] ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- docker.io/centos latest 42b4762643dc 4 weeks ago 109.2 MB
- docker.io/cen v1 42b4762643dc 4 weeks ago 109.2 MB
9)查看镜像的底层信息,如图-3所示:
- [[email protected] docker_images]# docker inspect docker.io/centos
图-3
10)修改镜像的标签
- [[email protected] docker_images]# docker tag docker.io/centos:latest docker.io/cen:v1
- [[email protected] docker_images]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- docker.io/cen v1 e934aafc2206 5 months ago 198.6 MB
- [[email protected] docker_images]# docker rmi docker.io/centos //删除centos
- [[email protected] ~]# docker run -it centos
- //启动的时候,因为是用标签标签启动的,所以会重新通过ID下载
- [[email protected] 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
- [[email protected] /]#
- [[email protected] ~]# docker run -it docker.io/cen:v1
- //通过新建的标签启动docker.io/cen:v1
步骤二:容器命令
1)关闭容器
命令:docker stop 容器ID
- [[email protected] docker]# docker stop 0f //0f为容器ID
- 0f
2)启动容器
- [[email protected] docker]# docker start 0f
- 0f
3)重启容器
- [[email protected] docker]# docker restart 0f
- 0f
4)删除容器
运行中删除不掉,先关闭容器
- [[email protected] docker]# docker rm 0f //删除失败
- Error response from daemon: You cannot remove a running container 0f63706692e15134a8f07655a992771b312b8eb01554fc37e1a39b03b28dd05c. Stop the container before attempting removal or use -f
- [[email protected] docker]# docker stop 0f //关闭容器
- 0f
- [[email protected] docker]# docker rm 0f //删除成功
- 0f
- [[email protected] docker]#
5)连接容器attach|exec
- [[email protected] docker]# docker attach 0f
- [[email protected] docker]# docker ps //容器关闭
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- [[email protected] docker]# docker exec -it 0f /bin/bash
- [[email protected] docker]# docker ps //容器不会关闭
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 0b3c50284a1c docker.io/centos "/bin/bash" 15 minutes ago Up 15 minutes tiny_lamarr
- [[email protected] docker]# docker top f7 //查看容器进程列表
- [[email protected] docker]# docker run -itd docker.io/centos:latest
- 76e7577cff5d8a3ec877d3ea564fed2fb502bd4a0447705bec269a646d414d07
- [[email protected] docker]# ps
- PID TTY TIME CMD
- 5552 pts/0 00:00:00 bash
- 6739 pts/0 00:00:00 ps
- [[email protected] docker]# docker exec -it 76 /bin/bash
- [[email protected] /]# sleep 50 &
- [1] 30
- [[email protected] /]# exit
- exit
- [[email protected] docker]# docker top 76
- UID PID PPID C STIME TTY TIME CMD
- root 6708 6691 0 17:00 pts/1 00:00:00 /bin/bash
- UID PID PPID C STIME TTY TIME CMD
- root 2744 2729 0 18:01 pts/4 00:00:00 /bin/bash
6)过滤查看mac和ip地址
- [[email protected] docker]# docker inspect -f '{{.NetworkSettings.MacAddress}}' 76
- 02:42:ac:11:00:04
- [[email protected] docker]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 76
- 172.17.0.4
7)修改nginx的显示内容
- [[email protected] docker_images]# docker run -it docker.io/nginx:latest
- [[email protected] docker_images]# docker exec -it 56 /bin/bash
- [email protected]:/# nginx -T /usr/share/nginx/html/
- nginx: invalid option: "/usr/share/nginx/html/" //查找并显示结果
- [email protected]:/# echo aaa > /usr/share/nginx/html/index.html
- //修改主页显示的内容
- [email protected]:/# nginx -T
- [email protected]:/# cat /usr/share/nginx/html/index.html
- aaa
8)过滤查看nginx的ip地址
- [[email protected] docker]# docker inspect -f '{{.NetworkSettings.IPAddress}}' a6
- 172.17.0.3
- [[email protected] docker]# curl 172.17.0.3
- aaa