docker容器部署 鏡像 經常使用指令

Top

NSD CLOUD DAY06

  1. 案例1:安裝Docker
  2. 案例2:鏡像基本操做
  3. 案例3:鏡像與容器經常使用指令

1 案例1:安裝Docker

1.1 問題

本案例要求配置yum源並安裝Docker:html

  • 準備兩臺虛擬機,IP爲192.168.1.10和192.168.1.20
  • 安裝docker-engine 和 docker-engine-selinux
  • 關閉防火牆

1.2 步驟

實現此案例須要按照以下步驟進行。linux

步驟一:配置yum源nginx

1)配置第三方yum源(真機操做)redis

  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主機一樣操做sql

  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. [root@docker2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
  19. # Generated by dracut initrd
  20. DEVICE="eth0"
  21. ONBOOT="yes"
  22. IPV6INIT="no"
  23. IPV4_FAILURE_FATAL="no"
  24. NM_CONTROLLED="no"
  25. TYPE="Ethernet"
  26. BOOTPROTO="static"
  27. IPADDR="192.168.1.20"
  28. PREFIX=24
  29. GATEWAY=192.168.1.254
  30. [root@docker1 ~]# systemctl restart network

3)配置yum客戶端(docker1和docker2主機一樣操做)docker

  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. [loca]
  8. name=local
  9. baseurl="ftp://192.168.1.254/docker"
  10. enabled=1
  11. gpgcheck=0
  12. [root@docker2 ~]# vim /etc/yum.repos.d/local.repo
  13. [local_repo]
  14. name=CentOS-$releasever - Base
  15. baseurl="ftp://192.168.1.254/system"
  16. enabled=1
  17. gpgcheck=1
  18. [loca]
  19. name=local
  20. baseurl="ftp://192.168.1.254/docker"
  21. enabled=1
  22. gpgcheck=0

4)安裝docker(docker1和docker2主機一樣操做)ubuntu

  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. [root@docker2 ~]# yum -y install docker-engine
  14. [root@docker2 ~]# systemctl restart docker
  15. [root@docker2 ~]# systemctl enable docker
  16. [root@docker2 ~]# ifconfig     //有docker0說明環境部署完成
  17. docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
  18. inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
  19. ether 02:42:53:82:b9:d4 txqueuelen 0 (Ethernet)
  20. RX packets 0 bytes 0 (0.0 B)
  21. RX errors 0 dropped 0 overruns 0 frame 0
  22. TX packets 0 bytes 0 (0.0 B)
  23. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  24. [root@docker2 ~]# docker version        //查看版本

2 案例2:鏡像基本操做

2.1 問題

本案例要求熟悉鏡像的基本操做:vim

  • 導入鏡像
  • 導出鏡像
  • 啓動鏡像

2.2 步驟

實現此案例須要按照以下步驟進行。centos

步驟一:docker鏡像bash

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鏡像生成一個容器

啓動鏡像時若不知道後面的命令加什麼:

一、能夠猜(如:/bin/bash、/bin/sh)

二、能夠不加後面的命令,默認啓動

  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. Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 4f83871aa42e is using its referenced image a5311a310510 //刪除時報錯
  3. [root@docker1 docker_images]# docker stop 4f
  4. 4f
  5. [root@docker1 docker_images]# docker rm 4f
  6. 4f
  7. [root@docker1 docker_images]# docker rmi nginx //成功刪除
  8. Untagged: nginx:latest
  9. Deleted: sha256:d1fd7d86a8257f3404f92c4474fb3353076883062d64a09232d95d940627459d
  10. Deleted: sha256:4d765aea84ce4f56bd623e4fd38dec996a259af3418e2466d0e2067ed0ae8aa6
  11. Deleted: sha256:5d385be69c9c4ce5538e12e6e677727ebf19ca0afaff6f035d8043b5e413003a
  12. Deleted: sha256:adb712878b60bd7ed8ce661c91eb3ac30f41b67bfafed321395863051596a8e9
  13. Deleted: sha256:55a50a618c1b76f784b0b68a0b3d70db93b353fb03227ea6bd87f794cad92917
  14. 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. [root@docker1 docker_images]# docker top f7        //查看容器進程列表
  9. [root@localhost ~]# docker run -itd centos:latest
  10. [root@0b3c50284a1c /]# ps
  11. PID TTY TIME CMD
  12. 1 ? 00:00:00 bash
  13. 13 ? 00:00:00 ps
  14. [root@docker1 docker_images]# docker exec -it 85 /bin/bash
  15. root@85c6b0b62235:/# sleep 50 &
  16. [1] 9
  17. root@85c6b0b62235:/# exit
  18. exit
  19. [root@docker1 docker_images]#docker top 85
  20. UID PID PPID C STIME     TTY         TIME      CMD
  21. 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
  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
相關文章
相關標籤/搜索