一、使用Docker Compose 部署应用简介

Docker Compose能够在Docker节点上,以单引擎模式(Single-Engine Mode)进行多容器应用的部署和管理。

Docker Compose通过一个声明式的配置文件描述整个应用,使用一条命令完成部署。而当应用部署成功后,还可以通过一系列简单的命令实现对其完整生命周期的管理。甚至,配置文件还可以置于版本控制系统中进行存储和管理。

二、使用Docker Compose 部署应用详解

2.1 Docker Compose背景

2014年Docker公司收购了Orchard公司,并将Fig更名为Docker Compose。命令行工具由fig更名为docker-compose,并自此成为绑定在Docker引擎之上的外部工具。

到今天为至,Docker Compose仍然是一个需要在Docker主机上进行安装的外部python工具。使用它时,首先编写定义多容器(多服务)应用的YAML文件,然后将其交由docker-compose命令处理,Docker Compose就会基于Docker引擎API完成应用的部署。

2.2 安装Docker Compose

参考基于Ubuntu版本和Centos版本安装和卸载Docker Compose进行安装。

2.3 Compose文件

Docker Compose使用YAML文件来定义多服务的应用。YAML是JSON的一个子集。

Docker Compose默认使用文件名docker-compose.yml。也可以使用-f参数指定具体文件。

2.3.1 Compose文件内容

以下Compose文件定义了一个包含两个服务(web-fe和redis)的小型Flask应用。该文件能够对访问者进行计数并将其保存到Redis的简单的Web服务。

Compose文件内容如下:

version: "3.5"
services:
  web-fe:
    build: .
    command: python app.py
    ports:
      - target: 5000
        published: 5000
    networks:
      - counter-net
    volumes:
      - type: volume
        source: counter-vol
        target: /code
  redis:
    image: "redis:alpine"
    networks:
      counter-net:

networks:
  counter-net:

文件内容包含4个一级key:version、services、networks、volumes。 version总是位于文件的第一行,用来定义Compose文件格式(主要是API)的版本; services用于定义不同的应用服务。案例中定义了两个服务,一个名为web-fe的web前端服务以及一个redis的内存数据库服务。 networks用于指引Docker创建新的网络。默认情况下,Docker Compose创建Bridge网络。 volumes用于指引Docker来创建新的卷。

2.3.2 Compose文件深入分析

services定义了两个二级key:web-fe和redis。

web-fe服务定义中,包含以下指令: 1、build 【.】指定Docker基于当前目录(.)下Dockerfile中定义的指令来构建一个新镜像。该镜像会被用于启动该服务的容器。 2、command python app.py指定Docker在容器中执行名为app.py的Python脚本作为主程序。 3、ports 指定Docker将容器内(-target)的5000端口映射到主机(published)的5000端口。表示发送到Docker主机5000端口的流量会被转发到容器的5000端口。容器中的应用监听端口5000。 4、networks 让Docker可以将服务连接到指定的网络上。对于Overlay网络来说,它还需要定义一个attachable标志,这样独立容器才可以连接上。 5、volumes 指定Docker将counter-vol卷(source:)挂载到容器内的/code(target:)。

小结: Docker Compose会调用Docker来为web-fe服务部署一个独立的容器。该容器基于与Compose文件位于同一目录的Dockerfile构建的镜像。基于该镜像启动的容器会运行app.py作为主程序,将5000端口暴露给宿主机,连接到counter-net网络上,并挂载一个卷到/code。

redis服务定义中,包含以下指令: 1、image redis:alpine使得Docker基于redis:alpine镜像启动一个独立的名为redis的容器。这个镜像会被从Docker Hub上拉取下来。 2、networks 配置redis容器连接到counter-net网络。

说明:由于web-fe服务和redis服务都连接到counter-net网络,因此它们可以通过名称解析到对方的地址!!!

三、使用Docker Compose部署应用

3.1 克隆应用代码

1、执行git https://github.com/nigelpoulton/counter-app.git命令从GitHub克隆代码。

root@zq-virtual-machine:/home/zq/Desktop# git clone https://github.com/nigelpoulton/counter-app.git

3.2 查看应用代码

1、执行cd和ls命令查看克隆后的文件内容。

root@zq-virtual-machine:/home/zq/Desktop# cd counter-app/
root@zq-virtual-machine:/home/zq/Desktop/counter-app# ls
app.py              Dockerfile  requirements.txt
docker-compose.yml  README.md

3.3 开始部署应用

1、counter-app/目录视图下执行docker-compose up &命令启动Docker Compose。其中&参数代表日志回显给终端窗口。(-d参数是在后台运行)

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose up & 
[1] 2717
[+] Running 7/7-machine:/home/zq/Desktop/counter-app# 
  redis Pulled                                                          12.1s
    213ec9aee27d Pull complete                                           3.2s
    c99be1b28c7f Pull complete                                           3.4s
    8ff0bb7e55e3 Pull complete                                           3.8s
    dedc3f029c28 Pull complete                                           5.6s
    820ef2f65f1b Pull complete                                           5.8s
    2117f9ecd795 Pull complete                                           6.0s
[+] Building 48.8s (10/10) FINISHED                                            
 => [internal] load build definition from Dockerfile                      0.2s
 => => transferring dockerfile: 147B                                      0.0s
 => [internal] load .dockerignore                                         0.2s
 => => transferring context: 2B                                           0.0s
 => [internal] load metadata for docker.io/library/python:3.6-alpine      5.3s
 => [auth] library/python:pull token for registry-1.docker.io             0.0s
 => [internal] load build context                                         0.1s
 => => transferring context: 30.36kB                                      0.0s
 => [1/4] FROM docker.io/library/python:3.6-alpine@sha256:579978dec46026  4.8s
 => => resolve docker.io/library/python:3.6-alpine@sha256:579978dec46026  0.0s
 => => sha256:579978dec4602646fe1262f02b96371779bfb0294e 1.65kB / 1.65kB  0.0s
 => => sha256:bc3447f6ba006c2268ccab840236cd1e27502a1d91 1.37kB / 1.37kB  0.0s
 => => sha256:3a9e80fa460687aa888ebe54540132662b58a3bb1a 8.08kB / 8.08kB  0.0s
 => => sha256:59bf1c3509f33515622619af21ed55bbe26d24913c 2.82MB / 2.82MB  1.6s
 => => sha256:8786870f287676cca49c1e1e5029467c087ad2 282.15kB / 282.15kB  1.8s
 => => sha256:acb0e804800ed3c10624ddeac73a6ebd3f2d6dbb 10.18MB / 10.18MB  3.2s
 => => extracting sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca  0.2s
 => => sha256:52bedcb3e853dd5d782c46d387f2af404af9ec75c2aba3 229B / 229B  2.4s
 => => sha256:b064415ed3d75cd9bf462c4ea1a29aebe67dfe8fc7 2.21MB / 2.21MB  3.3s
 => => extracting sha256:8786870f287676cca49c1e1e5029467c087ad293b824fcd  0.2s
 => => extracting sha256:acb0e804800ed3c10624ddeac73a6ebd3f2d6dbb03970f0  0.5s
 => => extracting sha256:52bedcb3e853dd5d782c46d387f2af404af9ec75c2aba3a  0.1s
 => => extracting sha256:b064415ed3d75cd9bf462c4ea1a29aebe67dfe8fc76e672  0.4s
 => [2/4] ADD . /code                                                     0.3s
 => [3/4] WORKDIR /code                                                   0.1s
 => [4/4] RUN pip install -r requirements.txt                            36.6s
 => exporting to image                                                    1.1s
 => => exporting layers                                                   1.1s
 => => writing image sha256:48da14617ca06fad52d56e7ef058c591412f0c51b93f  0.0s
 => => naming to docker.io/library/counter-app_web-fe                     0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 4/4
  Network counter-app_counter-net   Created                              0.1s
  Volume "counter-app_counter-vol"  Created                              0.0s
  Container counter-app-redis-1     C...                                 0.5s
  Container counter-app-web-fe-1    Created                              0.6s
Attaching to counter-app-redis-1, counter-app-web-fe-1
counter-app-redis-1   | 1:C 06 Oct 2022 01:49:06.445 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
counter-app-redis-1   | 1:C 06 Oct 2022 01:49:06.445 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
counter-app-redis-1   | 1:C 06 Oct 2022 01:49:06.445 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.446 * monotonic clock: POSIX clock_gettime
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 * Running mode=standalone, port=6379.
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 # Server initialized
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 * Ready to accept connections
counter-app-web-fe-1  |  * Serving Flask app 'app' (lazy loading)
counter-app-web-fe-1  |  * Environment: production
counter-app-web-fe-1  |    WARNING: This is a development server. Do not use it in a production deployment.
counter-app-web-fe-1  |    Use a production WSGI server instead.
counter-app-web-fe-1  |  * Debug mode: on
counter-app-web-fe-1  |  * Running on all addresses.
counter-app-web-fe-1  |    WARNING: This is a development server. Do not use it in a production deployment.
counter-app-web-fe-1  |  * Running on http://172.18.0.2:5000/ (Press CTRL+C to quit)
counter-app-web-fe-1  |  * Restarting with stat
counter-app-web-fe-1  |  * Debugger is active!
counter-app-web-fe-1  |  * Debugger PIN: 355-203-816

说明:默认docker-compose up会查找名为docker-compose.yml或docker-compose.yaml的Compose文件。如果Compose文件是其他文件名,需要-f参数指定文件名。

2、执行docker image ls命令查看Docker Compose创建的镜像、容器、网络和卷。这里需要注意,Docker Compose会将项目名称(counter-app)和Compose文件中定义的资源名称web-fe连起来,作为新构建镜像的名称。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker image ls
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
counter-app_web-fe   latest    48da14617ca0   9 minutes ago   55.5MB
redis                alpine    0139e7020436   13 days ago     28.5MB

3、执行docker container ls命令查看启动容器。观察到,每个容器的名称都以项目名称为前缀。同时,它们还以一个数字为后缀用于标识容器实例序号,这是因为Docker Compose允许扩缩容。其中counter-app_web-fe运行的是应用的WEB前端。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker container ls
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                                       NAMES
9e477d785e98   redis:alpine         "docker-entrypoint.s…"   16 minutes ago   Up 16 minutes   6379/tcp                                    counter-app-redis-1
05757fc563f9   counter-app_web-fe   "python app.py"          16 minutes ago   Up 16 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   counter-app-web-fe-1

4、执行docker network ls命令查看网络。这里观察到生成名为counter-app_counter-net的网络。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker network ls
NETWORK ID     NAME                      DRIVER    SCOPE
7db9b084e73d   bridge                    bridge    local
15c8b59f9c64   counter-app_counter-net   bridge    local
0692b1905fd1   host                      host      local
a096b69b36e5   none                      null      local

5、执行docker volume ls命令查看卷,这里观察到生成名为counter-app_counter-vol的卷。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker volume ls
DRIVER    VOLUME NAME
local     91b2efae88829c2de058292dcd33932db21dae9570557b5d9a5081e01fb102b5
local     counter-app_counter-vol

3.4 验证部署应用

1、打开Docker主机浏览器输入【dock-id_addres:5000】查看应用的运行效果。 2、单击浏览器的刷新按钮,观察到计数会增加。如果使用docker-compose up &命令启动Docker Compose,终端窗口会看到包含HTTP响应码200的日志。每次刷新都会打印日志。

验证Docker Compose部署应用

四、使用Docker Compose管理应用

4.1 使用Docker Compose启动应用

1、Compose文件(compose.yml或compose.yaml)所在目录视图下执行docker-compose up &命令启动Docker Compose。其中&参数代表日志回显给终端窗口。(-d参数是在后台运行)

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose up & 
[1] 2717
[+] Running 7/7-machine:/home/zq/Desktop/counter-app# 
  redis Pulled                                                          12.1s
    213ec9aee27d Pull complete                                           3.2s
    c99be1b28c7f Pull complete                                           3.4s
    8ff0bb7e55e3 Pull complete                                           3.8s
    dedc3f029c28 Pull complete                                           5.6s
    820ef2f65f1b Pull complete                                           5.8s
    2117f9ecd795 Pull complete                                           6.0s
[+] Building 48.8s (10/10) FINISHED                                            
 => [internal] load build definition from Dockerfile                      0.2s
 => => transferring dockerfile: 147B                                      0.0s
 => [internal] load .dockerignore                                         0.2s
 => => transferring context: 2B                                           0.0s
 => [internal] load metadata for docker.io/library/python:3.6-alpine      5.3s
 => [auth] library/python:pull token for registry-1.docker.io             0.0s
 => [internal] load build context                                         0.1s
 => => transferring context: 30.36kB                                      0.0s
 => [1/4] FROM docker.io/library/python:3.6-alpine@sha256:579978dec46026  4.8s
 => => resolve docker.io/library/python:3.6-alpine@sha256:579978dec46026  0.0s
 => => sha256:579978dec4602646fe1262f02b96371779bfb0294e 1.65kB / 1.65kB  0.0s
 => => sha256:bc3447f6ba006c2268ccab840236cd1e27502a1d91 1.37kB / 1.37kB  0.0s
 => => sha256:3a9e80fa460687aa888ebe54540132662b58a3bb1a 8.08kB / 8.08kB  0.0s
 => => sha256:59bf1c3509f33515622619af21ed55bbe26d24913c 2.82MB / 2.82MB  1.6s
 => => sha256:8786870f287676cca49c1e1e5029467c087ad2 282.15kB / 282.15kB  1.8s
 => => sha256:acb0e804800ed3c10624ddeac73a6ebd3f2d6dbb 10.18MB / 10.18MB  3.2s
 => => extracting sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca  0.2s
 => => sha256:52bedcb3e853dd5d782c46d387f2af404af9ec75c2aba3 229B / 229B  2.4s
 => => sha256:b064415ed3d75cd9bf462c4ea1a29aebe67dfe8fc7 2.21MB / 2.21MB  3.3s
 => => extracting sha256:8786870f287676cca49c1e1e5029467c087ad293b824fcd  0.2s
 => => extracting sha256:acb0e804800ed3c10624ddeac73a6ebd3f2d6dbb03970f0  0.5s
 => => extracting sha256:52bedcb3e853dd5d782c46d387f2af404af9ec75c2aba3a  0.1s
 => => extracting sha256:b064415ed3d75cd9bf462c4ea1a29aebe67dfe8fc76e672  0.4s
 => [2/4] ADD . /code                                                     0.3s
 => [3/4] WORKDIR /code                                                   0.1s
 => [4/4] RUN pip install -r requirements.txt                            36.6s
 => exporting to image                                                    1.1s
 => => exporting layers                                                   1.1s
 => => writing image sha256:48da14617ca06fad52d56e7ef058c591412f0c51b93f  0.0s
 => => naming to docker.io/library/counter-app_web-fe                     0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 4/4
  Network counter-app_counter-net   Created                              0.1s
  Volume "counter-app_counter-vol"  Created                              0.0s
  Container counter-app-redis-1     C...                                 0.5s
  Container counter-app-web-fe-1    Created                              0.6s
Attaching to counter-app-redis-1, counter-app-web-fe-1
counter-app-redis-1   | 1:C 06 Oct 2022 01:49:06.445 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
counter-app-redis-1   | 1:C 06 Oct 2022 01:49:06.445 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
counter-app-redis-1   | 1:C 06 Oct 2022 01:49:06.445 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.446 * monotonic clock: POSIX clock_gettime
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 * Running mode=standalone, port=6379.
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 # Server initialized
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
counter-app-redis-1   | 1:M 06 Oct 2022 01:49:06.447 * Ready to accept connections
counter-app-web-fe-1  |  * Serving Flask app 'app' (lazy loading)
counter-app-web-fe-1  |  * Environment: production
counter-app-web-fe-1  |    WARNING: This is a development server. Do not use it in a production deployment.
counter-app-web-fe-1  |    Use a production WSGI server instead.
counter-app-web-fe-1  |  * Debug mode: on
counter-app-web-fe-1  |  * Running on all addresses.
counter-app-web-fe-1  |    WARNING: This is a development server. Do not use it in a production deployment.
counter-app-web-fe-1  |  * Running on http://172.18.0.2:5000/ (Press CTRL+C to quit)
counter-app-web-fe-1  |  * Restarting with stat
counter-app-web-fe-1  |  * Debugger is active!
counter-app-web-fe-1  |  * Debugger PIN: 355-203-816

说明:默认docker-compose up会查找名为docker-compose.yml或docker-compose.yaml的Compose文件。如果Compose文件是其他文件名,需要-f参数指定文件名。

2、Compose文件(compose.yml或compose.yaml)所在目录视图下执行docker-compose up -d命令启动Docker Compose。其中-d参数代表在后台启动。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose up -d
[+] Running 3/3
  Network counter-app_counter-net  Created                               0.1s
  Container counter-app-redis-1    St...                                 2.7s
  Container counter-app-web-fe-1   S...                                  3.2s

4.2 使用Docker Compose关闭应用

1、执行docker-compose down命令停止和关闭应用。这里注意,会留下镜像、卷和源码。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose down
[+] Running 0/0
  Container counter-app-redis-1   Sto...                                 0.1s
[+] Running 0/2unter-app-web-fe-1  St...                                  0.1s
  Container counter-app-redis-1   Sto...                                 0.2si  Container counter-app-web-fe-1  St...                                  0.2s
counter-app-redis-1   | 1:M 06 Oct 2022 02:32:39.369 # User requested shutdown...
counter-app-redis-1   | 1:M 06 Oct 2022 02:32:39.369 * Saving the final RDB snapshot before exiting.
[+] Running 0/2is-1   | 1:M 06 Oct 2022 02:32:39.371 * DB saved on disk
  Container counter-app-redis-1   Sto...                                 0.5sx[+] Running 1/2unter-app-web-fe-1  St...                                  0.5s
[+] Running 3/3unter-app-redis-1   Rem...                                 0.5s
  Container counter-app-redis-1    Re...                                 0.5s
  Container counter-app-web-fe-1   R...                                  1.3s
  Network counter-app_counter-net  Removed                               0.1s
[1]+  Done                    docker-compose up

2、执行docker-compose ps命令查看应用的当前状态。这里观察到,应用目前处于关闭状态。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose ps
NAME                COMMAND             SERVICE             STATUS              PORTS

3、执行docker image ls命令和docker volume ls命令查看卷和镜像情况。这里观察到,二者仍然存在。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker volume ls
DRIVER    VOLUME NAME
local     91b2efae88829c2de058292dcd33932db21dae9570557b5d9a5081e01fb102b5
local     counter-app_counter-vol

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker image  ls
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
counter-app_web-fe   latest    48da14617ca0   49 minutes ago   55.5MB
redis                alpine    0139e7020436   13 days ago      28.5MB

4.3 使用Docker Compose停止应用

1、执行docker-compose stop命令停止应用。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose stop
[+] Running 2/2
  Container counter-app-redis-1   Sto...                                 0.3s
  Container counter-app-web-fe-1  St...                                  0.4s

2、执行docker container ls -a命令查看全部的容器,这里观察到停止的容器仍然存在,不会删除。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker container ls -a
CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS                          PORTS     NAMES
e5a75ba1706e   counter-app_web-fe   "python app.py"          5 minutes ago   Exited (0) About a minute ago             counter-app-web-fe-1
e1dcbdf60223   redis:alpine         "docker-entrypoint.s…"   5 minutes ago   Exited (0) About a minute ago             counter-app-redis-1

4.4 使用Docker Compose删除应用

1、执行docker-compose rm命令删除应用。这里注意,这只会删除应用相关的容器和网络,但是不会删除卷、镜像、应用源码。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose rm
? Going to remove counter-app-web-fe-1, counter-app-redis-1 Yes
[+] Running 2/0
  Container counter-app-redis-1   Rem...                                 0.0s
  Container counter-app-web-fe-1  Re...                                  0.0s

2、执行docker container ls -a命令查看全部的容器,这里观察到容器删除完成。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

4.5 使用Docker Compose重启应用

1、执行docker-compose restart命令重启应用。这里注意,重启应用的条件是停止应用,而不是删除应用,换句话说,删除应用没办法重启应用。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose restart 
[+] Running 2/2
  Container counter-app-web-fe-1  St...                                  1.7s
  Container counter-app-redis-1   Sta...                                 0.8s

2、执行docker-compose ps命令查看应用的当前状态。这里观察到,应用目前处于关闭状态。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose ps
NAME                   COMMAND                  SERVICE             STATUS              PORTS
counter-app-redis-1    "docker-entrypoint.s…"   redis               running             6379/tcp
counter-app-web-fe-1   "python app.py"          web-fe              running             0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

五、使用Docker Compose部署、管理应用命令

1、Compose文件(compose.yml或compose.yaml)所在目录视图下执行docker-compose up &命令启动Docker Compose。其中&参数代表日志回显给终端窗口。(-d参数是在后台运行)

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose up & 
root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose up -d 

说明:默认docker-compose up会查找名为docker-compose.yml或docker-compose.yaml的Compose文件。如果Compose文件是其他文件名,需要-f参数指定文件名。

2、执行docker network ls命令查看网络。这里观察到生成名为counter-app_counter-net的网络。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker network ls

3、执行docker volume ls命令查看卷,这里观察到生成名为counter-app_counter-vol的卷。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker volume ls

4、执行docker-compose down命令停止和关闭应用。这里注意,会留下镜像、卷和源码。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose down

5、执行docker-compose ps命令查看应用的当前状态。这里观察到,应用目前处于关闭状态。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose ps

6、执行docker-compose restart命令重启应用。这里注意,重启应用的条件是停止应用,而不是删除应用,换句话说,删除应用没办法重启应用。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker-compose restart 

7、执行docker volume inspect counter-app_counter-vol | grep Mount命令查看卷位于Docker主机的什么位置。

root@zq-virtual-machine:/home/zq/Desktop/counter-app# docker volume inspect counter-app_counter-vol | grep Mount
        "Mountpoint": "/var/lib/docker/volumes/counter-app_counter-vol/_data"