Docker Compose(九)

一、背景:

        对于现代应用来说,大多数都是通过很多的微服务互相协同组成一个完整的应用。例如,订单管理、用户管理、品类管理、缓存服务、数据库服务等,他们构成了一个电商平台的应用。而部署和管理大量的服务容器是一件非常繁琐的事情,而 Docker Compose 就是解决这类问题的。

        Docker Compose 是一个需要在 Docker 主机上进行安装的 Docker 容器编排外部工具。其并不是通过脚本或各种冗长的 Docker 命令来将应用组件组织起来,而是通过一个声明式的配置文件来描述整个应用,然后通过一条命令来完成应用的部署。部署成功后,还可以通过一系列简单命令实现对其完整生命周期的管理。

二、Compose 文件

2.1 文件简介

        Docker Compose 使用 YAML 文件来定义服务。官方推荐的默认文件名为 compose.yml ,但同时也支持 docker-compose.yml。

        由于一个 compose 文件定义的为一个项目所有的服务,所以一般在创建 compose 文件之前先新建一个目录,目录名称一般为项目名称,然后再将项目所需的所有镜像和微服务的 Dockerfile 放入该目录,并在此目录中新建 compose 文件。

        compose 文件包含 6 个顶级属性:versionservicesnetworksvolumesconfigs secrets,以及很多的他们下面包含的属性,下面简单介绍下常用的属性。

2.2 version

        version 是一个顶级属性,但已经过时,不再需要在 compose 文件中出现了,从以下官网中可以看出。

Docker Compose(九)_第1张图片

2.3 networks

        networks 作为一个顶级属性,用于定义和创建应用中所使用到的所有网络。其下包含的第一级属性即为网络属性,这个网络名称可以随意命名。而在网络名称下还可以包含很多的属性,常用属性如下:

services:
  app:
    networks:
	  -app_bridge:  # 这里使用的并不是网络名称
networks:
  app_bridge:
    name:appBGnet    # 这才是网络名称
	driver:bridge

2.3.1 name

        networks 下的第一级属性一网络名称,并不是真正的网络名称,而仅仅是网终名称的一部分。在真正生成网络后,其真正的网络名称格式为:当前 compose 文件所在目录名_networks 下的第一级属性

        但如果设置了 name 属性,则网络名称即为这里指定的名称,不会出现名称再合成情况。

2.3.2 driver

        用于指定网络驱动,缺省驱动为 Bridge

2.3.3 attachable

        如果该属性设置为 true,则除了当前 compose 中定义的服务外,其它独立容器也可以连接到此网络,并能与该网络中的服务及也连接到该网络的其它独立容器通信。缺省状态为 false

2.4 volumes

        volumes 作为一个顶级属性,用于定义和创建应用中所用到的所有 volume 。其下包含的第一级属性即为 volume 的卷标,这个卷标可以随意命名。这个卷标所代表的是当前 Docker 主机中的目录,至于该目录的具体位置,是由系统自动分配的。

        在网络名称下还可包含很多的属性,但这些属性并不常用,所以这里就不进行介绍了。

2.5 services

        services 是一个顶级属性,用于定义一个应用中所包含的服务。Docker Compose 会将每个服务部署在各自的容器中。其下包含的第一级的属性即为服务名称,这个名称可以根据服务内容随意命名。而在服务名称下还可包含很多的属性,常用属性如下:

2.5.1 build

        用于指定一个 Dockerfile 的路径。而该 Dockerfile 则是用于创建当前服务镜像的。这个路径可以是以斜杠(/)开头的绝对路径,也可以是相对于当前 compose 文件的以点(.)号开头的相对路径。

        如果 Dockerfile 文件名不是默认名称,则需要通过 build 下的 context 属性指定路径,dockerfile 属性指定文件名。

build:
  context: ./
  dockerfile: myDockerfile

2.5.2 image

        用户指定当前服务所需要使用的镜像,这个镜像可以是本地镜像,也可以是远程镜像仓库中的镜像(会自动 pull)。

        如果设置了 build,此时再设置的 image 属性即为构建出的镜像的名称与 Tag

2.5.3 container_name

        该属性用于设置容器名称,但并不是必须的。如果没有设置该属性,容器名称则会采用“合成方式”。而合成时需要用到 services 下的第一级属性。

        在 services 下存在一级属性,称为服务名称。该级属性是作为 services 下的第一级属性出现的。服务名称将来会作为容器名称的一部分出现。容器的名称格式为:当前 compose文件所在目录名_服务名称

        如果在 services 下没有指定 image 属性,而是使用 build 属性,即没有现成的镜像,而是根据 build 下指定的 Dockerfile 生成镜像,此时生成的镜像名称格式为:当前 compose 文件所在目录名_服务名称

2.5.4 ports

        一个列表。前面为暴露出的端口号,后面为容器中应用的端口号。如果仅设置了一个端口号,那么这个端口号是容器中应用的端口号,其暴露到宿主机的端口号会被随机分配。

ports:
  - 80:80      # 绑定容器的 80 端口到主机的 80 端口
  - 9000:80	   # 绑定容器的 80 端口到主机的 9000 端口
  - 443        # 绑定容器的 443 端口到主机的任意端口,容器启动时随机分配绑定的主机端口号

2.5.5 command

        用于覆盖 Dockerfile 中的 CMD 指令内容,即启动该服务容器后立即运行的命令。如果直接按照 Dockerfile 中的 CMD 指令内容执行,则 compose 文件中无需该 command 属性。

2.5.6 depends_on

        一个列表。用于指定当前服务的启动所依赖的应用名称。即列表中指定的服务会先于当前服务启动。

2.5.7 deploy

        用于指定当前服务容器的部署设置。其下有一个常用属性 replicas,用于指定该服务启动的容器的数量。即实现一个服务多个容器。一旦指定了 deploy:replicas,就不能再指定 container name 属性了。因为各个启动的容器名称不能相同,而只能由系统自动生成。

services:
  frontend:
    image: awesome/webapp
	deploy:
	  mode: replicated
	  replicas: 6

2.5.8 networks

        用于指定当前服务容器要连接到的网络。该网络必须是已经存在的,或通过顶级属性networks 创建的网络。

2.5.9 volumes

        用于指定当前服务容器所使用到的所有 volume。这些 volume 可以使用路径与卷标两种方式。

        例如,下面是路径方式,非常直观,易于查看,但需要管理本地路径。

mysql:
  image: mysql:5.6
  ports:
    - 3306:3306
  volumes:
    - /etc/mysql:/var/lib/mysql

        再如,下面是卷标方式,backend backup 两个服务共享了 db-data 的卷,逻辑简洁明了,且无需管理本地路径。但具体卷标所代表的是 Docker 主机的哪个路径,并不能直观的看到。需要通过 docker volume inspect [卷标] 来查看。 

services:
  backend:
    image: awesome/database
    volumes:
      - db-data:/etc/data
  backup:
    image: backup-service
    volumes:
      - db-data:/var/lib/backup/data
volumes:
  db-data:

三、常用命令

3.1 docker-compose pull

        拉取 compose 中服务以来的全部镜像或指定镜像。通过在命令后添加服务名称来指定。

# 1、首先查看当前的所有镜像
[root@localhost xhf_test]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

# 2、必须切换到有 compose.yml 的目录下
[root@localhost xhf_test]# ls
com.DockerTestOne-0.0.1-SNAPSHOT.jar  compose.yml  Dockerfile

# 3、拉取 compose.yml 文件中涉及到的镜像
[root@localhost xhf_test]# docker-compose pull
[+] Pulling 20/20
 ✔ app Skipped - No image to be pulled                                                                                                                            0.0s 
 ✔ mysql 11 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                                          33.2s 
   ✔ 35b2232c987e Pull complete                                                                                                                                   7.8s 
   ✔ fc55c00e48f2 Pull complete                                                                                                                                   7.8s 
   ✔ 0030405130e3 Pull complete                                                                                                                                   8.3s 
   ✔ e1fef7f6a8d1 Pull complete                                                                                                                                   8.4s 
   ✔ 1c76272398bb Pull complete                                                                                                                                   8.5s 
   ✔ f57e698171b6 Pull complete                                                                                                                                  11.3s 
   ✔ f5b825b269c0 Pull complete                                                                                                                                  11.4s 
   ✔ dcb0af686073 Pull complete                                                                                                                                  11.5s 
   ✔ 27bbfeb886d1 Pull complete                                                                                                                                  15.4s 
   ✔ 6f70cc868145 Pull complete                                                                                                                                  15.7s 
   ✔ 1f6637f4600d Pull complete                                                                                                                                  15.9s 
 ✔ redis 6 layers [⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                                                 8.6s 
   ✔ 065132d9f705 Pull complete                                                                                                                                   5.4s 
   ✔ be9835c27852 Pull complete                                                                                                                                   5.4s 
   ✔ f4a0d1212c38 Pull complete                                                                                                                                   5.5s 
   ✔ ea1f878b621a Pull complete                                                                                                                                   5.9s 
   ✔ 7a838393b4b9 Pull complete                                                                                                                                   5.9s 
   ✔ 9f48e489da12 Pull complete                                                                                                                                   6.0s 

# 4、查看镜像是否拉取成功
[root@localhost xhf_test]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        5.6       dd3b2a5dcb48   19 months ago   303MB
redis        4.0.1     aaf79d45ddb1   5 years ago     107MB

3.2 docker-compose config

        检查 compose 文件是否正确。可添加选项 -q,表示只有存在问题时才有输出。

# 1、检查编写的 compose 文件是否有问题
[root@localhost xhf_test]# docker-compose config -q

# 2、输出的就是错误的信息
parsing /root/xhf_test/compose.yml: yaml: line 4: could not find expected ':'

# 3、改正错误
[root@localhost xhf_test]# vi compose.yml

# 4、再次检查
[root@localhost xhf_test]# docker-compose config -q

# 5、这种输出就没有问题了
[root@localhost xhf_test]#

3.3 docker-compose up

        启动 compose 中所有的容器。-d 选项表示后台启动

# 1、后台的方式启动服务
[root@localhost xhf_test]# docker-compose up -d
[+] Building 16.3s (7/7) FINISHED                                                                                            
 => [app internal] load build definition from Dockerfile                                                                0.2s
 => => transferring dockerfile: 209B                                                                                    0.0s
 => [app internal] load .dockerignore                                                                                   0.1s
 => => transferring context: 2B                                                                                         0.0s
 => [app internal] load metadata for docker.io/library/java:8                                                          15.6s
 => [app internal] load build context                                                                                   0.2s
 => => transferring context: 21.12MB                                                                                    0.2s
 => CACHED [app 1/2] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c  0.0s
 => [app 2/2] COPY *.jar /app.jar                                                                                       0.2s
 => [app] exporting to image                                                                                            0.1s
 => => exporting layers                                                                                                 0.1s
 => => writing image sha256:02f0a157ab34f13ef6847060a96acf94b3f3903d0c0d409b969254126da44e1c                            0.0s
 => => naming to docker.io/library/xhf_test-app                                                                         0.0s
[+] Running 4/4
 ✔ Network xhf_test_default    Created                                                                                  0.3s 
 ✔ Container xhf_test-mysql-1  Started                                                                                  0.9s 
 ✔ Container xhf_test-redis-1  Started                                                                                  0.8s 
 ✔ Container xhf-project02     Started                                                                                  1.2s 

# 2、查看启动的容器是否成功
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
712b7474b78c   xhf_test-app   "java -jar /app.jar …"   58 seconds ago   Up 57 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp              xhf-project02
454c684d4f7a   mysql:5.7      "docker-entrypoint.s…"   58 seconds ago   Up 57 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   xhf_test-mysql-1
45f5d1589c71   redis:4.0.1    "docker-entrypoint.s…"   58 seconds ago   Up 57 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              xhf_test-redis-1

3.4 docker-compose logs

        查看 compose 中所有服务或指定服务的运行日志。通过命令后添加服务名称来指定。默认情况下,将对不同的服务日志使用不同的颜色来区分。

Docker Compose(九)_第2张图片

 3.5 docker-compose ps

        列出 compose 中所有服务或指定服务。通过在命令后添加服务名称来指定

# 1、显示当前 compose 中的服务信息
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 2 minutes ago       Up 2 minutes        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               2 minutes ago       Up 2 minutes        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               2 minutes ago       Up 2 minutes        0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 2、显示当前运行的容器信息
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                       NAMES
06d6ad97781c   xhf_test-app   "java -jar /app.jar …"   3 minutes ago   Up 3 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   xhf-project02
a41c89cde090   mysql:5.6      "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   xhf_test-mysql-1
02324ae92687   redis:4.0.1    "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   xhf_test-redis-1

3.6 docker-compose top

        列出 compose 中当前正在运行的所有或指定服务。通过在命令后添加服务名称来指定

[root@localhost xhf_test]# docker-compose top
xhf-project02
UID    PID     PPID    C    STIME   TTY   TIME       CMD
root   18670   18642   9    18:15   ?     00:00:29   java -jar /app.jar -server.port=8080   

xhf_test-mysql-1
UID       PID     PPID    C    STIME   TTY   TIME       CMD
polkitd   18516   18489   0    18:15   ?     00:00:02   mysqld   

xhf_test-redis-1
UID       PID     PPID    C    STIME   TTY   TIME       CMD
polkitd   18515   18445   0    18:15   ?     00:00:00   redis-server *:6379  

3.7 docker-compose images

        列出 compose 中所有服务或指定服务对应的镜像。通过在命令后添加服务名称来指定

[root@localhost xhf_test]# docker-compose images
CONTAINER           REPOSITORY          TAG                 IMAGE ID            SIZE
xhf-project02       xhf_test-app        latest              02f0a157ab34        664MB
xhf_test-mysql-1    mysql               5.6                 dd3b2a5dcb48        303MB
xhf_test-redis-1    redis               4.0.1               aaf79d45ddb1        107MB

3.8 docker-compose port

        显示某个容器端口所映射的宿主机端口。

[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 32 minutes ago      Up 13 minutes       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               32 minutes ago      Up 13 minutes       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               32 minutes ago      Up 13 minutes       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 查看公网的映射端口
[root@localhost xhf_test]# docker-compose port mysql 3306
0.0.0.0:3306

3.9  docker-compose run

        在指定服务上执行一条命令

[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 45 minutes ago      Up 26 minutes       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               45 minutes ago      Up 26 minutes       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               45 minutes ago      Up 26 minutes       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 执行 ping 的命令,发现 redis 里面没有安装 network 工具
[root@localhost xhf_test]# docker-compose run redis ping www.baidu.com
/usr/local/bin/docker-entrypoint.sh: 16: exec: ping: not found

3.10 docker-compose exec

        进入指定服务容器。通过在命令后添加服务名称来指定

# 1、查看 service 的名字
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 26 minutes ago      Up 8 minutes        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               26 minutes ago      Up 8 minutes        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               26 minutes ago      Up 8 minutes        0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 2、进入 compose 的容器里面,记得是 service 的名字
[root@localhost xhf_test]# docker-compose exec -it app bash
root@06d6ad97781c:/# ls
app.jar  bin  boot  dev  etc  home  lib  lib64	media  mnt  opt  proc  root  run  sbin	srv  sys  tmp  usr  var
root@06d6ad97781c:/# exit
exit

3.11 docker-compose pause

        暂停 compose 中所有服务容器或指定服务容器。通过在命令后添加服务名称来指定

# 1、执行暂停命令
[root@localhost xhf_test]# docker-compose pause
[+] Pausing 3/0
 ✔ Container xhf_test-redis-1  Paused                                                                                                                                0.0s 
 ✔ Container xhf_test-mysql-1  Paused                                                                                                                                0.0s 
 ✔ Container xhf-project02     Paused                                                                                                                                0.0s 

# 2、查看 compose 中的容器状态,发现处于暂停状态
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS                  PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 21 minutes ago      Up 3 minutes (Paused)   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               21 minutes ago      Up 3 minutes (Paused)   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               21 minutes ago      Up 3 minutes (Paused)   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

3.12 docker-compose unpause

        恢复 compose 中处于暂停状态的所有服务容器或指定服务器。通过在命令后添加服务名称来指定。

# 1、解除暂停状态
[root@localhost xhf_test]# docker-compose unpause
[+] Running 3/0
 ✔ Container xhf-project02     Unpaused                                                                                                                              0.0s 
 ✔ Container xhf_test-redis-1  Unpaused                                                                                                                              0.0s 
 ✔ Container xhf_test-mysql-1  Unpaused                                                                                                                              0.0s 
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS                                       NAMES
06d6ad97781c   xhf_test-app   "java -jar /app.jar …"   23 minutes ago   Up 4 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   xhf-project02
a41c89cde090   mysql:5.6      "docker-entrypoint.s…"   23 minutes ago   Up 4 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   xhf_test-mysql-1
02324ae92687   redis:4.0.1    "docker-entrypoint.s…"   23 minutes ago   Up 4 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   xhf_test-redis-1
[

3.13 docker-compose stop

        停止 compose 中所有服务容器或指定服务容器。通过在命令后添加服务名称来指定

# 1、先查看
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 16 minutes ago      Up 2 seconds        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               16 minutes ago      Up 2 seconds        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               16 minutes ago      Up 2 seconds        0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 2、再停止
[root@localhost xhf_test]# docker-compose stop
[+] Stopping 3/3
 ✔ Container xhf-project02     Stopped                                                                                                                               0.3s 
 ✔ Container xhf_test-redis-1  Stopped                                                                                                                               0.2s 
 ✔ Container xhf_test-mysql-1  Stopped                                                                                                                               1.8s 
# 3、再查看,发现都关掉了
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND             SERVICE             CREATED             STATUS              PORTS

# 4、再启动
[root@localhost xhf_test]# docker-compose start
[+] Running 3/3
 ✔ Container xhf_test-redis-1  Started                                                                                                                               0.3s 
 ✔ Container xhf_test-mysql-1  Started                                                                                                                               0.4s 
 ✔ Container xhf-project02     Started                                                                                                                               0.4s 

# 5、又都成功启动了
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 17 minutes ago      Up 2 seconds        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               17 minutes ago      Up 3 seconds        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               17 minutes ago      Up 3 seconds        0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

3.14 docker-compose restart

        重启 compose 中所有服务容器或指定服务容器。通过在命令后添加服务名称来指定

# 1、执行重启的命令
[root@localhost xhf_test]# docker-compose restart
[+] Restarting 3/3
 ✔ Container xhf-project02     Started                                                                                                                               0.7s 
 ✔ Container xhf_test-mysql-1  Started                                                                                                                               2.8s 
 ✔ Container xhf_test-redis-1  Started                                                                                                                               0.7s 

# 2、再次查看
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 18 minutes ago      Up 12 seconds       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               18 minutes ago      Up 10 seconds       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               18 minutes ago      Up 12 seconds       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

3.15 docker-compose start

        启动 compose 中所有服务容器或指定服务容器。通过在命令后添加服务名称来指定

# 1、先查看
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 16 minutes ago      Up 2 seconds        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               16 minutes ago      Up 2 seconds        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               16 minutes ago      Up 2 seconds        0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 2、再停止
[root@localhost xhf_test]# docker-compose stop
[+] Stopping 3/3
 ✔ Container xhf-project02     Stopped                                                                                                                               0.3s 
 ✔ Container xhf_test-redis-1  Stopped                                                                                                                               0.2s 
 ✔ Container xhf_test-mysql-1  Stopped                                                                                                                               1.8s 
# 3、再查看,发现都关掉了
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND             SERVICE             CREATED             STATUS              PORTS

# 4、再启动
[root@localhost xhf_test]# docker-compose start
[+] Running 3/3
 ✔ Container xhf_test-redis-1  Started                                                                                                                               0.3s 
 ✔ Container xhf_test-mysql-1  Started                                                                                                                               0.4s 
 ✔ Container xhf-project02     Started                                                                                                                               0.4s 

# 5、又都成功启动了
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 17 minutes ago      Up 2 seconds        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               17 minutes ago      Up 3 seconds        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               17 minutes ago      Up 3 seconds        0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

3.16 docker-compose kill

        通过发送 SIGKILL 信号停止指定服务的容器。

[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 47 minutes ago      Up 28 minutes       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               47 minutes ago      Up 28 minutes       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               47 minutes ago      Up 28 minutes       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 杀掉 mysql 容器
[root@localhost xhf_test]# docker-compose kill mysql
[+] Killing 1/1
 ✔ Container xhf_test-mysql-1  Killed                                                                                                                                0.3s 
[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 47 minutes ago      Up 28 minutes       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               47 minutes ago      Up 28 minutes       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

3.17 docker-compose rm

        删除 compose 中的处于停止状态的所有容器或指定服务容器。通过在命令后添加服务名称来指定

[root@localhost xhf_test]# docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
xhf-project02       xhf_test-app        "java -jar /app.jar …"   app                 48 minutes ago      Up 29 minutes       0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
xhf_test-mysql-1    mysql:5.6           "docker-entrypoint.s…"   mysql               48 minutes ago      Up 1 second         0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
xhf_test-redis-1    redis:4.0.1         "docker-entrypoint.s…"   redis               48 minutes ago      Up 29 minutes       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

# 正在运行的是无法删除的
[root@localhost xhf_test]# docker-compose rm mysql
No stopped containers

# 关掉
[root@localhost xhf_test]# docker-compose stop  mysql
[+] Stopping 1/1
 ✔ Container xhf_test-mysql-1  Stopped                                                                                                                               2.0s 

# 再次删除
[root@localhost xhf_test]# docker-compose rm mysql
? Going to remove xhf_test-mysql-1 Yes
[+] Removing 1/0
 ✔ Container xhf_test-mysql-1  Removed                                                                                                                               0.0s 
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                       NAMES
06d6ad97781c   xhf_test-app   "java -jar /app.jar …"   49 minutes ago   Up 30 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   xhf-project02
02324ae92687   redis:4.0.1    "docker-entrypoint.s…"   49 minutes ago   Up 30 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   xhf_test-redis-1

四、安装

[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# ls
[root@localhost bin]# ls

# 1、官方推荐的下载地址
[root@localhost bin]# curl -SL https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 57.6M  100 57.6M    0     0  12.4M      0  0:00:04  0:00:04 --:--:-- 20.7M
[root@localhost bin]# ls
docker-compose

# 2、增加可执行属性
[root@localhost bin]# chmod +x docker-compose 
[root@localhost bin]# ls
docker-compose

# 3、检测是否安装成功
[root@localhost bin]# docker-compose version
Docker Compose version v2.20.0

# 不需要开启 docker,因为他是一个独立的组件
[root@localhost bin]# docker version
Client: Docker Engine - Community
 Version:           24.0.4
 API version:       1.43
 Go version:        go1.20.5
 Git commit:        3713ee1
 Built:             Fri Jul  7 14:54:21 2023
 OS/Arch:           linux/amd64
 Context:           default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

五、项目构建

5.1 工程构建打包

        首先,根据 文章1和 文章2 创建一个 spring boot 的项目,项目不用很复杂和完全照搬,只是让这个工程用到了 mysql redis 就好,因为我们后面的操作,需要涉及到多个组件。

        特别需要注意的地方是下面这块,mysql redis IP,目前使用的是宿主机上的 ip,因为要打包部署到 Docker 容器上。 

Docker Compose(九)_第3张图片

        构建版本,此时我们得到了一个可执行的 jar 包文件:com.docker_compose-0.0.1-SNAPSHOT.jar

5.2 定义 Dockerfile 

        在 /root 目录中 mkdir 一个名称为 xhf_test 的目录,然后在该目录下新建一个 Dockerfile 文件,并把上一步骤的 jar 包文件也拷贝进去,Dockerfile 文件的内容如下:

FROM java:8

COPY *.jar /app.jar

CMD ["-server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

Docker Compose(九)_第4张图片

六、手工启动项目

        为了与使用 Docker Compose 编排项目进行对比,体现 Docker Compose 的方便快捷,这里先使用纯手动方式启动项目。

6.1 启动 mysql 容器

# 1、启动 mysql 容器并挂载相应的目录
[root@localhost xhf_test]# docker run -d --name mysql5.6 -p 3306:3306 -e MYSQL_ROOT_PASSWORD='Rfid123456' -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql mysql:5.6
Unable to find image 'mysql:5.6' locally
5.6: Pulling from library/mysql
35b2232c987e: Pull complete 
fc55c00e48f2: Pull complete 
0030405130e3: Pull complete 
e1fef7f6a8d1: Pull complete 
1c76272398bb: Pull complete 
f57e698171b6: Pull complete 
f5b825b269c0: Pull complete 
dcb0af686073: Pull complete 
27bbfeb886d1: Pull complete 
6f70cc868145: Pull complete 
1f6637f4600d: Pull complete 
Digest: sha256:20575ecebe6216036d25dab5903808211f1e9ba63dc7825ac20cb975e34cfcae
Status: Downloaded newer image for mysql:5.6
9c63331ae947627fef7d84bd4027b724db528645ce5bcd2518a13409a45d0f96

# 2、看下容器是否启动成功
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                       NAMES
9c63331ae947   mysql:5.6   "docker-entrypoint.s…"   8 seconds ago   Up 5 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql5.6

# 3、进入到 mysql 的容器里面
[root@localhost xhf_test]# docker exec -it mysql5.6 bash

# 4、使用 root 账号登录
root@9c63331ae947:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 5、执行我们的建表语句
mysql> DROP DATABASE IF EXISTS zlits_user;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE DATABASE zlits_user;
Query OK, 1 row affected (0.00 sec)

mysql> USE zlits_user;
Database changed
mysql> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `user`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE `user` (
    ->   `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
    ->   `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '',
    ->   `password` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '',
    ->   `age` bigint(20)  DEFAULT NULL  COMMENT '',
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='';
Query OK, 0 rows affected (0.00 sec)

# 6、查看表是否创建成功
mysql> show tables;
+----------------------+
| Tables_in_zlits_user |
+----------------------+
| user                 |
+----------------------+
1 row in set (0.01 sec)

 6.2 启动 redis 容器

# 1、启动 redis ,并挂载相关目录
[root@localhost xhf_test]# docker run --name redis -p 6379:6379 -v /usr/local/redis/data:/data -v /usr/local/redis/conf/redis.conf:/etc/redis/redis.conf -d redis:4.0.1 redis-server /etc/redis/redis.conf
Unable to find image 'redis:4.0.1' locally
4.0.1: Pulling from library/redis
065132d9f705: Pull complete 
be9835c27852: Pull complete 
f4a0d1212c38: Pull complete 
ea1f878b621a: Pull complete 
7a838393b4b9: Pull complete 
9f48e489da12: Pull complete 
Digest: sha256:8a54dcc711406447b3631a81ef929f500e6836b43e7d61005fa27057882159da
Status: Downloaded newer image for redis:4.0.1
8c8b70174812a1d6cac7b2dfd9ff4e1fc24f25455ce4f095866174041d3d7061

# 2、查看容器是否启动成功
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                                       NAMES
8c8b70174812   redis:4.0.1   "docker-entrypoint.s…"   5 seconds ago    Up 3 seconds    0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis
9c63331ae947   mysql:5.6     "docker-entrypoint.s…"   15 minutes ago   Up 15 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql5.6

6.3 启动应用容器

        在构建应用镜像之前,打开项目的配置文件,再次确认配置文件中指定的 mysql redis 主机的 IP 地址是否为 Docker 主机的 IP。如果不是宿主机的 IP 的话,在修改地址后记得重新进行 package 打包,并且重新上传。

# 1、构建版本
[root@localhost xhf_test]# docker build -t xhf-demo:1.0 .
[+] Building 16.3s (7/7) FINISHED                                                                             docker:default
 => [internal] load build definition from Dockerfile                                                                    0.0s
 => => transferring dockerfile: 209B                                                                                    0.0s
 => [internal] load .dockerignore                                                                                       0.0s
 => => transferring context: 2B                                                                                         0.0s
 => [internal] load metadata for docker.io/library/java:8                                                              15.8s
 => [internal] load build context                                                                                       0.2s
 => => transferring context: 21.12MB                                                                                    0.2s
 => CACHED [1/2] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d  0.0s
 => [2/2] COPY *.jar /app.jar                                                                                           0.1s
 => exporting to image                                                                                                  0.1s
 => => exporting layers                                                                                                 0.1s
 => => writing image sha256:c66050a07984686596109ef53a40ac5a54b68458eafe5bf938bc0abf443956a1                            0.0s
 => => naming to docker.io/library/xhf-demo:1.0                                                                         0.0s

# 2、查看构建的镜像是否成功
[root@localhost xhf_test]# docker images
REPOSITORY                                                                 TAG                IMAGE ID       CREATED          SIZE
xhf-demo                                                                   1.0                c66050a07984   13 seconds ago   664MB
tomcat                                                                     8.0                ef6a7c98d192   4 years ago      356MB
redis                                                                      4.0.1              aaf79d45ddb1   5 years ago      107MB

# 3、启动我们的容器
[root@localhost xhf_test]# docker run --name xhf-project01 -dp 8080:8080 xhf-demo:1.0
4216f5485110634ee5b71d9ae0213d767b1d2dac602efa43c0717ae9cc44a9c3

# 4、查看容器是否启动成功,此时我们已经启动了三个容器了
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                       NAMES
4216f5485110   xhf-demo:1.0   "java -jar /app.jar …"   5 seconds ago    Up 3 seconds    0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   xhf-project01
8c8b70174812   redis:4.0.1    "docker-entrypoint.s…"   8 minutes ago    Up 8 minutes    0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis
9c63331ae947   mysql:5.6      "docker-entrypoint.s…"   23 minutes ago   Up 23 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql5.6

6.4 验证

        参考刚才在 eclipse 的验证方式进行验证,查看 mysql 里面是否存储了数据,再查看 redis 里面是否进行了缓存,如下图所示,我们可以看到,mysql 存储了数据,redis 添加了缓存。

Docker Compose(九)_第5张图片

七、Compose 编排启动项目

        手工启动项目不仅繁琐易错,而且还存在一个致命问题:当前应用访问的 mysql redis 只能是运行在当前 Docker 宿主机之上的,否则将无法访问它们。因为在应用的配置文件中已经将 mysql redis IP 进行了硬编码。而使用 Docker Compose 可以解决所有的问题。

7.1 定义 compose.yml

        在 xhf_test 目录下新建一个文件 compose.yml,内容如下所示:

# 一级标签,里面定义了所有的服务
services:
  # 这个是服务的名称,叫什么都可以
  app:
    # 构建 dockerfile,如果你的名称不是dockerfile,那么就需要在./ 后面添加文件的名字
    build: ./
    # 容器的名称
    container_name: xhf-project02
    # 暴露的端口号
    ports:
      - 8080:8080
    # 容器的启动是有先后顺序的,这里先启动下面的那两个服务
    depends_on:
      - mysql
      - redis 
  mysql:
    image: mysql:5.6
    # 环境配置
    environment:
      MYSQL_ROOT_PASSWORD: Rfid123456
    ports:
      - 3306:3306
	# 暴露的文件挂载
    volumes:
      - /usr/local/mysql/conf:/etc/mysql/conf.d
      - /usr/local/mysql/data:/var/lib/mysql
  redis:
    image: redis:4.0.1
    ports:
      - 6379:6379
    volumes:
      - /usr/local/redis/data:/data
      - /usr/local/redis/conf/redis.conf:/etc/redis/redis.conf
    # 执行命令
    command: redis-server /etc/redis/redis.conf

        编写完文件之后,输入命令  docker-compose config -q 来检查上面的文件里面是否存在格式的错误,如下所示,如果没有任何输出就证明没有问题了:

7.2 修改应用

        对于应用服务,需要修改其配置文件 application.properties,将 mysql redis 的主机名修改为它们相应服务的名称。如下图所示:

Docker Compose(九)_第6张图片

        由于应用程序的配置文件发生了变化,所以需要对应用程序重新进行 package 打包,并将新的 jar 包上传到 Linux 系统的 /root/xhf_test 目录中。

7.3 启动所有容器

[root@localhost xhf_test]# docker-compose up -d
[+] Building 16.3s (7/7) FINISHED                                                                                            
 => [app internal] load build definition from Dockerfile                                                                0.2s
 => => transferring dockerfile: 209B                                                                                    0.0s
 => [app internal] load .dockerignore                                                                                   0.1s
 => => transferring context: 2B                                                                                         0.0s
 => [app internal] load metadata for docker.io/library/java:8                                                          15.6s
 => [app internal] load build context                                                                                   0.2s
 => => transferring context: 21.12MB                                                                                    0.2s
 => CACHED [app 1/2] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c  0.0s
 => [app 2/2] COPY *.jar /app.jar                                                                                       0.2s
 => [app] exporting to image                                                                                            0.1s
 => => exporting layers                                                                                                 0.1s
 => => writing image sha256:02f0a157ab34f13ef6847060a96acf94b3f3903d0c0d409b969254126da44e1c                            0.0s
 => => naming to docker.io/library/xhf_test-app                                                                         0.0s
[+] Running 4/4
 ✔ Network xhf_test_default    Created                                                                                  0.3s 
 ✔ Container xhf_test-mysql-1  Started                                                                                  0.9s 
 ✔ Container xhf_test-redis-1  Started                                                                                  0.8s 
 ✔ Container xhf-project02     Started                                                                                  1.2s 

7.4 写入 mysql 数据

# 1、查看 mysql 的容器ID
[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                       NAMES
d9683f8edb1e   xhf_test-app   "java -jar /app.jar …"   5 seconds ago   Up 3 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   xhf-project02
d38d91c71e4c   mysql:5.6      "docker-entrypoint.s…"   5 seconds ago   Up 3 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   xhf_test-mysql-1
6ed666ea37ab   redis:4.0.1    "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   xhf_test-redis-1

# 3、进入到 mysql 的容器里面
[root@localhost xhf_test]# docker exec -it d38d91c71e4c bash

# 4、使用 root 账号登录
root@9c63331ae947:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 5、执行我们的建表语句
mysql> DROP DATABASE IF EXISTS zlits_user;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE DATABASE zlits_user;
Query OK, 1 row affected (0.00 sec)

mysql> USE zlits_user;
Database changed
mysql> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `user`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE `user` (
    ->   `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
    ->   `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '',
    ->   `password` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '',
    ->   `age` bigint(20)  DEFAULT NULL  COMMENT '',
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='';
Query OK, 0 rows affected (0.00 sec)

# 6、查看表是否创建成功
mysql> show tables;
+----------------------+
| Tables_in_zlits_user |
+----------------------+
| user                 |
+----------------------+
1 row in set (0.01 sec)

7.5 验证

        参考刚才在 6.4 章节的验证方式进行验证,查看 mysql 里面是否存储了数据,再查看 redis 里面是否进行了缓存,如下图所示,我们可以看到,mysql 存储了数据,redis 添加了缓存。

Docker Compose(九)_第7张图片

7.6 指定各组件名称

        前面的 compose.yml 文件中应用生成的镜像采用的是默认名称,mysql redis 生成的容器名称采用的也是默认名称,而生成加入的 bridge 网络也成是默认名称的网络。这些组件的名称也可以被指定的名称所替代。

7.6.1 定义 compose2.yml

[root@localhost xhf_test]# cp compose.yml compose2.yml
[root@localhost xhf_test]# ls
com.DockerTestOne-0.0.1-SNAPSHOT.jar  compose2.yml  compose.yml  Dockerfile
[root@localhost xhf_test]# vi compose2.yml 
[root@localhost xhf_test]# cat compose2.yml 
services:
  app:
    build: ./
    container_name: xhf-project02
    ports:
      - 8080:8080
    depends_on:
      - mysql
      - redis
    networks:
      - ab
  mysql:
    image: mysql:5.6
    container_name: xhfmysql     # 设置 mysql 的容器名称为 xhfmysql
    environment:
      MYSQL_ROOT_PASSWORD: Rfid123456
    ports:
      - 3306:3306
    volumes:
      - /usr/local/mysql/conf:/etc/mysql/conf.d
      - /usr/local/mysql/data:/var/lib/mysql
    networks:                     # 加入指定网络组
      - ab
  redis:
    image: redis:4.0.1
    container_name: xhfredis      # 设置 redis 的容器名称为 xhfredis
    ports:
      - 6379:6379
    volumes:
      - /usr/local/redis/data:/data
      - /usr/local/redis/conf/redis.conf:/etc/redis/redis.conf
    networks:                     # 加入指定网络组
      - ab
    command: redis-server /etc/redis/redis.conf
networks:                         # 添加网络组信息
  ab:

7.6.2 启动所有容器

        这里通过 -f 选项指定要使用的 compose 文件,如下所示:

# 验证文件格式,有问题就及时改正
[root@localhost xhf_test]# docker-compose -f compose2.yml config -q
service "redis" refers to undefined network ab: invalid compose project
[root@localhost xhf_test]# vi compose2.yml
[root@localhost xhf_test]# docker-compose -f compose2.yml config -q

# 启动
[root@localhost xhf_test]# docker-compose -f compose2.yml up -d
[+] Running 19/19
 ✔ redis 6 layers [⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                18.7s 
   ✔ 065132d9f705 Already exists                                                  0.0s 
   ✔ be9835c27852 Already exists                                                  0.0s 
   ✔ f4a0d1212c38 Already exists                                                  0.0s 
   ✔ ea1f878b621a Already exists                                                  0.0s 
   ✔ 7a838393b4b9 Already exists                                                  0.0s 
   ✔ 9f48e489da12 Already exists                                                  0.0s 
 ✔ mysql 11 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                          15.5s 
   ✔ 35b2232c987e Pull complete                                                   4.2s 
   ✔ fc55c00e48f2 Pull complete                                                   4.3s 
   ✔ 0030405130e3 Pull complete                                                   4.6s 
   ✔ e1fef7f6a8d1 Pull complete                                                   4.7s 
   ✔ 1c76272398bb Pull complete                                                   4.8s 
   ✔ f57e698171b6 Pull complete                                                   5.8s 
   ✔ f5b825b269c0 Pull complete                                                   5.8s 
   ✔ dcb0af686073 Pull complete                                                   5.9s 
   ✔ 27bbfeb886d1 Pull complete                                                  12.5s 
   ✔ 6f70cc868145 Pull complete                                                  12.6s 
   ✔ 1f6637f4600d Pull complete                                                  12.6s 
[+] Building 15.6s (7/7) FINISHED                                                      
 => [app internal] load build definition from Dockerfile                          0.0s
 => => transferring dockerfile: 209B                                              0.0s
 => [app internal] load .dockerignore                                             0.0s
 => => transferring context: 2B                                                   0.0s
 => [app internal] load metadata for docker.io/library/java:8                    15.6s
 => [app internal] load build context                                             0.0s
 => => transferring context: 120B                                                 0.0s
 => [app 1/2] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940  0.0s
 => CACHED [app 2/2] COPY *.jar /app.jar                                          0.0s
 => [app] exporting to image                                                      0.0s
 => => exporting layers                                                           0.0s
 => => writing image sha256:02f0a157ab34f13ef6847060a96acf94b3f3903d0c0d409b9692  0.0s
 => => naming to docker.io/library/xhf_test-app                                   0.0s
[+] Running 5/5
 ✔ Network xhf_test_ab         Created                                            0.3s 
 ✔ Container xhfmysql          Started                                            0.7s 
 ✔ Container xhf_test-redis-1  Recreated                                          0.2s 
 ✔ Container xhf-project02     Started                                            0.9s 
 ✔ Container xhfredis          Started                                            0.4s 

7.6.3 查看容器

[root@localhost xhf_test]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                       NAMES
89c90b06bac6   xhf_test-app   "java -jar /app.jar …"   26 seconds ago   Up 24 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   xhf-project02
94e2c98281d7   redis:4.0.1    "docker-entrypoint.s…"   26 seconds ago   Up 25 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   xhfredis
93dbcd634d7e   mysql:5.6      "docker-entrypoint.s…"   26 seconds ago   Up 24 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   xhfmysql

7.6.4 查看网络

        此时查看网络可以看到,又新生成一个网络。对于生成的网络,其生成的名称为"当前compose 文件所在目录名_networks 下的第一级属性”。如下所示:

[root@localhost xhf_test]# docker network ls
NETWORK ID     NAME               DRIVER    SCOPE
ae1e997a44c1   bridge             bridge    local
e2ea71d5a587   host               host      local
33960260d2b0   none               null      local
8dd3b5c5c231   xhf_test_ab        bridge    local
[

7.6.5 查看网络详情

        通过 docker network inspect finance ab 命令,查看该网络的详情,可以看到,compose中定义的三个服务容器都在该网终中。

[root@localhost xhf_test]# docker network inspect xhf_test_ab
[
    {
        "Name": "xhf_test_ab",
        "Id": "8dd3b5c5c231298f5fa8a0a6c755d45f19bff626fdc7f789c9ea92eabb66436a",
        "Created": "2023-07-24T20:08:00.415830596-07:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "89c90b06bac64ee91b5a4175fba43e7efedeb5816f7eff1374b72fcb3d00965b": {
                "Name": "xhf-project02",
                "EndpointID": "f3867bf727a46e9d49f3e424f8fe1f9bfeb0a6a6e3dfad6e6dd1ff82ede036bc",
                "MacAddress": "02:42:ac:13:00:04",
                "IPv4Address": "172.19.0.4/16",
                "IPv6Address": ""
            },
            "93dbcd634d7e47b891c268ee109df5c38fb78e151d61c50c37cc1a9b3ffe2151": {
                "Name": "xhfmysql",
                "EndpointID": "2c17de705bd077f75bd589bdc2474620a7ba36fafc1aa6423aef81a4e6eda99a",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "94e2c98281d7c0200b154afb962e913b06f17dd4f8733614774ff295b4ab1e4e": {
                "Name": "xhfredis",
                "EndpointID": "f74110c2c78d8d4c12b1b573d731d32f3f38db253e48e69b491cbad3a9769802",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "ab",
            "com.docker.compose.project": "xhf_test",
            "com.docker.compose.version": "2.20.0"
        }
    }
]

你可能感兴趣的:(docker,docker)