• 全网最牛最全的测试开发技术之Docker容器实战


    【文章末尾给大家准备了大量的福利】

    docker概述

          Docker是一个开源的容器引擎,基于Go语言开发,同时基于Apache2.0协议开发。对Docker简单的理解就是使用Docker可以

    把一台服务器隔离成一个个独立的容器,我们可以把这地方描述的容器理解成一个沙盒。在每个容器中运行一个程序,不同的容器

    之间相互隔离,容器的创建,停止,以及启动基本都是秒为单位,而且容器对资源的需求也是非常有限的,比起虚拟化的技术而言,

    可以节约很多的资源成本。在DevOps技术栈体系和可持续交付中占据了重要的位置。

    docker安装

    内存检测

    1. [root@wuyaShare ~]# uname -r
    2. 3.10.0-1160.31.1.el7.x86_64

    系统升级

    yum update 

    安装依赖包

    yum install -y yum-utils device-mapper-persistent-data lvm2

    设置yum源

    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

    查看docker版本

    1. yum list docker-ce --showduplicates | sort -r
    2. #执行后,输出的结果信息如下:
    3. Installed Packages
    4. docker-ce.x86_64           3:20.10.9-3.el7                   docker-ce-stable
    5. docker-ce.x86_64           3:20.10.8-3.el7                   docker-ce-stable
    6. docker-ce.x86_64           3:20.10.7-3.el7                   docker-ce-stable
    7. docker-ce.x86_64           3:20.10.7-3.el7                   @docker-ce-stable

    docker安装

    yum install docker-ce-19.03.1-3.el7

    验证版本

    1. docker version
    2. #执行后,输出的结果信息为
    3. Client: Docker Engine - Community
    4. Version:           20.10.7
    5. API version:       1.41
    6. Go version:       go1.13.15
    7. Git commit:       f0df350
    8. Built:             Wed Jun 2 11:58:10 2021
    9. OS/Arch:           linux/amd64
    10. Context:           default
    11. Experimental:     true
    12. Server: Docker Engine - Community
    13. Engine:
    14. Version:         20.10.7
    15. API version:     1.41 (minimum version 1.12)
    16. Go version:       go1.13.15
    17. Git commit:       b0f5bc3
    18. Built:           Wed Jun 2 11:56:35 2021
    19. OS/Arch:         linux/amd64
    20. Experimental:     false
    21. containerd:
    22. Version:         1.4.6
    23. GitCommit:       d71fcd7d8303cbf684402823e425e9dd2e99285d
    24. runc:
    25. Version:         1.0.0-rc95
    26. GitCommit:       b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
    27. docker-init:
    28. Version:         0.19.0
    29. GitCommit:       de40ad0

    设置开机启动

    1. systemctl start docker
    2. systemctl enable docker

    查看docker状态

    1. systemctl status docker
    2. #执行后,输出的结果信息如下所示
    3. ● docker.service - Docker Application Container Engine
    4.   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
    5.   Active: active (running) since Sun 2021-10-10 11:29:53 CST; 5 days ago
    6.     Docs: https://docs.docker.com
    7. Main PID: 25805 (dockerd)
    8.   Tasks: 12
    9.   Memory: 63.1M
    10.   CGroup: /system.slice/docker.service
    11.           └─25805 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

    查看docker信息

    查看docker的存储路径以及docker的信息,命令为:docker info ,具体指令执行以及输出信息为:

    1. docker info
    2. #执行如上的基本信息后显示如下(部分信息)
    3. Operating System: CentOS Linux 7 (Core)
    4. OSType: linux
    5. Architecture: x86_64
    6. CPUs: 2
    7. Total Memory: 3.7GiB
    8. Name: wuyaShare
    9. ID: KBS5:IWHS:POJW:BLCG:XSQ5:OU2B:5YPB:KXCW:JYZP:GHYI:KP4M:VEA5
    10. Docker Root Dir: /var/lib/docker
    11. Debug Mode: false
    12. Username: wuyashare
    13. Registry: https://index.docker.io/v1/
    14. Labels:
    15. Experimental: false
    16. Insecure Registries:
    17. 127.0.0.0/8
    18. Registry Mirrors:
    19. https://8xpkSwnt.mirror.aliyuncs.com/
    20. Live Restore Enabled: false

    Docker管理

    初玩docker

    安装docker完成后,我们先来玩下docker的基本使用。不管是学习任何的编程语言还是学习docker,都先向世界好,下面也详细

    的演示下这部分的过程,输入指令为:

    1. docker run hello-world
    2. #输入如上的指令后,输出的结果信息如下
    3. Unable to find image 'hello-world:latest' locally
    4. latest: Pulling from library/hello-world
    5. 2db29710123e: Pull complete
    6. Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
    7. Status: Downloaded newer image for hello-world:latest
    8. Hello from Docker!
    9. This message shows that your installation appears to be working correctly.
    10. To generate this message, Docker took the following steps:
    11. 1. The Docker client contacted the Docker daemon.
    12. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    13.   (amd64)
    14. 3. The Docker daemon created a new container from that image which runs the
    15.   executable that produces the output you are currently reading.
    16. 4. The Docker daemon streamed that output to the Docker client, which sent it
    17.   to your terminal.
    18. To try something more ambitious, you can run an Ubuntu container with:
    19. $ docker run -it ubuntu bash
    20. Share images, automate workflows, and more with a free Docker ID:
    21. https://hub.docker.com/
    22. For more examples and ideas, visit:
    23. https://docs.docker.com/get-started/

    备注:docker run imageName后,实际上docker内部的整个过程,是先从docker hub获取镜像的信息后,然后运行这个docker的镜像。

    获取镜像

    在docker中,我看首先需要从docker hub获取到镜像,也就是image,然后才可以运行它。docker获取镜像的步骤可以总结为如下:

    • 获取该软件的docker镜像,直接可以进行搜索,比如docker pull nginx

    • 运行该容器,运行成功后可以启动一个容器,可以nginx服务就运行在一个容器里面了

    • 停止容器,删除该镜像

    docker容器式的环境更可以看成是一个沙盒的环境,在这个沙盒的环境中,我们可以搭建对应的环境,比如安装一个centos的操作系统。

    下面详细的演示下获取centos镜像的过程,具体指令如下:

    1. #获取镜像的指令可以总结为:docker pull imageName:image的版本
    2. [root@wuyaShare ~]# docker pull centos:7.8.2003
    3. 7.8.2003: Pulling from library/centos
    4. 9b4ebb48de8d: Pull complete
    5. Digest: sha256:8540a199ad51c6b7b51492fa9fee27549fd11b3bb913e888ab2ccf77cbb72cc1
    6. Status: Downloaded newer image for centos:7.8.2003
    7. docker.io/library/centos:7.8.2003

    如上可以看到已经获取到centos的镜像信息,可以查看它的镜像,指令具体为:

    docker images

    执行后,输出结果信息如下:

    1. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    2. centos       7.8.2003   afb6fca791e0   17 months ago   203MB

    获取到镜像后,就可以运行镜像的信息,运行使用到的指令具体总结为如下:

    • -it:开启交互式的命令

    下面主要演示运行centos:7.8.2003的镜像信息,如下所示:

    1. [root@wuyaShare ~]# docker run -it centos:7.8.2003 bash
    2. [root@b35d9c343f4b /]# date
    3. Fri Oct 15 08:33:16 UTC 2021
    4. [root@b35d9c343f4b /]# echo "Hello Docker"
    5. Hello Docker

    如果想退出该环境,使用的命令为:exit就可以退出了。

    查看镜像

    获取镜像后,我们可以查看镜像的信息,查看镜像的指令可以查看所有的镜像,也可以查看具体的镜像信息和格式化的查看。

    查看所有

    查看所有的镜像信息使用的指令为:

    1. docker images or docker image ls
    2. #执行如上的指令后,显示查看到的镜像的信息
    3. [root@wuyaShare ~]# docker images
    4. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    5. centos       7.8.2003   afb6fca791e0   17 months ago   203MB
    6. [root@wuyaShare ~]# docker image ls
    7. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    8. centos       7.8.2003   afb6fca791e0   17 months ago   203MB

    查看具体镜像

    当docker镜像很多的时候,也可以查看具体的镜像信息,这个使用可以使用docker image ls | grep imageName,如下具体

    演示下该指令的使用,具体如下:

    1. [root@wuyaShare ~]# docker image ls | grep centos
    2. centos       7.8.2003   afb6fca791e0   17 months ago   203MB

    显示镜像ID

    某些时候,需要只查看镜像的ID,并不需要展示其他的信息,下面演示只查看镜像的ID的,指令如下:

    1. [root@wuyaShare ~]# docker images -q
    2. afb6fca791e0

    查看镜像详细信息

    获取到镜像的ID后,可以使用指令inspect查看镜像的详细的信息,命令如下所示:

    1. docker image inspect $(docker images -q)
    2. #仅仅使用于mac和Linux的系统,windows系统该命令是无法使用的

    运行镜像

    获取docker的镜像后,就可以运行对应的docker的镜像信息了,运行的命令为run,具体指令总结如下:

    • -it:开启交互式的命令

    • --rm:容器退出时删除容器的记录信息

    • --name:指定容器的名称

    • -d:后台方式运行容器

    每个镜像运行后,都会在容器中存在一条记录的信息,查询的指令为:

    docker ps -a 

    下面演示主要运行centos:7.8.2003的镜像信息,来演示它的过程:

    1. [root@wuyaShare ~]# docker run -it --name centos7.8.2003 centos:7.8.2003
    2. [root@3db6f936ed4b /]# date
    3. Fri Oct 15 09:14:17 UTC 2021

    执行后,可以再查看容器中的记录信息,具体如下:

    1. [root@wuyaShare ~]# docker ps -a
    2. CONTAINER ID   IMAGE             COMMAND       CREATED             STATUS                         PORTS     NAMES
    3. 3db6f936ed4b   centos:7.8.2003   "/bin/bash"   About a minute ago   Exited (0) About a minute ago             centos7.8.2003

    如上,可以看到容器的名称为:centos7.8.2003。

    如果每次运行镜像不想在容器中记录它的信息,那么就可以带上--rm,下面具体演示下它的使用:

    1. [root@wuyaShare ~]# docker run -it --rm --name centos7.8.2003 centos:7.8.2003
    2. [root@e096c9fd6281 /]# date
    3. Fri Oct 15 10:42:43 UTC 2021
    4. [root@e096c9fd6281 /]# exit
    5. exit
    6. #查看容器记录,显示为空
    7. [root@wuyaShare ~]# docker ps -a
    8. CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS   PORTS     NAMES

    镜像信息修改

    删除镜像

    删除镜像可以使用镜像的ID,名字,摘要等方式来进行删除。删除镜像主要是两种方式,具体总结如下。

    docker的镜像未被容器运行

    未被执行的镜像,直接可以根据ID,NAME来进行删除,下面详细的演示该过程。

    依据name删除

    1. #获取镜像信息
    2. [root@wuyaShare ~]# docker pull hello-world
    3. Using default tag: latest
    4. latest: Pulling from library/hello-world
    5. 2db29710123e: Pull complete
    6. Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
    7. Status: Downloaded newer image for hello-world:latest
    8. docker.io/library/hello-world:latest
    9. #查看获取的镜像信息
    10. [root@wuyaShare ~]# docker images
    11. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    12. hello-world   latest     feb5d9fea6a5   3 weeks ago     13.3kB
    13. #根据name来删除镜像
    14. [root@wuyaShare ~]# docker rmi hello-world
    15. Untagged: hello-world:latest
    16. Untagged: hello-world@sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
    17. Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
    18. Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
    19. [root@wuyaShare ~]# docker images
    20. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE

    依据ID删除

    1. #获取镜像信息
    2. [root@wuyaShare ~]# docker pull hello-world
    3. Using default tag: latest
    4. latest: Pulling from library/hello-world
    5. 2db29710123e: Pull complete
    6. Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
    7. Status: Downloaded newer image for hello-world:latest
    8. docker.io/library/hello-world:latest
    9. #查看获取的镜像
    10. [root@wuyaShare ~]# docker images
    11. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    12. hello-world   latest     feb5d9fea6a5   3 weeks ago     13.3kB
    13. #根据ID删除镜像
    14. [root@wuyaShare ~]# docker rmi feb5d9fea6a5
    15. Untagged: hello-world:latest
    16. Untagged: hello-world@sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
    17. Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
    18. Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
    19. [root@wuyaShare ~]# docker images
    20. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE

    docker的镜像被容器运行

    如果一个镜像被容器运行了,那么删除镜像的步骤可以总结为:

    • 得到容器的ID信息

    • 删除关联的容器ID

    • 使用name或者ID来删除镜像

    下面通过一个具体的案例来演示它的过程,具体演示过程如下所示:

    1. #查看镜像信息
    2. [root@wuyaShare ~]# docker images
    3. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    4. centos       7.8.2003   afb6fca791e0   17 months ago   203MB
    5. #在容器中运行镜像
    6. [root@wuyaShare ~]# docker run -it centos:7.8.2003 bash
    7. [root@85c221e31867 /]# date
    8. Mon Oct 18 15:00:54 UTC 2021
    9. [root@85c221e31867 /]# exit
    10. exit
    11. #获取运行容器的ID
    12. [root@wuyaShare ~]# docker ps -a
    13. CONTAINER ID   IMAGE             COMMAND   CREATED         STATUS                     PORTS     NAMES
    14. 85c221e31867   centos:7.8.2003   "bash"   11 seconds ago   Exited (0) 6 seconds ago             mystifying_shirley
    15. #删除镜像运行的关联容器的ID信息
    16. [root@wuyaShare ~]# docker rm 85c221e31867
    17. 85c221e31867
    18. #根据name删除镜像
    19. [root@wuyaShare ~]# docker rmi centos:7.8.2003
    20. Untagged: centos:7.8.2003
    21. Untagged: centos@sha256:8540a199ad51c6b7b51492fa9fee27549fd11b3bb913e888ab2ccf77cbb72cc1
    22. Deleted: sha256:afb6fca791e071c66276202f8efca5ce3d3dc4fb218bcddff1bc565d981ddd1e
    23. Deleted: sha256:fb82b029bea0a2a3b6a62a9c1e47e57fae2a82f629b2d1a346da4fc8fb53a0b6

    导出&导入镜像

    在企业案例中,我们往往打包成一个镜像后,就会打包,然后把它发送给其他的同学来使用。那么在本案例中,我主要包一个centos的

    镜像,里面也是安装了vim的编辑器,然后再在另外一个docker的导入和执行。镜像导入导出的场景:异地的办公模式,那么场景就是:

    异地的开发同学写完代码后,然后把自己的应用程序打包成镜像,打包成镜像后,发送给测试同学,测试同学运行镜像(前提是把镜像

    导入中docker中),镜像运行成功后,等于环境部署成功,可以测试通许可以开始测试了。

    开发同学操作:

    1、写代码

    2、写的代码打包成镜像

    3、把镜像导出

    4、发送给测试同学

    测试同学操作:

    1、接收开发发送的镜像

    2、导入镜像到docker中

    3、运行镜像

    4、开始测试

    导出镜像

    在本地的docker中打包一个镜像,然后导出,在容器里面安装了vim的编辑器,详细的过程如下:

    1. #获取镜像
    2. $ docker pull centos:7.8.2003
    3. 7.8.2003: Pulling from library/centos
    4. 9b4ebb48de8d: Pull complete
    5. Digest: sha256:8540a199ad51c6b7b51492fa9fee27549fd11b3bb913e888ab2ccf77cbb72cc1
    6. Status: Downloaded newer image for centos:7.8.2003
    7. docker.io/library/centos:7.8.2003
    8. #在容器中运行镜像
    9. $ docker run -it --rm centos:7.8.2003 bash
    10. #在容器中安装vim编辑器
    11. [root@a1abbe762fa2 /]# yum install vim -y
    12. #安装vim编辑器成功,退出容器
    13. [root@a1abbe762fa2 /]# exit
    14. exit
    15. #导出容器到/var的目录下
    16. $docker image save centos:7.8.2003 -o /Users/***/Desktop/centos.taz

    导入镜像

    把本地的镜像上传到服务器,然后倒入后来进行执行,具体演示它的过程,具体如下:

    1. #从本地把文件上传到Linux的服务器
    2. scp   centos.taz root@47.95.142.233:/root
    3. root@47.95.142.233's password:
    4. centos.taz                                                   100% 201MB   3.4MB/s   00:59
    5. #查看镜像信息
    6. [root@wuyaShare ~]# docker images
    7. REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
    8. #导入镜像信息
    9. [root@wuyaShare ~]# docker image load -i /root/centos.taz
    10. fb82b029bea0: Loading layer 211.1MB/211.1MB
    11. Loaded image: centos:7.8.2003
    12. #查看导入后的镜像信息
    13. [root@wuyaShare ~]# docker images
    14. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    15. centos       7.8.2003   afb6fca791e0   17 months ago   203MB
    16. #运行镜像信息
    17. [root@wuyaShare ~]# docker run -it --rm centos:7.8.2003 bash
    18. [root@4503d78558da /]# date
    19. Mon Oct 18 15:25:13 UTC 2021

    docker容器管理

    docker run 镜像名称 这个过程可以理解为:把镜像文件创建成docker容器的一部分,然后再进行启动。特别需要注意的是:容器内

    的进程必须是前台运行状态,否则容器直接退出。还有一点特别需要提醒的是:docker run 镜像名 如果镜像文件在本地的docker镜

    像中不存在,那么就会在线去下载该镜像的资源信息。docker的容器启动成功后,使用docker ps -a可以查看容器的ID记录信息。

    下面详细的演示这部分的应用:

    1. #运行docker的镜像信息
    2. [root@wuyaShare ~]# docker run -it centos:7.8.2003 bash
    3. [root@b8c896d0a0d6 /]# date
    4. Wed Oct 20 08:42:46 UTC 2021
    5. [root@b8c896d0a0d6 /]# exit
    6. exit
    7. #查看运行的docker镜像的容器记录信息
    8. [root@wuyaShare ~]# docker ps -a | grep centos
    9. b8c896d0a0d6   centos:7.8.2003   "bash"                   13 seconds ago   Exited (0) 7 seconds ago

    容器命令

    在运行镜像中,后面需要带一些指令的信息,这些指令的信息具体汇总为如下:

    • -d:后台运行

    • -it:交互式命令(bash)

    • --rm:容器挂掉后自动被删除

    • --name:给容器起一个名字

    • -p:端口映射

    详细详细的演示下各个指令的具体使用,具体如下:

    1. #运行容器的指令
    2. docker run -it --rm --name centos.7.8.2003 centos:7.8.2003
    3. #查看到的容器记录的信息
    4. docker ps -a | grep centos
    5. 360df4b98625   centos:7.8.2003   "/bin/bash"     10 seconds ago   Up 9 seconds   centos.7.8.2003

    容器信息查看

    一个容器执行后,我们可以使用如下的指令查看容器的详细信息,具体指令为:

    1. #运行hello-world的容器
    2. [root@wuyaShare ~]# docker run hello-world
    3. Unable to find image 'hello-world:latest' locally
    4. latest: Pulling from library/hello-world
    5. #查看运行容器的记录信息
    6. [root@wuyaShare ~]# docker ps -a | grep hello-world
    7. b87b70fd99b5   hello-world   "/hello"     11 seconds ago   Exited (0) 11 seconds ago keen_dewdney
    8. #查看容器的详细记录信息
    9. [root@wuyaShare ~]# docker container inspect b87b70fd99b5
    10. #备注,执行后,就会显示该容器的详细的信息

    log查看

    在docker容器中,查看容器的日志信息的命令为:

    docker logs  -f  容器ID 

    下面详细的演示下这部分的使用,具体为:

    1. docker run -it --rm centos:7.8.2003 bash
    2. [root@51e2db607d0c /]#
    3. #查询到容器的ID信息
    4. docker ps -a | grep centos
    5. 51e2db607d0c   centos:7.8.2003   "bash"                   16 seconds ago   Up 14 seconds                         great_kepler
    6. #查看容器的日志信息
    7. docker logs -f 51e2db607d0c
    8. #在容器中操作的输出,就会显示到日志中,具体容器的操作如下:
    9. [root@51e2db607d0c /]# echo "Hello Docker!"
    10. bash: !": event not found
    11. [root@51e2db607d0c /]# date
    12. Wed Oct 20 08:55:41 UTC 2021
    13. [root@51e2db607d0c /]# touch index.py
    14. #日志文件显示的信息:
    15. [root@wuyaShare ~]# docker logs -f 51e2db607d0c
    16. [root@51e2db607d0c /]# echo "Hello Docker!"
    17. bash: !": event not found
    18. [root@51e2db607d0c /]# date
    19. Wed Oct 20 08:55:41 UTC 2021
    20. [root@51e2db607d0c /]# touch index.py
    21. #如果只显示最新的5条日志信息,那么执行的命令为:
    22. docker logs 51e2db607d0c | tail -5

    容器提交

    在一个容器里面,比如centos里面安装了vim的编辑器,如果没进行提交,那么下次进入还需要安装vim的编辑器,这其实是非常不友

    好的,我们更加想要的是一个容器里面安装对应的应用程序后,下次进入还是存在的,但是前提条件是需要提交,下面详细的演示下

    这部分的应用,具体步骤如下:

    • 运行容器

      1. [root@wuyaShare ~]# docker run -it --rm centos:7.8.2003 bash
      2. [root@b237640cd900 /]# vim
      3. bash: vim: command not found
    • 在容器里面安装应用程序

      1. #安装vim的编辑器
      2. [root@b237640cd900 /]# yum install vim -y
    • 查询到容器的ID信息

      1. [root@wuyaShare ~]# docker ps -a | grep centos
      2. b237640cd900   centos:7.8.2003   "bash" 2 minutes ago   Up 2 minutes quizzical_kalam
    • 提交容器

      1. [root@wuyaShare ~]# docker commit b237640cd900 centos_vim
      2. sha256:bd0cd838f38d18dd8a840d9c0267b0ed2b5400cbb42c1267afcda4fbc36b8b2d
      3. #提交容器的时候,切记一定要保持运行的容器是不能退出的
    • 退出容器,查看镜像信息,就会显示最新提交的centos_vim

      1. [root@wuyaShare ~]# docker images | grep centos
      2. centos_vim   latest     bd0cd838f38d   About a minute ago   402MB
      3. centos       7.8.2003   afb6fca791e0   17 months ago       203MB
    • 进入到容器centos_vim,就会显示出vim的编辑器

      1. [root@wuyaShare ~]# docker run -it --rm centos_vim bash
      2. [root@a46fc0e60c71 /]# vim

    容器停止&启动

    容器运行后,可以启动容器,也是可以停止容器,以及查看容器的端口信息,下面还是以nginx的镜像信息为案例,来演示下这部分。

    启动&停止容器

    下面以nginx的镜像为案例来演示它的具体容器的停止和启动的演示,具体命令信息如下:

    1. #运行镜像容器信息
    2. [root@wuyaShare ~]# docker run -d -p 80:80 nginx
    3. dc42dd7323a3b5de103f679378cb3f3288ed7e623c175acbd6420a68e7fb3bd4
    4. #查看容器记录ID的信息
    5. [root@wuyaShare ~]# docker ps -a
    6. CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS               NAMES
    7. dc42dd7323a3   nginx     "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp   brave_khayyam
    8. #查看容器的端口
    9. [root@wuyaShare ~]# docker port dc42dd7323a3
    10. 80/tcp -> 0.0.0.0:80
    11. #停止容器
    12. [root@wuyaShare ~]# docker stop dc42dd7323a3
    13. dc42dd7323a3
    14. [root@wuyaShare ~]# docker port dc42dd7323a3
    15. #启动容器,启动容器后,就可以查看容器的ID的信息了
    16. [root@wuyaShare ~]# docker start dc42dd7323a3
    17. dc42dd7323a3
    18. [root@wuyaShare ~]# docker port dc42dd7323a3
    19. 80/tcp -> 0.0.0.0:80

    容器交互式

    在容器的管理中,也是可以通过exec的命令方式来查看容器交互式的命令信息,涉及到的命令信息以及交互具体如下:

    1. #运行容器信息
    2. [root@wuyaShare ~]# docker run -it --rm centos:7.8.2003 bash
    3. [root@3466f8fe336c /]# touch index.txt
    4. #通过exec命令的方式进入到交互式的命令信息
    5. [root@wuyaShare ~]# docker exec -it 3466f8fe336c bash
    6. [root@3466f8fe336c /]# ls
    7. anaconda-post.log dev home       lib   media opt   root sbin sys usr
    8. bin     etc index.txt lib64 mnt   proc run   srv   tmp var

    Dockerfile实战

    Dockerfile概述

    使用dockerfile主要是用于构建docker镜像文件信息,相当于一个脚本,通过dockerfile自己的指令,来构建软件依赖,文件依赖,

    存储等等情况。

    Dockerfile使用

    在docker中创建镜像可以分为两个方法,这两个方法具体可以总结为如下:

    • 手动修改容器内容,然后docker commit提交容器为新的镜像文件

    • 通过在dockerfile中定义一系列的命令和参数构成的脚本,然后这些命令应用于基础镜像,依次添加层,最终生成一个新的镜像,这样就极大的简化了部署的工作。

    Dockerfile组成部分

    dockerfile的主要组成部分可以汇总为:

    • 基础镜像信息

    • 制作镜像操作指令 RUM

    • 容器启动时执行指令CMD["/bin/bash"]

    Dockerfile指令

    Dockerfile基本指令

    dockerfile的指令主要可以汇总为如下,使用dockerfile的前提是学习会这些指令的基本使用,具体如下:

    • FROM #指令基础镜像

    • MAINTAINER #指定维护者信息,也可以没有

    • RUN #具体执行的操作,在命令前面加上RUN即可

    • ADD #COPY文件,会自动进行解压

    • WORKDIR #设置当前工作目录

    • VOLUME #设置卷,挂载主机目录

    • EXPOSE #指定对外的端口

    • CMD #指定容器启动后需要干的事情

    • COPY #复制文件

    • ENV #环境变量

    • ENTRYPOINT #容器启动后执行的命令

    Dockerfile入门

    初玩Dockerfile

    容器能够运行的前提是必须在容器内,有一个进程在前台运行,该容器内,比如是MySQL,那么也就是说有MySQL正在前台运行。下

    面演示一个Linux中调用变量的案例应用,具体Dockerfile文件信息如下所示:

    1. FROM centos:7.8.2003
    2. MAINTAINER 无涯
    3. #定义一个变量,变量的值是Docker
    4. ENV name Docker!
    5. #运行程序,输出变量的值
    6. ENTRYPOINT echo "Hello $name"

    下面详细的演示构建Dockerfile的过程信息,构建的命令以及构建的过程具体如下;

    1. #构建Dockerfile的镜像信息
    2. docker build .
    3. #执行如上构建的指令后,输出如下的信息
    4. Sending build context to Docker daemon 15.87kB
    5. Step 1/4 : FROM centos:7.8.2003
    6. 7.8.2003: Pulling from library/centos
    7. 9b4ebb48de8d: Pull complete
    8. Digest: sha256:8540a199ad51c6b7b51492fa9fee27549fd11b3bb913e888ab2ccf77cbb72cc1
    9. Status: Downloaded newer image for centos:7.8.2003
    10. ---> afb6fca791e0
    11. Step 2/4 : MAINTAINER 无涯
    12. ---> Running in 2bfd2fad4751
    13. Removing intermediate container 2bfd2fad4751
    14. ---> 71dad12c4110
    15. Step 3/4 : ENV name Docker
    16. ---> Running in f70dc098790d
    17. Removing intermediate container f70dc098790d
    18. ---> 3c5b5a752709
    19. Step 4/4 : ENTRYPOINT echo "Hello $name"
    20. ---> Running in cf39ef605a9a
    21. Removing intermediate container cf39ef605a9a
    22. ---> 2e70823e2d4a
    23. Successfully built 2e70823e2d4a

    构建镜像成功后,可以查看该镜像信息以及运行该容器,执行的结果就会显示“Hello Docker”,具体见如下的操作过程:

    1. #查看镜像的信息
    2. [root@wuyaShare learnDocker]# docker images
    3. REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    4. <none>       <none>     2e70823e2d4a   4 minutes ago   203MB
    5. centos       7.8.2003   afb6fca791e0   17 months ago   203MB
    6. #修改镜像的名称
    7. [root@wuyaShare learnDocker]# docker tag 2e70823e2d4a centos_hello
    8. #查看修改后的镜像名称
    9. [root@wuyaShare learnDocker]# docker images
    10. REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
    11. centos_hello   latest     2e70823e2d4a   5 minutes ago   203MB
    12. centos         7.8.2003   afb6fca791e0   17 months ago   203MB
    13. #运行镜像以及显示镜像运行后的信息
    14. [root@wuyaShare learnDocker]# docker run -it --rm centos_hello bash
    15. Hello Docker

    Dockerfile实战

    部署Nginx

    下面显示部署nginx的服务,期望部署是启动容器后,可以访问它的地址显示的信息。涉及到的nginx的Dockerfile的文件内容为:

    1. FROM nginx
    2. MAINTAINER 无涯
    3. RUN echo "Hi, This Is A Docker Continer WebSite" > /usr/share/nginx/html/index.html
    4. EXPOSE 80

    下来开始构建该Dockerfile的文件,主要信息如下:

    1. #构建docker镜像的信息
    2. docker build -t nginx_container .
    3. #构建docker镜像输出的信息
    4. Sending build context to Docker daemon 15.87kB
    5. Step 1/4 : FROM nginx
    6. latest: Pulling from library/nginx
    7. Digest: sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
    8. Status: Downloaded newer image for nginx:latest
    9.   ---> 87a94228f133
    10. Step 2/4 : MAINTAINER 无涯Share
    11.   ---> Running in 99e2f6164553
    12. Removing intermediate container 99e2f6164553
    13.   ---> dd9da530d248
    14. Step 3/4 : RUN echo "Hi, This Is A Docker Continer Website" > /usr/share/nginx/html/index.html
    15.   ---> Running in fcf2344a86a1
    16. Removing intermediate container fcf2344a86a1
    17.   ---> ca224dc81b9a
    18. Step 4/4 : EXPOSE 80
    19.   ---> Running in 73a7d4fa90d1
    20. Removing intermediate container 73a7d4fa90d1
    21.   ---> 040e989b549b
    22. Successfully built 040e989b549b
    23. Successfully tagged nginx_container:latest

    下来运行该docker的镜像,具体详细过程如下所示:

    1. #查看docker镜像的信息
    2. [root@wuyaShare learnDocker]# docker image ls | grep nginx
    3. nginx_container   latest     040e989b549b   3 minutes ago   133MB
    4. nginx             latest     87a94228f133   9 days ago       133MB
    5. #运行docker容器的信息
    6. [root@wuyaShare learnDocker]# docker run -it --rm -d -p 80:80 nginx_container
    7. b7a7dddbfb2303a2b07dd5cd1f9bfb07cf5d0e1c8e9130ac94c435b05a1055ec
    8. #查看docker容器的记录
    9. [root@wuyaShare learnDocker]# docker ps -a | grep nginx
    10. b7a7dddbfb23   nginx_container   "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds   0.0.0.0:80->80/tcp   modest_pike
    11. #查看容器端口
    12. [root@wuyaShare learnDocker]# docker port b7a7dddbfb23
    13. 80/tcp -> 0.0.0.0:80
    14. #验证nginx服务的可用行
    15. [root@wuyaShare learnDocker]# curl http://localhost:80
    16. Hi, This Is A Docker Continer Website

    部署Python

    本案例主要使用Python语言,使用的WEB框架是Flask,然后来实现一个网站的部署过程,也就是说写好程序代码好后,开发dockerfile

    的代码,执行以及生成镜像,其他人基于该镜像,docker run就可以在电脑跑起来对应的应用程序。

    Flask代码

    1. from flask import  Flask,make_response,jsonify,abort,request
    2. from flask_restful import  Api,Resource
    3. from flask_httpauth import  HTTPBasicAuth
    4. from flask import Flask
    5. from flask_jwt import JWT, jwt_required, current_identity
    6. from werkzeug.security import safe_str_cmp
    7. app=Flask(__name__)
    8. app.debug = True
    9. app.config['SECRET_KEY'] = 'super-secret'
    10. api=Api(app=app)
    11. auth=HTTPBasicAuth()
    12. @auth.get_password
    13. def get_password(name):
    14.   if name=='admin':
    15.      return 'admin'
    16. @auth.error_handler
    17. def authoorized():
    18.   return make_response(jsonify({'msg':"请认证"}),403)
    19. books=[
    20.   {'id':1,'author':'wuya','name':'Python接口自动化测试实战','done':True},
    21.   {'id':2,'author':'无涯','name':'Selenium3自动化测试实战','done':False}
    22. ]
    23. class User(object):
    24.   def __init__(self, id, username, password):
    25.      self.id = id
    26.      self.username = username
    27.      self.password = password
    28.   def __str__(self):
    29.      return "User(id='%s')" % self.id
    30. users = [
    31.   User(1, 'wuya', 'asd888'),
    32.   User(2, 'stage', 'asd888'),
    33.   User(3,'line','asd888')
    34. ]
    35. username_table = {u.username: u for u in users}
    36. userid_table = {u.id: u for u in users}
    37. def authenticate(username, password):
    38.   user = username_table.get(username, None)
    39.   if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
    40.      return user
    41. def identity(payload):
    42.   user_id = payload['identity']
    43.   return userid_table.get(user_id, None)
    44. jwt = JWT(app, authenticate, identity)
    45. class Books(Resource):
    46.   # decorators = [auth.login_required]
    47.   #decorators=[jwt_required()]
    48.   def get(self):
    49.      return jsonify({'status':0,'msg':'ok','datas':books})
    50.   def post(self):
    51.      if not request.json:
    52.         return jsonify({'status':1001,'msg':'请求参数不是JSON的数据,请检查,谢谢!'})
    53.      else:
    54.         book = {
    55.            'id': books[-1]['id'] + 1,
    56.            'author': request.json.get('author'),
    57.            'name': request.json.get('name'),
    58.            'done': True
    59.         }
    60.         books.append(book)
    61.         return {'status':1002,'msg': '添加书籍成功','datas':book}
    62.         # return jsonify({'status':1002,'msg': '添加书籍成功','datas':book}, 201)
    63. class Book(Resource):
    64.   # decorators = [auth.login_required]
    65.   decorators = [jwt_required()]
    66.   def get(self,book_id):
    67.      book = list(filter(lambda t: t['id'] == book_id, books))
    68.      if len(book) == 0:
    69.         return jsonify({'status': 1003, 'msg': '很抱歉,您查询的书的信息不存在'})
    70.      else:
    71.         return jsonify({'status': 0, 'msg': 'ok', 'datas': book})
    72.   def put(self,book_id):
    73.      book = list(filter(lambda t: t['id'] == book_id, books))
    74.      if len(book) == 0:
    75.         return jsonify({'status': 1003, 'msg': '很抱歉,您查询的书的信息不存在'})
    76.      elif not request.json:
    77.         return jsonify({'status': 1001, 'msg': '请求参数不是JSON的数据,请检查,谢谢!'})
    78.      elif 'author' not in request.json:
    79.         return jsonify({'status': 1004, 'msg': '请求参数author不能为空'})
    80.      elif 'name' not in request.json:
    81.         return jsonify({'status': 1005, 'msg': '请求参数name不能为空'})
    82.      elif 'done' not in request.json:
    83.         return jsonify({'status': 1006, 'msg': '请求参数done不能为空'})
    84.      elif type(request.json['done'])!=bool:
    85.         return jsonify({'status': 1007, 'msg': '请求参数done为bool类型'})
    86.      else:
    87.         book[0]['author'] = request.json.get('author', book[0]['author'])
    88.         book[0]['name'] = request.json.get('name', book[0]['name'])
    89.         book[0]['done'] = request.json.get('done', book[0]['done'])
    90.         return jsonify({'status': 1008, 'msg': '更新书的信息成功', 'datas': book})
    91.   def delete(self,book_id):
    92.      book = list(filter(lambda t: t['id'] == book_id, books))
    93.      if len(book) == 0:
    94.         return jsonify({'status': 1003, 'msg': '很抱歉,您查询的书的信息不存在'})
    95.      else:
    96.         books.remove(book[0])
    97.         return jsonify({'status': 1009, 'msg': '删除书籍成功'})
    98. api.add_resource(Books,'/v1/api/books')
    99. api.add_resource(Book,'/v1/api/book/')
    100. if __name__ == '__main__':
    101.   app.run(debug=True,host='0.0.0.0')

    Dockerfile文件

    这部分的Dockerfile文件的信息具体为:

    1. FROM centos:7.8.2003
    2. MAINTAINER 无涯
    3. #下载yum
    4. RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo;
    5. RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo;
    6. #安装Python环境
    7. RUN yum install python3-devel python3-pip -y
    8. #安装flask库
    9. RUN pip3 install -i https://pypi.douban.com/simple   flask
    10. RUN pip3 install flask_restful
    11. RUN pip3 install flask_httpauth
    12. RUN pip3 install flask_jwt
    13. #复制文件到容器目录
    14. COPY app.py /opt
    15. #切换目录
    16. WORKDIR /opt
    17. #启动服务
    18. EXPOSE 5000
    19. CMD ["python3","app.py"]

    构建镜像

    下来构建镜像,指令为:

    1. docker build -t flask_web .
    2. #执行构建后,部分输出信息
    3. ---> 654d11509ebb
    4. Step 7/10 : COPY app.py /opt
    5. ---> cf4f278d900b
    6. Step 8/10 : WORKDIR /opt
    7. ---> Running in 8dd988a4bf0d
    8. Removing intermediate container 8dd988a4bf0d
    9. ---> 6866628f23f7
    10. Step 9/10 : EXPOSE 5000
    11. ---> Running in 131bded2d074
    12. Removing intermediate container 131bded2d074
    13. ---> b7876c74dae4
    14. Step 10/10 : CMD ["python3","app.py"]
    15. ---> Running in 498f6c4ab849
    16. Removing intermediate container 498f6c4ab849
    17. ---> 89f07acc5d97
    18. Successfully built 89f07acc5d97
    19. Successfully tagged flask_web:latest

    运行容器

    构建成功后,下来运行容器,具体详细过程如下:

    1. [root@wuyaShare learnDocker]# docker run -it --rm -p 5000:5000 flask_web
    2.   * Serving Flask app 'app' (lazy loading)
    3.   * Environment: production
    4.   WARNING: This is a development server. Do not use it in a production deployment.
    5.   Use a production WSGI server instead.
    6.   * Debug mode: on
    7.   * Running on http://172.18.0.2:5000/ (Press CTRL+C to quit)
    8.   * Restarting with stat
    9.     * Debugger is active!
    10.   * Debugger PIN: 132-410-186

    验证容器

    测试代码具体如下,模块名称为test_app.py,源码为:

    1. import requests
    2. def test_login():
    3.   r=requests.post(
    4.      url='http://172.18.0.2:5000/auth',
    5.      json={"username":"wuya","password":"asd888"})
    6.   assert r.status_code==200

    验证结果信息如下:

    1. python3 -m pytest -v test_app.py
    2. ============================== test session starts ===============================
    3. platform linux -- Python 3.7.1, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- /usr/python37/bin/python3
    4. collected 1 item
    5. test_app.py::test_login PASSED

    服务端收到的请求信息具体如下:

    172.18.0.1 - - [21/Oct/2021 10:55:50] "POST /auth HTTP/1.1" 200 -

    部署SpringBoot

    项目结构

    创建SpringBoot的项目后,需要在src/main的目录下创建docker的文件夹,专门来管理Dockerfile的文件信息,整体项目结构信息如下:

    Dockerfile文件

    下来在docker文件夹里面创建Dockerfile的文件,文件内容具体如下:

    1. FROM java:8
    2. MAINTAINER 无涯
    3. VOLUME /tmp
    4. RUN mkdir /app
    5. COPY app-0.0.1-SNAPSHOT.jar /app/app.jar
    6. WORKDIR /app
    7. EXPOSE 8080
    8. CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

    Maven依赖插件

    在Spring Boot中,需要在插件依赖中新增关于Docker的插件,详细的信息具体如下:

    1. <!--Docker Maven依赖插件-->
    2. <plugin>
    3.    <groupId>com.spotify</groupId>
    4.    <artifactId>docker-maven-plugin</artifactId>
    5.    <configuration>
    6.        <imageName>${project.name}:${project.version}</imageName>
    7.        <!--Dockerfile文件存储目录-->
    8.        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
    9.        <skipDockerBuild>false</skipDockerBuild>
    10.        <resources>
    11.            <resource>
    12.                <directory>${project.build.directory}</directory>
    13.                <include>${project.build.finalName}.jar</include>
    14.            </resource>
    15.        </resources>
    16.    </configuration>
    17. </plugin>

    Maven构建

    下来开始构建,构建的命令具体为:

    1. mvn clean package -Dmaven.test.skip=true   docker:build
    2. #执行如上的构建命令后,输出的信息具体如下
    3. Step 1/8 : FROM java:8
    4. ---> d23bdf5b1b1b
    5. Step 2/8 : MAINTAINER 无涯
    6. ---> Running in 5f788f4733bf
    7. Removing intermediate container 5f788f4733bf
    8. ---> 628dd9a79fb9
    9. Step 3/8 : VOLUME /tmp
    10. ---> Running in 39c614cd1c00
    11. Removing intermediate container 39c614cd1c00
    12. ---> ece8a35f07d9
    13. Step 4/8 : RUN mkdir /app
    14. ---> Running in 77d1ccea2ab3
    15. Removing intermediate container 77d1ccea2ab3
    16. ---> d28fbddcd816
    17. Step 5/8 : COPY app-0.0.1-SNAPSHOT.jar /app/app.jar
    18. ---> 16fd34225aa2
    19. Step 6/8 : WORKDIR /app
    20. ---> Running in 2d4098618383
    21. Removing intermediate container 2d4098618383
    22. ---> 88888be40520
    23. Step 7/8 : EXPOSE 8081
    24. ---> Running in f1452b177cfe
    25. Removing intermediate container f1452b177cfe
    26. ---> 968944b64337
    27. Step 8/8 : CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
    28. ---> Running in 07dc64c0c19f
    29. Removing intermediate container 07dc64c0c19f
    30. ---> e6e04292173f
    31. ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
    32. Successfully built e6e04292173f
    33. Successfully tagged app:0.0.1-SNAPSHOT
    34. [INFO] Built app:0.0.1-SNAPSHOT
    35. [INFO] ------------------------------------------------------------------------
    36. [INFO] BUILD SUCCESS
    37. [INFO] ------------------------------------------------------------------------
    38. [INFO] Total time: 11.349 s
    39. [INFO] Finished at: 2021-12-27T16:50:10+08:00
    40. [INFO] ------------------------------------------------------------------------

    镜像验证

    构建成功后,使用docker images就可以看到构建成功的镜像信息,具体如下:

    1. docker images
    2. REPOSITORY         TAG                 IMAGE ID           CREATED             SIZE
    3. app                 0.0.1-SNAPSHOT     e6e04292173f       4 hours ago         664MB

    下来开始运行镜像文件,运行的命令为:

    1. docker run --rm -p8081:8081 e6e04292173f
    2. #执行如上信息后,输出结果信息如下:
    3. .   ____         _           __ _ _
    4. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    5. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    6. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    7. ' |____| .__|_| |_|_| |_\__, | / / / /
    8. =========|_|==============|___/=/_/_/_/
    9. :: Spring Boot ::               (v2.6.2)
    10. 2021-12-27 13:49:43.389 INFO 1 --- [           main] com.example.app.AppApplication           : Starting AppApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on 22a0a65bbe46 with PID 1 (/app/app.jar started by root in /app)
    11. 2021-12-27 13:49:43.398 INFO 1 --- [           main] com.example.app.AppApplication           : No active profile set, falling back to default profiles: default
    12. 2021-12-27 13:49:47.042 INFO 1 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8081
    13. 2021-12-27 13:49:47.077 INFO 1 --- [           main] com.example.app.AppApplication           : Started AppApplication in 4.99 seconds (JVM running for 6.14)

    下来在PostMan中访问地址,就可以看到访问网络请求后的响应结果信息。

    Docker-compose启动镜像

    Docker-compose

    依据前面的SpringBoot的项目,在src/main/docker的文件夹下创建docker-compose.yml的文件,在该文件里面定义容器的网络,端口,

    被启动的镜像信息,文件详细的信息如下:

    1. version: '3.2'
    2. services:
    3. app:
    4.   image: app:0.0.1-SNAPSHOT
    5.   hostname: localhost
    6.   ports:
    7.   - "8081:8081"
    8.   networks:
    9.     - mynetwork
    10. networks:
    11. mynetwork:
    12.   external: true

    创建网络

    下来创建网络,创建网络的目的是为了容器与宿主机是在同一个网络的模式下,一般我们创建的是Bridge,创建的命令为:

    docker network create -o parent=en0 --driver=bridge --subnet=192.168.3.157/24 --gateway=192.168.3.157 mynetwork

    网络创建成功后,就可以查看到目前的网络情况中存在mynetwork,具体如下:

    1. docker network ls
    2. NETWORK ID         NAME                       DRIVER             SCOPE
    3. 5e0d06b35341       bridge                     bridge             local
    4. 34f731bed1dc       host                       host               local
    5. 4b5926f1e44d       mynetwork                 bridge             local

    启动镜像

    下来在src/main/docker的目录下,执行如下命令来启动镜像信息,具体如下:

    1. docker-compose up -d
    2. Creating docker_app_1 ... done
    3. #获取容器的ID信息
    4. docker ps -a
    5. CONTAINER ID       IMAGE               COMMAND                 CREATED             STATUS             PORTS                   NAMES
    6. caee578c4876       app:0.0.1-SNAPSHOT   "java -Djava.securit…"   43 minutes ago     Up 43 minutes       0.0.0.0:8081->8081/tcp   docker_app_1
    7. #查看日志信息
    8. docker logs -f caee578c4876
    9. .   ____         _           __ _ _
    10. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    11. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    12. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    13. ' |____| .__|_| |_|_| |_\__, | / / / /
    14. =========|_|==============|___/=/_/_/_/
    15. :: Spring Boot ::               (v2.6.2)
    16. 2021-12-29 01:52:37.085 INFO 1 --- [           main] com.example.app.AppApplication           : Starting AppApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on localhost with PID 1 (/app/app.jar started by root in /app)
    17. 2021-12-29 01:52:37.090 INFO 1 --- [           main] com.example.app.AppApplication           : No active profile set, falling back to default profiles: default
    18. 2021-12-29 01:52:39.280 INFO 1 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8081
    19. 2021-12-29 01:52:39.291 INFO 1 --- [           main] com.example.app.AppApplication           : Started AppApplication in 2.973 seconds (JVM running for 4.082)

    停止镜像

    1. docker-compose down
    2. Stopping docker_app_1 ... done
    3. Removing docker_app_1 ... done
    4. Network mynetwork is external, skipping

    更新配置文件

    更新配置文件application.yaml,把服务的端口修改为8081

    1. server:
    2. port: 8081
    3. servlet:
    4.   session:
    5.     timeout: 30
    6. tomcat:
    7.   uri-encoding: UTF-8

    自动化部署&验证

    下面我们可以把自动构建镜像,以及自动启动服务,和自动化验证测试服务的过程,完全结合Jenkins持续集成的流水线,完全实现自动化的

    部署和过程。

    pipeline

    在Jenkins持续集成的工具里面创建Pipeline的项目,设计到的脚本具体如下:

    1. pipeline{
    2.   agent any
    3.   stages{
    4.       stage('build the image'){
    5.           steps{
    6.               sh '''cd /Applications/code/workSpace/data/app
    7.               mvn clean package -Dmaven.test.skip=true   docker:build'''
    8.           }
    9.       }
    10.       stage('run the container'){
    11.           steps{
    12.               sh '''cd /Applications/code/workSpace/data/app/src/main/docker
    13.               docker-compose up -d '''
    14.           }
    15.       }
    16.       stage('smoke test'){
    17.           steps{
    18.               sh '''cd /Applications/code/workSpace/data/app/src/main/docker
    19.               sleep 10s
    20.               python3 -m pytest -v test_springboot.py'''
    21.           }
    22.       }
    23.   }
    24. }

    下来我们开始构建镜像,其实我们构建的过程,第一步主要就是打包镜像,第二步就是自动化测试的启动镜像,第三个步骤就是验证部署的服

    务这部分,这部分也是可以理解为一个冒烟测试的过程。具体构建后输出的结果信息如下:

    1. Successfully tagged app:0.0.1-SNAPSHOT
    2. [INFO] Built app:0.0.1-SNAPSHOT
    3. [INFO] ------------------------------------------------------------------------
    4. [INFO] BUILD SUCCESS
    5. [INFO] ------------------------------------------------------------------------
    6. [INFO] Total time: 14.678 s
    7. [INFO] Finished at: 2022-01-02T23:07:35+08:00
    8. [INFO] ------------------------------------------------------------------------
    9. [Pipeline] }
    10. [Pipeline] // stage
    11. [Pipeline] stage
    12. [Pipeline] { (run the container)
    13. [Pipeline] sh
    14. + cd /Applications/code/workSpace/data/app/src/main/docker
    15. + docker-compose up -d
    16. Creating docker_app_1 ...
    17. •[1A•[2K
    18. Creating docker_app_1 ... •[32mdone•[0m

     

  • 相关阅读:
    玩转 Scrapy 框架 (二):Scrapy 架构、Request和Response介绍
    交通物流模型 | 基于双向时空自适应Transformer的城市交通流预测
    2022-07-05 数据结构与算法-散列表、哈希算法
    mysql数据库基础
    admin后台管理
    Faster-RCNN论文解读
    流程梳理有什么价值?如何建立高效的流程管理体系?
    常用的微商话术
    2022年湖北工业大学招生简章之高起专、专升本非全日制学历提升
    【C语言】指针和数组笔试题解析(2)
  • 原文地址:https://blog.csdn.net/csdnchengxi/article/details/126754616