Top

NSD CLOUD DAY04

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

1 案例1:安装Docker

1.1 问题

本案例要求配置yum源并安装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>  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
[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>  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
[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
Using default tag: 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         d8233ab899d4        10 days ago         1.199 MB

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 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 
[root@docker2 ~]# 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)删除镜像

[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@room9pc01 04]$ cd /linux-soft/04/
[student@room9pc01 04]$ scp -r docker/ 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]# 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
[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  usr  var
[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"   9 seconds ago       Up 8 seconds        80/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为镜像名
Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 81458156f6e8 is using its referenced image 42b4762643dc  
//删除时报错
  
[root@docker1 docker]# docker stop 81
81
[root@docker1 docker]# docker rm 81
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              42b4762643dc        4 weeks ago         109.2 MB
docker.io/cen     v1                  42b4762643dc        4 weeks ago         109.2 MB


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             e934aafc2206   5 months ago    198.6 MB 
[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  //删除失败
Error response 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"   15 minutes ago   Up 15 minutes             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
 5552 pts/0    00:00:00 bash
 6739 pts/0    00:00:00 ps
 
[root@docker1 docker]# docker exec -it 76 /bin/bash
[root@76e7577cff5d /]# sleep 50 &
[1] 30
[root@76e7577cff5d /]# exit
exit

[root@docker1 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地址

[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 -it 56 /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]# curl 172.17.0.3
aaa