欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《云计算》-docker安装部署、镜像基本操作、镜像与容器常用指令

程序员文章站 2022-06-13 10:45:16
...
案例1:安装Docker
案例2:镜像基本操作
案例3:镜像与容器常用指令

1 案例1:安装Docker
1.1 问题

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

准备两台虚拟机,IP为192.168.1.10和192.168.1.20
安装docker-engine 和 docker-engine-selinux
关闭防火墙

1.2 步骤

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

步骤一:配置yum源

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

[aaa@qq.com ~]# mkdir /var/ftp/docker
[aaa@qq.com ~]# mv docker-engine-* /var/ftp/docker
[aaa@qq.com ~]# ls /var/ftp/docker
docker-engine-1.12.1-1.el7.centos.x86_64.rpm
docker-engine-selinux-1.12.1-1.el7.centos.noarch.rpm
[aaa@qq.com ~]# createrepo /var/ftp/docker/
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 1 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Spawning worker 4 with 0 pkgs
Spawning worker 5 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

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

[aaa@qq.com ~]# echo docker1 > /etc/hostname 
[aaa@qq.com ~]# hostname docker1
[aaa@qq.com ~]# echo docker2 > /etc/hostname 
[aaa@qq.com ~]# hostname docker2
[aaa@qq.com ~]# 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.10"
PREFIX=24
GATEWAY=192.168.1.254
[aaa@qq.com ~]# systemctl restart network
[aaa@qq.com ~]# 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.20"
PREFIX=24
GATEWAY=192.168.1.254
[aaa@qq.com ~]# systemctl restart network

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

[aaa@qq.com ~]# vim /etc/yum.repos.d/local.repo 
[local_repo]
name=CentOS-$releasever - Base
baseurl="ftp://192.168.1.254/system"
enabled=1
gpgcheck=1
[loca]
name=local
baseurl="ftp://192.168.1.254/docker"
enabled=1
gpgcheck=0
[aaa@qq.com ~]# vim /etc/yum.repos.d/local.repo 
[local_repo]
name=CentOS-$releasever - Base
baseurl="ftp://192.168.1.254/system"
enabled=1
gpgcheck=1
[loca]
name=local
baseurl="ftp://192.168.1.254/docker"
enabled=1
gpgcheck=0

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

[aaa@qq.com ~]# yum -y install docker-engine
[aaa@qq.com ~]# systemctl restart docker
[aaa@qq.com ~]# systemctl enable docker
[aaa@qq.com ~]# 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
[aaa@qq.com2 ~]# docker version        //查看版本
[aaa@qq.com ~]# yum -y install docker-engine
[aaa@qq.com ~]# systemctl restart docker
[aaa@qq.com ~]# systemctl enable docker
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# docker version        //查看版本

2 案例2:镜像基本操作
2.1 问题

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

导入镜像
导出镜像
启动镜像

2.2 步骤

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

步骤一:docker镜像

1)下载镜像

[aaa@qq.com ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
8c5a7da1afbc: Pull complete 
Digest: sha256:cb63aa0641a885f54de20f61d152187419e8f6b159ed11a251a09d115fdff9bd
Status: Downloaded newer image for busybox:latest

2)上传镜像

[aaa@qq.com ~]# docker push busybox

3)查看镜像

[aaa@qq.com ~]# docker images
REPOSITORY        TAG               IMAGE ID          CREATED           SIZE
busybox           latest            e1ddd7948a1c      4 weeks ago       1.163 MB

4)查找busybox镜像

[aaa@qq.com ~]# docker search busybox

5)导出busybox镜像为busybox.tar

[aaa@qq.com ~]# docker  save  busybox:latest >busybox.tar
[aaa@qq.com ~]# ls
busybox.tar  

6)导入镜像

[aaa@qq.com ~]# scp  busybox.tar 192.168.1.20:/root
[aaa@qq.com ~]# ls
busybox.tar  
[aaa@qq.com ~]# docker load <busybox.tar
f9d9e4e6e2f0: Loading layer [==================================================>] 1.378 MB/1.378 MB
Loaded image: busybox:latest[=>                                                 ] 32.77 kB/1.378 MB
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED           SIZE
busybox             latest              e1ddd7948a1c        4 weeks ago      1.163 MB

7)删除镜像

[aaa@qq.com ~]# docker rmi  busybox
Untagged: busybox:latest
Deleted: sha256:e1ddd7948a1c31709a23cc5b7dfe96e55fc364f90e1cebcde0773a1b5a30dcda
Deleted: sha256:f9d9e4e6e2f0689cd752390e14ade48b0ec6f2a488a05af5ab2f9ccaf54c299d

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

[aaa@qq.com ~]# yum -y install unzip
[aaa@qq.com ~]# unzip docker_images.zip 
Archive:  docker_images.zip
   creating: docker_images/
  inflating: docker_images/nginx.tar  
  inflating: docker_images/redis.tar  
  inflating: docker_images/centos.tar  
  inflating: docker_images/registry.tar  
  inflating: docker_images/ubuntu.tar
[aaa@qq.com ~]# ls
busybox.tar  docker_images  docker_images.zip  eip
[aaa@qq.com ~]# cd docker_images
[aaa@qq.com docker_images]# ls
centos.tar  nginx.tar  redis.tar  registry.tar  ubuntu.tar
[aaa@qq.com docker_images]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              e1ddd7948a1c        4 weeks ago         1.163 MB
[aaa@qq.com docker_images]# for i in *; do docker load <$i; done

导入多个镜像如图-1所示:
《云计算》-docker安装部署、镜像基本操作、镜像与容器常用指令
图-1

步骤三:启动镜像

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

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

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

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

[aaa@qq.com docker_images]# docker run -it centos /bin/bash
[aaa@qq.com /]# ls /
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[aaa@qq.com /]# cd /etc/yum.repos.d/
[aaa@qq.com 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
[aaa@qq.com yum.repos.d]# rm -rf C*
[aaa@qq.com yum.repos.d]# ls
[aaa@qq.com yum.repos.d]#vi dvd.repo    //在容器里面配置一个yum源
[local]
name=local
baseurl=ftp://192.168.1.254/system
enable=1
gpgcheck=0
[aaa@qq.com yum.repos.d]# yum -y install net-tools  //安装软件
[aaa@qq.com yum.repos.d]# exit
exit

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

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

镜像常用指令练习
容器常用指令练习

3.2 步骤

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

步骤一:镜像常用命令

1)查看后台运行的容器

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

2)只显示容器ID

[aaa@qq.com docker_images]# docker ps -q
56ec8154f8e0
85c6b0b62235
f7ee40a87af5

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

 [aaa@qq.com docker_images]# docker ps -a

4)显示所有的容器ID

[aaa@qq.com docker_images]# docker ps -qa
56ec8154f8e0
2b68c3960737
85c6b0b62235
f7ee40a87af5
b261be571648
fb2fb8c3d7a8

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

 [aaa@qq.com docker_images]# docker history centos

《云计算》-docker安装部署、镜像基本操作、镜像与容器常用指令
图-2

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

格式:docker rmi 镜像名

[aaa@qq.com docker_images]# docker rmi nginx  //nginx为镜像名
Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 4f83871aa42e is using its referenced image a5311a310510 //删除时报错
[aaa@qq.com docker_images]# docker stop 4f
4f
[aaa@qq.com docker_images]# docker rm 4f
4f
[aaa@qq.com docker_images]# docker rmi nginx   //成功删除
Untagged: nginx:latest
Deleted: sha256:d1fd7d86a8257f3404f92c4474fb3353076883062d64a09232d95d940627459d
Deleted: sha256:4d765aea84ce4f56bd623e4fd38dec996a259af3418e2466d0e2067ed0ae8aa6
Deleted: sha256:5d385be69c9c4ce5538e12e6e677727ebf19ca0afaff6f035d8043b5e413003a
Deleted: sha256:adb712878b60bd7ed8ce661c91eb3ac30f41b67bfafed321395863051596a8e9
Deleted: sha256:55a50a618c1b76f784b0b68a0b3d70db93b353fb03227ea6bd87f794cad92917
Deleted: sha256:e53f74215d12318372e4412d0f0eb3908e17db25c6185f670db49aef5271f91f

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

 [aaa@qq.com docker_images]# docker tag  centos:latest cen:v1

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

[aaa@qq.com docker_images]# docker inspect centos

《云计算》-docker安装部署、镜像基本操作、镜像与容器常用指令
图-3

10)修改镜像的标签

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

步骤二:容器命令

1)关闭容器

命令:docker stop 容器ID

[aaa@qq.com docker_images]# docker stop 0f   //0f为容器ID
0f

2)启动容器

 [aaa@qq.com docker_images]# docker start 0f
0f

3)重启容器

 [aaa@qq.com docker_images]# docker restart 0f
0f

4)删除容器

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

[aaa@qq.com docker_images]# docker rm 0f  //删除失败
Error response from daemon: You cannot remove a running container 0f63706692e15134a8f07655a992771b312b8eb01554fc37e1a39b03b28dd05c. Stop the container before attempting removal or use -f
[aaa@qq.com docker_images]# docker stop 0f   //关闭容器
0f
[aaa@qq.com docker_images]# docker rm 0f         //删除成功
0f
[aaa@qq.com docker_images]#

5)连接容器attach|exec

[aaa@qq.com docker_images]# docker attach  0f
[aaa@qq.com docker_images]# docker ps    //容器关闭
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES
[aaa@qq.com docker_images]#  docker exec -it 0f /bin/bash
[aaa@qq.com docker_images]# docker ps        //容器不会关闭
CONTAINER ID  IMAGE         COMMAND        CREATED          STATUS          PORTS     NAMES
0b3c50284a1c  centos:v1      "/bin/bash"   15 minutes ago   Up 15 minutes             tiny_lamarr
[aaa@qq.com docker_images]# docker top f7        //查看容器进程列表 
[aaa@qq.com ~]# docker run -itd centos:latest
[aaa@qq.com /]# ps
  PID TTY          TIME CMD
    1 ?        00:00:00 bash
   13 ?        00:00:00 ps  
 [aaa@qq.com docker_images]# docker exec -it 85 /bin/bash
aaa@qq.com:/# sleep 50 &
[1] 9
aaa@qq.com:/# exit
exit
[aaa@qq.com docker_images]#docker top 85
UID    PID  PPID C   STIME     TTY         TIME      CMD
root  2744 2729  0   18:01    pts/4  00:00:00   /bin/bash

6)过滤查看mac和ip地址

[aaa@qq.com docker_images]# docker inspect -f '{{.NetworkSettings.MacAddress}}' 4f
02:42:ac:11:00:03
[aaa@qq.com docker_images]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 4f
172.17.0.3

7)修改nginx的显示内容

[aaa@qq.com docker_images]# docker run -it nginx:latest 

《云计算》-docker安装部署、镜像基本操作、镜像与容器常用指令
[aaa@qq.com docker_images]# docker exec -it 56 /bin/bash
aaa@qq.com:/# nginx -T /usr/share/nginx/html/
nginx: invalid option: “/usr/share/nginx/html/” //查找并显示结果
aaa@qq.com:/# echo aaa > /usr/share/nginx/html/index.html
//修改主页显示的内容
aaa@qq.com:/# nginx -T
aaa@qq.com:/# cat /usr/share/nginx/html/index.html
aaa

8)过滤查看nginx的ip地址

[aaa@qq.com ~]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 56
172.17.0.5
[aaa@qq.com ~]# curl   172.17.0.5
aaa
相关标签: 云计算全栈