Top

NSD CLOUD DAY05

  1. 案例1:安装Docker
  2. 案例2:镜像基本操作
  3. 案例3:镜像与容器常用指令

1 案例1:安装Docker

1.1 问题

本案例要求配置yum源并安装Docker:

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置yum源

1)配置第三方yum源(真机操作)

  1. [root@room9pc01 ~]# mkdir /var/ftp/docker
  2. [root@room9pc01 ~]# mv docker-engine-* /var/ftp/docker
  3. [root@room9pc01 ~]# ls /var/ftp/docker
  4. docker-engine-1.12.1-1.el7.centos.x86_64.rpm
  5. docker-engine-selinux-1.12.1-1.el7.centos.noarch.rpm
  6. [root@room9pc01 ~]# createrepo /var/ftp/docker/
  7. Spawning worker 0 with 1 pkgs
  8. Spawning worker 1 with 1 pkgs
  9. Spawning worker 2 with 0 pkgs
  10. Spawning worker 3 with 0 pkgs
  11. Spawning worker 4 with 0 pkgs
  12. Spawning worker 5 with 0 pkgs
  13. Workers Finished
  14. Saving Primary metadata
  15. Saving file lists metadata
  16. Saving other metadata
  17. Generating sqlite DBs
  18. Sqlite DBs complete

2)配置IP(虚拟机配置静态ip)docker1和docker2主机同样操作

  1. [root@localhost ~]# echo docker1 > /etc/hostname
  2. [root@localhost ~]# hostname docker1
  3. [root@localhost ~]# echo docker2 > /etc/hostname
  4. [root@localhost ~]# hostname docker2
  5. [root@docker1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
  6. # Generated by dracut initrd
  7. DEVICE="eth0"
  8. ONBOOT="yes"
  9. IPV6INIT="no"
  10. IPV4_FAILURE_FATAL="no"
  11. NM_CONTROLLED="no"
  12. TYPE="Ethernet"
  13. BOOTPROTO="static"
  14. IPADDR="192.168.1.10"
  15. PREFIX=24
  16. GATEWAY=192.168.1.254
  17. [root@docker1 ~]# systemctl restart network
  18.  
  19. [root@docker2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
  20. # Generated by dracut initrd
  21. DEVICE="eth0"
  22. ONBOOT="yes"
  23. IPV6INIT="no"
  24. IPV4_FAILURE_FATAL="no"
  25. NM_CONTROLLED="no"
  26. TYPE="Ethernet"
  27. BOOTPROTO="static"
  28. IPADDR="192.168.1.20"
  29. PREFIX=24
  30. GATEWAY=192.168.1.254
  31. [root@docker1 ~]# systemctl restart network

3)配置yum客户端(docker1和docker2主机同样操作)

  1. [root@docker1 ~]# vim /etc/yum.repos.d/local.repo
  2. [local_repo]
  3. name=CentOS-$releasever - Base
  4. baseurl="ftp://192.168.1.254/system"
  5. enabled=1
  6. gpgcheck=1
  7.  
  8. [loca]
  9. name=local
  10. baseurl="ftp://192.168.1.254/docker"
  11. enabled=1
  12. gpgcheck=0
  13.  
  14. [root@docker2 ~]# vim /etc/yum.repos.d/local.repo
  15. [local_repo]
  16. name=CentOS-$releasever - Base
  17. baseurl="ftp://192.168.1.254/system"
  18. enabled=1
  19. gpgcheck=1
  20.  
  21. [loca]
  22. name=local
  23. baseurl="ftp://192.168.1.254/docker"
  24. enabled=1
  25. gpgcheck=0

4)安装docker(docker1和docker2主机同样操作)

  1. [root@docker1 ~]# yum -y install docker-engine
  2. [root@docker1 ~]# systemctl restart docker
  3. [root@docker1 ~]# systemctl enable docker
  4. [root@docker1 ~]# ifconfig     //有docker0说明环境部署完成
  5. docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
  6. inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
  7. ether 02:42:3e:e7:3f:6e txqueuelen 0 (Ethernet)
  8. RX packets 0 bytes 0 (0.0 B)
  9. RX errors 0 dropped 0 overruns 0 frame 0
  10. TX packets 0 bytes 0 (0.0 B)
  11. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  12. [root@docker2 ~]# docker version        //查看版本
  13.  
  14. [root@docker2 ~]# yum -y install docker-engine
  15. [root@docker2 ~]# systemctl restart docker
  16. [root@docker2 ~]# systemctl enable docker
  17. [root@docker2 ~]# ifconfig     //有docker0说明环境部署完成
  18. docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
  19. inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
  20. ether 02:42:53:82:b9:d4 txqueuelen 0 (Ethernet)
  21. RX packets 0 bytes 0 (0.0 B)
  22. RX errors 0 dropped 0 overruns 0 frame 0
  23. TX packets 0 bytes 0 (0.0 B)
  24. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  25. [root@docker2 ~]# docker version        //查看版本

2 案例2:镜像基本操作

2.1 问题

本案例要求熟悉镜像的基本操作:

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:docker镜像

1)下载镜像

  1. [root@docker1 ~]# docker pull busybox
  2. Using default tag: latest
  3. latest: Pulling from library/busybox
  4. 8c5a7da1afbc: Pull complete
  5. Digest: sha256:cb63aa0641a885f54de20f61d152187419e8f6b159ed11a251a09d115fdff9bd
  6. Status: Downloaded newer image for busybox:latest

2)上传镜像

  1. [root@docker1 ~]# docker push busybox

3)查看镜像

  1. [root@docker1 ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. busybox latest e1ddd7948a1c 4 weeks ago 1.163 MB

4)查找busybox镜像

  1. [root@docker1 ~]# docker search busybox

5)导出busybox镜像为busybox.tar

  1. [root@docker1 ~]# docker save busybox:latest >busybox.tar
  2. [root@docker1 ~]# ls
  3. busybox.tar

6)导入镜像

  1. [root@docker1 ~]# scp busybox.tar 192.168.1.20:/root
  2. [root@docker2 ~]# ls
  3. busybox.tar
  4. [root@docker2 ~]# docker load <busybox.tar
  5. f9d9e4e6e2f0: Loading layer [==================================================>] 1.378 MB/1.378 MB
  6. Loaded image: busybox:latest[=> ] 32.77 kB/1.378 MB
  7. [root@docker2 ~]# docker images
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. busybox latest e1ddd7948a1c 4 weeks ago 1.163 MB

7)删除镜像

  1. [root@docker2 ~]# docker rmi busybox
  2. Untagged: busybox:latest
  3. Deleted: sha256:e1ddd7948a1c31709a23cc5b7dfe96e55fc364f90e1cebcde0773a1b5a30dcda
  4. Deleted: sha256:f9d9e4e6e2f0689cd752390e14ade48b0ec6f2a488a05af5ab2f9ccaf54c299d

步骤二:一次性导入多个镜像

  1. [root@docker1 ~]# yum -y install unzip
  2. [root@docker1 ~]# unzip docker_images.zip
  3. Archive: docker_images.zip
  4. creating: docker_images/
  5. inflating: docker_images/nginx.tar
  6. inflating: docker_images/redis.tar
  7. inflating: docker_images/centos.tar
  8. inflating: docker_images/registry.tar
  9. inflating: docker_images/ubuntu.tar
  10. [root@docker1 ~]# ls
  11. busybox.tar docker_images docker_images.zip eip
  12. [root@docker1 ~]# cd docker_images
  13. [root@docker1 docker_images]# ls
  14. centos.tar nginx.tar redis.tar registry.tar ubuntu.tar
  15. [root@docker1 docker_images]# docker images
  16. REPOSITORY TAG IMAGE ID CREATED SIZE
  17. busybox latest e1ddd7948a1c 4 weeks ago 1.163 MB
  18. [root@docker1 docker_images]# for i in *; do docker load <$i; done

导入多个镜像如图-1所示:

图-1

步骤三:启动镜像

1)启动centos镜像生成一个容器

启动镜像时若不知道后面的命令加什么:

1、可以猜(如:/bin/bash、/bin/sh)

2、可以不加后面的命令,默认启动

  1. [root@docker1 docker_images]# docker run -it centos /bin/bash
  2. [root@7a652fc72a9f /]# ls /
  3. anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
  4. [root@7a652fc72a9f /]# cd /etc/yum.repos.d/
  5. [root@7a652fc72a9f yum.repos.d]# ls
  6. CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Sources.repo CentOS-fasttrack.repo
  7. CentOS-CR.repo CentOS-Media.repo CentOS-Vault.repo
  8. [root@7a652fc72a9f yum.repos.d]# rm -rf C*
  9. [root@7a652fc72a9f yum.repos.d]# ls
  10. [root@7a652fc72a9f yum.repos.d]#vi dvd.repo //在容器里面配置一个yum源
  11. [local]
  12. name=local
  13. baseurl=ftp://192.168.1.254/system
  14. enable=1
  15. gpgcheck=0
  16. [root@7a652fc72a9f yum.repos.d]# yum -y install net-tools //安装软件
  17. [root@7a652fc72a9f yum.repos.d]# exit
  18. exit

3 案例3:镜像与容器常用指令

3.1 问题

本案例要求掌握镜像与容器的常用命令:

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:镜像常用命令

1)查看后台运行的容器

  1. [root@docker1 ~]# docker run -d nginx //启动nginx的镜像
  2. [root@docker1 ~]# docker ps    //查看后台运行的容器
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 56ec8154f8e0 nginx:latest "nginx -g 'daemon off" 17 minutes ago Up 12 minutes 80/tcp, 443/tcp zen_darwin

2)只显示容器ID

  1. [root@docker1 docker_images]# docker ps -q
  2. 56ec8154f8e0
  3. 85c6b0b62235
  4. f7ee40a87af5

3)显示所有的容器,包括没有启动的

  1. [root@docker1 docker_images]# docker ps -a

4)显示所有的容器ID

  1. [root@docker1 docker_images]# docker ps -qa
  2. 56ec8154f8e0
  3. 2b68c3960737
  4. 85c6b0b62235
  5. f7ee40a87af5
  6. b261be571648
  7. fb2fb8c3d7a8

5)查看centos镜像历史(制作过程),如图-2所示:

  1. [root@docker1 docker_images]# docker history centos

图-2

7)删除镜像,启动容器时删除镜像会失败,先删除容器,再删除镜像

格式:docker rmi 镜像名

  1. [root@docker1 docker_images]# docker rmi nginx //nginx为镜像名
  2.  
  3. Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 4f83871aa42e is using its referenced image a5311a310510 //删除时报错
  4. [root@docker1 docker_images]# docker stop 4f
  5. 4f
  6. [root@docker1 docker_images]# docker rm 4f
  7. 4f
  8. [root@docker1 docker_images]# docker rmi nginx //成功删除
  9. Untagged: nginx:latest
  10. Deleted: sha256:d1fd7d86a8257f3404f92c4474fb3353076883062d64a09232d95d940627459d
  11. Deleted: sha256:4d765aea84ce4f56bd623e4fd38dec996a259af3418e2466d0e2067ed0ae8aa6
  12. Deleted: sha256:5d385be69c9c4ce5538e12e6e677727ebf19ca0afaff6f035d8043b5e413003a
  13. Deleted: sha256:adb712878b60bd7ed8ce661c91eb3ac30f41b67bfafed321395863051596a8e9
  14. Deleted: sha256:55a50a618c1b76f784b0b68a0b3d70db93b353fb03227ea6bd87f794cad92917
  15. Deleted: sha256:e53f74215d12318372e4412d0f0eb3908e17db25c6185f670db49aef5271f91f

8)修改镜像的名称和标签,默认标签为latest

  1. [root@docker1 docker_images]# docker tag centos:latest cen:v1

9)查看镜像的底层信息,如图-3所示:

  1. [root@docker1 docker_images]# docker inspect centos

图-3

10)修改镜像的标签

  1. [root@docker1 docker_images]# docker tag centos:latest cen:v1
  2. [root@docker1 docker_images]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. cen v1 e934aafc2206 5 months ago 198.6 MB
  5. [root@docker1 docker_images]# docker rmi centos //删除centos
  6. [root@localhost ~]# docker run -it centos
  7. //启动的时候,因为是用标签标签启动的,所以会重新通过ID下载
  8. [root@localhost ~]# docker run -it centos
  9. Unable to find image 'centos:latest' locally
  10. latest: Pulling from library/centos
  11. Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
  12. Status: Downloaded newer image for centos:latest
  13. [root@localhost ~]# docker run -it cen:v1 //通过新建的标签启动cen:v1

步骤二:容器命令

1)关闭容器

命令:docker stop 容器ID

  1. [root@docker1 docker_images]# docker stop 0f //0f为容器ID
  2. 0f

2)启动容器

  1. [root@docker1 docker_images]# docker start 0f
  2. 0f

3)重启容器

  1. [root@docker1 docker_images]# docker restart 0f
  2. 0f

4)删除容器

运行中删除不掉,先关闭容器

  1. [root@docker1 docker_images]# docker rm 0f //删除失败
  2. Error response from daemon: You cannot remove a running container 0f63706692e15134a8f07655a992771b312b8eb01554fc37e1a39b03b28dd05c. Stop the container before attempting removal or use -f
  3. [root@docker1 docker_images]# docker stop 0f //关闭容器
  4. 0f
  5. [root@docker1 docker_images]# docker rm 0f         //删除成功
  6. 0f
  7. [root@docker1 docker_images]#

5)连接容器attach|exec

  1. [root@docker1 docker_images]# docker attach 0f
  2. [root@docker1 docker_images]# docker ps //容器关闭
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. [root@docker1 docker_images]# docker exec -it 0f /bin/bash
  5. [root@docker1 docker_images]# docker ps        //容器不会关闭
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 0b3c50284a1c centos:v1 "/bin/bash" 15 minutes ago Up 15 minutes tiny_lamarr
  8.  
  9. [root@docker1 docker_images]# docker top f7        //查看容器进程列表
  10. [root@localhost ~]# docker run -itd centos:latest
  11. [root@0b3c50284a1c /]# ps
  12. PID TTY TIME CMD
  13. 1 ? 00:00:00 bash
  14. 13 ? 00:00:00 ps
  15. [root@docker1 docker_images]# docker exec -it 85 /bin/bash
  16. root@85c6b0b62235:/# sleep 50 &
  17. [1] 9
  18. root@85c6b0b62235:/# exit
  19. exit
  20.  
  21.  
  22. [root@docker1 docker_images]#docker top 85
  23.  
  24. UID PID PPID C STIME     TTY         TIME      CMD
  25. root 2744 2729 0 18:01 pts/4 00:00:00 /bin/bash

6)过滤查看mac和ip地址

  1. [root@docker1 docker_images]# docker inspect -f '{{.NetworkSettings.MacAddress}}' 4f
  2. 02:42:ac:11:00:03
  3. [root@docker1 docker_images]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 4f
  4. 172.17.0.3

7)修改nginx的显示内容

  1. [root@docker1 docker_images]# docker run -it nginx:latest
  2.  
  1. [root@docker1 docker_images]# docker exec -it 56 /bin/bash
  2. root@56ec8154f8e0:/# nginx -T /usr/share/nginx/html/
  3. nginx: invalid option: "/usr/share/nginx/html/" //查找并显示结果
  4. root@56ec8154f8e0:/# echo aaa > /usr/share/nginx/html/index.html
  5. //修改主页显示的内容
  6. root@56ec8154f8e0:/# nginx -T
  7. root@56ec8154f8e0:/# cat /usr/share/nginx/html/index.html
  8. aaa

8)过滤查看nginx的ip地址

  1. [root@docker1 ~]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 56
  2. 172.17.0.5
  3. [root@docker1 ~]# curl 172.17.0.5
  4. aaa