写点什么

【docker 总结】第六篇 - DockerCompose

作者:Brave
  • 2021 年 12 月 18 日
  • 本文字数:14052 字

    阅读完需:约 46 分钟

一,compose 简介


compose 译为:组合,它可以将多个容器组合到一起;


比如:制做一个网站,会包含若干服务,数据库 mysql,MongoDB,redis,nginx,java 等;

这些服务虽然也可以装到一个容器中,但是一般并不会这样做;

因为,如果两个服务都用到了 jdk,但版本不一致,放到一起就会引发冲突,导致问题;

所以,需要每个服务对应一个镜像,每个服务都是一个独立的容器;这样就至少需要 3 个容器;


但是,这些服务应该是一个整体:

先启动数据库,再启动 web 容器,再启动 nginx,

在客户端机型访问,他们是相互协作,启动时是有顺序的,


所以,希望能够将他们作为一个整体,一起按照顺序进行启动和关闭操作;

如何能够实现这种编排?这就需要使用到 docker-compose;


二,编排


编排是什么,为什么需要编排?


一个复杂的系统,是需要进行规划的,比如:

1,考虑选择哪些镜像和容器;

2,按照合适的顺序启动这些容器;

3,管理容器中的服务器;

4,如果服务器宕机了,需要复杂重启;

5,如果服务器负载太大,需要动态扩容;

6,如果程序更新了,还要动态升级镜像和容器和服务;


这样,可以将代码打包到镜像中,然后再启动;

如果源代码发生改变,比如添加了新的功能,那么就需要重新打包镜像;


所以,这种操作最好需要工具辅助完成,比如:docker-compose、K8S;


三,compose


  • Compose 通过一个配置文件来管理多个 Docker 容器

  • 在配置文件中,所有的容器通过 services 来定义,然后使用 docker-compose 脚本来启动、停止和重启应用和应用中的服务以及所有依赖服务的容器

  • 步骤:

  • 最后,运行 docker-compose up,Compose 将启动并运行整个应用程序 配置文件组成

  • services 可以定义需要的服务,每个服务都有自己的名字、使用的镜像、挂载的数据卷所属的网络和依赖的其它服务

  • networks 是应用的网络,在它下面可以定义使用的网络名称,类性

  • volumes 是数据卷,可以在此定义数据卷,然后挂载到不同的服务上面使用


安装 compose

mac 安装包中已经自带,无需安装


centos 需要安装:

yum -y install epel-releaseyum -y install python-pipyum clean allpip install docker-compose
复制代码

编写 docker-compose.yml

  • 在 docker-compose.yml 中定义组成应用程序的服务,以便它们可以在隔离的环境中一起运行

  • 空格缩进表示层次

  • 冒号空格后面有空格


version: '2'					services:  nginx1:    image: nginx    ports:      - "8080:80"  nginx2:    image: nginx    ports:      - "8081:80"
复制代码

version 必须是字符串

BravedeMacBook-Pro:~ brave$ mkdir docker_nginxBravedeMacBook-Pro:~ brave$ cd docker_nginx/BravedeMacBook-Pro:docker_nginx brave$ vi docker-compose.ymlBravedeMacBook-Pro:docker_nginx brave$ docker-compose up[+] Running 3/3 ⠿ Network docker_nginx_default     Created                                    0.1s ⠿ Container docker_nginx_nginx1_1  Creat...                                   0.2s ⠿ Container docker_nginx_nginx2_1  Creat...                                   0.2sAttaching to nginx1_1, nginx2_1
复制代码


访问 nginx 服务




问题:控制台会卡住,一旦退出服务就会停止,不能继续访问

^CGracefully stopping... (press Ctrl+C again to force)[+] Running 2/2 ⠿ Container docker_nginx_nginx1_1  Stopped                                         0.3s ⠿ Container docker_nginx_nginx2_1  Stopped                                         6.4scanceled
复制代码


可以在后台启动服务:

BravedeMacBook-Pro:docker_nginx brave$ docker-compose up -d[+] Running 2/2 ⠿ Container docker_nginx_nginx2_1  Started                                         0.8s ⠿ Container docker_nginx_nginx1_1  Started
复制代码

打印当前容器

BravedeMacBook-Pro:docker_nginx brave$ docker-compose psNAME                    COMMAND                  SERVICE             STATUS              PORTSdocker_nginx_nginx1_1   "/docker-entrypoint.…"   nginx1              running             0.0.0.0:8081->80/tcpdocker_nginx_nginx2_1   "/docker-entrypoint.…"   nginx2              running             0.0.0.0:8082->80/tcp
复制代码

停止服务

BravedeMacBook-Pro:docker_nginx brave$ docker-compose stop[+] Running 2/2 ⠿ Container docker_nginx_nginx2_1  Stopped                                         0.2s ⠿ Container docker_nginx_nginx1_1  Stopped                                         0.3s
BravedeMacBook-Pro:docker_nginx brave$ docker-compose psNAME COMMAND SERVICE STATUS PORTSdocker_nginx_nginx1_1 "/docker-entrypoint.…" nginx1 exited (0) docker_nginx_nginx2_1 "/docker-entrypoint.…" nginx2 exited (0)
复制代码

启动服务

BravedeMacBook-Pro:docker_nginx brave$ docker-compose start[+] Running 2/2 ⠿ Container docker_nginx_nginx2_1  Started                                         0.6s ⠿ Container docker_nginx_nginx1_1  Started                                         0.6s  BravedeMacBook-Pro:docker_nginx brave$ docker-compose psNAME                    COMMAND                  SERVICE             STATUS              PORTSdocker_nginx_nginx1_1   "/docker-entrypoint.…"   nginx1              running             0.0.0.0:8081->80/tcpdocker_nginx_nginx2_1   "/docker-entrypoint.…"   nginx2              running             0.0.0.0:8082->80/tcp
复制代码

看日志:

docker-compose logs -f

logs 查看日志

-f 跟踪日志变化 访问服务时,日志动态刷新

BravedeMacBook-Pro:docker_nginx brave$ docker-compose logs -fnginx2_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configurationnginx2_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/nginx2_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.shnginx2_1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.confnginx2_1  | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.confnginx2_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.shnginx2_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.shnginx2_1  | /docker-entrypoint.sh: Configuration complete; ready for start upnginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: using the "epoll" event methodnginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: nginx/1.21.4nginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) nginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: OS: Linux 5.10.76-linuxkitnginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576nginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker processesnginx1_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configurationnginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker process 31nginx1_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/nginx1_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.shnginx2_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker process 32nginx1_1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.confnginx1_1  | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.confnginx1_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.shnginx1_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.shnginx1_1  | /docker-entrypoint.sh: Configuration complete; ready for start upnginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: using the "epoll" event methodnginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: nginx/1.21.4nginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) nginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: OS: Linux 5.10.76-linuxkitnginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576nginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker processesnginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker process 32nginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker process 33nginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker process 34nginx1_1  | 2021/12/14 06:32:55 [notice] 1#1: start worker process 35nginx1_1  | 172.20.0.1 - - [14/Dec/2021:06:35:30 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"nginx1_1  | 2021/12/14 06:35:30 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8081", referrer: "http://localhost:8081/"nginx1_1  | 172.20.0.1 - - [14/Dec/2021:06:35:30 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost:8081/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-"
复制代码

进入服务

BravedeMacBook-Pro:docker_nginx brave$ docker exec -it docker_nginx_nginx1_1 bashroot@d482a80a53a6:/# 
复制代码


排除干扰:

1,停掉并删除所有容器


docker-compose down 停掉并删除所有容器和 compose 所创建的网络

BravedeMacBook-Pro:docker_nginx brave$ docker-compose down[+] Running 3/2 ⠿ Container docker_nginx_nginx2_1  Removed                                                                                                           0.3s ⠿ Container docker_nginx_nginx1_1  Removed                                                                                                           0.3s ⠿ Network docker_nginx_default     Removed    
复制代码

每当通过 compose 启动一堆服务时,默认会创建一个网络


docker container stop `docker container ps -a -q`

stop 停掉容器

BravedeMacBook-Pro:docker_nginx brave$ docker container stop `docker container ps -a -q`3fdb2c77358305a69d0bbfa144aa617a60540ebe1b6be49b706198092f9b980e9f285e936c9ac8a83da8553463de14b7dccd4593629f2934844233f7c6cb1d0358ff180a56dc2015BravedeMacBook-Pro:docker_nginx brave$ docker container rm `docker container ps -a -q`3fdb2c77358305a69d0bbfa144aa617a60540ebe1b6be49b706198092f9b980e9f285e936c9ac8a83da8553463de14b7dccd4593629f2934844233f7c6cb1d0358ff180a56dc2015
复制代码

这时,容器就都没了

BravedeMacBook-Pro:docker_nginx brave$ docker container ps -aCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
复制代码


重新启动

BravedeMacBook-Pro:docker_nginx brave$ docker-compose up -d[+] Running 3/3 ⠿ Network docker_nginx_default     Created                                                                                                           0.1s ⠿ Container docker_nginx_nginx2_1  Started                                                                                                           0.7s ⠿ Container docker_nginx_nginx1_1  Started                                                                                                           0.7s
复制代码

进入容器 docker_nginx_nginx1_1

因为 yaml 文件中没有指定容器的 name,所以默认生成了一个

BravedeMacBook-Pro:docker_nginx brave$ docker exec -it docker_nginx_nginx1_1 bashroot@310f6901b9bd:/# 
复制代码

查看 network:

发现多了一个 bridge 类型的网络: docker_nginx_default

BravedeMacBook-Pro:docker_nginx brave$ docker network lsNETWORK ID     NAME                   DRIVER    SCOPE0f0889985450   bridge                 bridge    locala02b571b9ecb   dev_web                bridge    local3abec53303cd   docker_nginx_default   bridge    local04a72bf29d13   finance_web            bridge    local1b1664b7d542   host                   host      localc57ec20004cf   none                   null      local
复制代码

compose 中的容器都会加入到这个网络中,和其他网络是隔离的


写一个更复杂的,先把这个 down 掉(stop 并 rm 掉)

BravedeMacBook-Pro:docker_nginx brave$ docker-compose down[+] Running 3/2 ⠿ Container docker_nginx_nginx2_1  Removed                                       0.6s ⠿ Container docker_nginx_nginx1_1  Removed                                       0.6s ⠿ Network docker_nginx_default     Removed                                       0.1s
复制代码



直接配置数据卷


两个静态网站,一个前台用,一个后台用

BravedeMacBook-Pro:docker_nginx brave$ mkdir frontBravedeMacBook-Pro:docker_nginx brave$ mkdir backend
BravedeMacBook-Pro:docker_nginx brave$ cd front/BravedeMacBook-Pro:front brave$ echo front > index.html
BravedeMacBook-Pro:front brave$ cd ../backend/BravedeMacBook-Pro:backend brave$ echo backend > index.html
BravedeMacBook-Pro:backend brave$ cd ..BravedeMacBook-Pro:docker_nginx brave$ lsbackend docker-compose.yml front
复制代码

添加 tree 命令


Ubuntu 下可执行下列命令安装

sudo apt-get install tree
复制代码


MacOS 下:


首先,进入家目录

cd $home
复制代码

使用 vim 命令创建一个.bashrc 文件

vim .bashrc
复制代码


在里面添加如下一行代码:

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
复制代码

保存退出后,使用 source .bashrc 命令更新一下脚本资源

source .bashrc
复制代码

在终端下试一下 tree 命令

BravedeMacBook-Pro:~ brave$ cd docker_nginx/BravedeMacBook-Pro:docker_nginx brave$ tree.|____.DS_Store|____backend| |____index.html|____docker-compose.yml|____front| |____index.html
复制代码

通过 docker 启动两个 nginx,一个映射到 front 目录,一个映射到 backend 目录;

当访问第一个容器时,8081,访问的是 front 目录下的 index.html 文件,

当访问第二个容器时,8082,访问的是 backend 目录下的 index.html 文件,


所以,这就需要将宿主机上的一个目录映射给容器中的目录

这样,修改只要在本地改就可以了,容器会到这里来读


拷贝一份目录出来

BravedeMacBook-Pro:~ brave$ cp -r docker_nginx webserverBravedeMacBook-Pro:~ brave$ cd webserver/BravedeMacBook-Pro:webserver brave$ lsbackend			docker-compose.yml	front
复制代码

修改 docker-compose.yml

vi docker-compose.yml
复制代码

添加数据卷的映射

version: '2'services:  nginx1:    image: nginx    volumes:     - "./front:/usr/share/nginx/html"    ports:      - "8081:80"  nginx2:    image: nginx    volumes:     - "./backend:/usr/share/nginx/html"    ports:      - "8082:80"
复制代码

启动 compose

BravedeMacBook-Pro:webserver brave$ docker-compose up[+] Running 3/3 ⠿ Network webserver_default     Created                                   0.1s ⠿ Container webserver_nginx1_1  Created                                   0.2s ⠿ Container webserver_nginx2_1  Created                                   0.2sAttaching to nginx1_1, nginx2_1
复制代码




当修改本地文件内容后,页面内容就会变化



创建并配置数据卷


先停掉 compose:

^CGracefully stopping... (press Ctrl+C again to force)[+] Running 2/2 ⠿ Container webserver_nginx1_1  Stopped                                   0.2s ⠿ Container webserver_nginx2_1  Stopped                                   8.6scanceled
复制代码


修改 docker-compose.yml 文件,添加数据卷:

BravedeMacBook-Pro:webserver brave$ vi docker-compose.yml
复制代码

创建本地数据卷

version: '2'services:  nginx1:    image: nginx    volumes:     - "data:/data"     - "./front:/usr/share/nginx/html"    ports:      - "8081:80"  nginx2:    image: nginx    volumes:     - "data:/data"     // 冒号左侧不是相对路径,data 代表数据卷     - "./backend:/usr/share/nginx/html"    // 冒号左侧可以是相对路径    ports:      - "8082:80"volumes:       // 创建数据卷 data  data:    driver: local
复制代码

后台启动 compose

BravedeMacBook-Pro:webserver brave$ docker-compose up -d[+] Running 3/3 ⠿ Network webserver_default     Created                                   0.1s ⠿ Container webserver_nginx2_1  Started                                   0.7s ⠿ Container webserver_nginx1_1  Started                                   0.8s
复制代码

进入容器 webserver_nginx2_1,在 data 目录下创建一个文件:

BravedeMacBook-Pro:webserver brave$ docker exec -it webserver_nginx2_1 bashroot@a7629261b3a5:/# cd /data/root@a7629261b3a5:/data# lsroot@a7629261b3a5:/data# touch test.txtroot@a7629261b3a5:/data# exitexit
复制代码

再进入容器 webserver_nginx1_1,可以看到刚刚创建的文件,再创建一个文件

BravedeMacBook-Pro:webserver brave$ docker exec -it webserver_nginx1_1 bashroot@b34bfd670a93:/# cd /data/root@b34bfd670a93:/data# lstest.txtroot@b34bfd670a93:/data# touch test2.txtroot@b34bfd670a93:/data# exitexit
复制代码

查看宿主机的 data 目录

BravedeMacBook-Pro:webserver brave$ docker inspect webserver_nginx1_1[    {        "Id": "b34bfd670a93ac29569afe9367485ef381ea9f8aa727af0491a87c0c19f3b624",        "Created": "2021-12-15T03:57:33.8890169Z",        "Path": "/docker-entrypoint.sh",        "Args": [            "nginx",            "-g",            "daemon off;"        ],        "State": {            "Status": "running",            "Running": true,            "Paused": false,            "Restarting": false,            "OOMKilled": false,            "Dead": false,            "Pid": 18514,            "ExitCode": 0,            "Error": "",            "StartedAt": "2021-12-15T03:57:34.6195702Z",            "FinishedAt": "0001-01-01T00:00:00Z"        },        "Image": "sha256:f652ca386ed135a4cbe356333e08ef0816f81b2ac8d0619af01e2b256837ed3e",        "ResolvConfPath": "/var/lib/docker/containers/b34bfd670a93ac29569afe9367485ef381ea9f8aa727af0491a87c0c19f3b624/resolv.conf",        "HostnamePath": "/var/lib/docker/containers/b34bfd670a93ac29569afe9367485ef381ea9f8aa727af0491a87c0c19f3b624/hostname",        "HostsPath": "/var/lib/docker/containers/b34bfd670a93ac29569afe9367485ef381ea9f8aa727af0491a87c0c19f3b624/hosts",        "LogPath": "/var/lib/docker/containers/b34bfd670a93ac29569afe9367485ef381ea9f8aa727af0491a87c0c19f3b624/b34bfd670a93ac29569afe9367485ef381ea9f8aa727af0491a87c0c19f3b624-json.log",        "Name": "/webserver_nginx1_1",        "RestartCount": 0,        "Driver": "overlay2",        "Platform": "linux",        "MountLabel": "",        "ProcessLabel": "",        "AppArmorProfile": "",        "ExecIDs": null,        "HostConfig": {            "Binds": [                "/Users/brave/webserver/front:/usr/share/nginx/html:rw"            ],            "ContainerIDFile": "",            "LogConfig": {                "Type": "json-file",                "Config": {}            },            "NetworkMode": "webserver_default",            "PortBindings": {                "80/tcp": [                    {                        "HostIp": "",                        "HostPort": "8081"                    }                ]            },            "RestartPolicy": {                "Name": "",                "MaximumRetryCount": 0            },            "AutoRemove": false,            "VolumeDriver": "",            "VolumesFrom": null,            "CapAdd": null,            "CapDrop": null,            "CgroupnsMode": "private",            "Dns": null,            "DnsOptions": null,            "DnsSearch": null,            "ExtraHosts": null,            "GroupAdd": null,            "IpcMode": "private",            "Cgroup": "",            "Links": null,            "OomScoreAdj": 0,            "PidMode": "",            "Privileged": false,            "PublishAllPorts": false,            "ReadonlyRootfs": false,            "SecurityOpt": null,            "UTSMode": "",            "UsernsMode": "",            "ShmSize": 67108864,            "Runtime": "runc",            "ConsoleSize": [                0,                0            ],            "Isolation": "",            "CpuShares": 0,            "Memory": 0,            "NanoCpus": 0,            "CgroupParent": "",            "BlkioWeight": 0,            "BlkioWeightDevice": null,            "BlkioDeviceReadBps": null,            "BlkioDeviceWriteBps": null,            "BlkioDeviceReadIOps": null,            "BlkioDeviceWriteIOps": null,            "CpuPeriod": 0,            "CpuQuota": 0,            "CpuRealtimePeriod": 0,            "CpuRealtimeRuntime": 0,            "CpusetCpus": "",            "CpusetMems": "",            "Devices": null,            "DeviceCgroupRules": null,            "DeviceRequests": null,            "KernelMemory": 0,            "KernelMemoryTCP": 0,            "MemoryReservation": 0,            "MemorySwap": 0,            "MemorySwappiness": null,            "OomKillDisable": null,            "PidsLimit": null,            "Ulimits": null,            "CpuCount": 0,            "CpuPercent": 0,            "IOMaximumIOps": 0,            "IOMaximumBandwidth": 0,            "Mounts": [                {                    "Type": "volume",                    "Source": "webserver_data",                    "Target": "/data",                    "VolumeOptions": {}                }            ],            "MaskedPaths": [                "/proc/asound",                "/proc/acpi",                "/proc/kcore",                "/proc/keys",                "/proc/latency_stats",                "/proc/timer_list",                "/proc/timer_stats",                "/proc/sched_debug",                "/proc/scsi",                "/sys/firmware"            ],            "ReadonlyPaths": [                "/proc/bus",                "/proc/fs",                "/proc/irq",                "/proc/sys",                "/proc/sysrq-trigger"            ]        },        "GraphDriver": {            "Data": {                "LowerDir": "/var/lib/docker/overlay2/bdcd67dc5ebc710bb8c4080f04d36ae53992bb08fa8e61c1e72a38b47d302eb4-init/diff:/var/lib/docker/overlay2/98ddd77c17fd0fd4fc6707553e32b49f8e2eba2bc9e70da3d5fca8b29b91d8ec/diff:/var/lib/docker/overlay2/3be50bc643a4970bcfa30f7e96866f443053bd785888275e1e7e3523e09b4360/diff:/var/lib/docker/overlay2/5012d011eb7dbcacff0b847f7e9c0ed198b3449bd343e7fa684896ff85aa9354/diff:/var/lib/docker/overlay2/51f10304ec3ccb9e9a87e92d04b7cd9cd1a7b4cf5ffaf44066d2efe5ab13bde1/diff:/var/lib/docker/overlay2/0cf4f1c2e2da09449d986dae65c19969eb2443a8bdedfd00a21e1a62f60fbed4/diff:/var/lib/docker/overlay2/43f4bcbd57da70e0ba27546bf82a9f3ff49be426b612d7c3a6c07753eebce40f/diff",                "MergedDir": "/var/lib/docker/overlay2/bdcd67dc5ebc710bb8c4080f04d36ae53992bb08fa8e61c1e72a38b47d302eb4/merged",                "UpperDir": "/var/lib/docker/overlay2/bdcd67dc5ebc710bb8c4080f04d36ae53992bb08fa8e61c1e72a38b47d302eb4/diff",                "WorkDir": "/var/lib/docker/overlay2/bdcd67dc5ebc710bb8c4080f04d36ae53992bb08fa8e61c1e72a38b47d302eb4/work"            },            "Name": "overlay2"        },      // 数据卷 Mounts  "Source": "/var/lib/docker/volumes/webserver_data/_data"        "Mounts": [            {                "Type": "bind",                "Source": "/Users/brave/webserver/front",                "Destination": "/usr/share/nginx/html",                "Mode": "rw",                "RW": true,                "Propagation": "rprivate"            },            {                "Type": "volume",                "Name": "webserver_data",                "Source": "/var/lib/docker/volumes/webserver_data/_data",                "Destination": "/data",                "Driver": "local",                "Mode": "z",                "RW": true,                "Propagation": ""            }        ],        "Config": {            "Hostname": "b34bfd670a93",            "Domainname": "",            "User": "",            "AttachStdin": false,            "AttachStdout": true,            "AttachStderr": true,            "ExposedPorts": {                "80/tcp": {}            },            "Tty": false,            "OpenStdin": false,            "StdinOnce": false,            "Env": [                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",                "NGINX_VERSION=1.21.4",                "NJS_VERSION=0.7.0",                "PKG_RELEASE=1~bullseye"            ],            "Cmd": [                "nginx",                "-g",                "daemon off;"            ],            "Image": "nginx",            "Volumes": {                "/data": {},                "/usr/share/nginx/html": {}            },            "WorkingDir": "",            "Entrypoint": [                "/docker-entrypoint.sh"            ],            "OnBuild": null,            "Labels": {                "com.docker.compose.config-hash": "f512730eadababb6d69e9935a86eceed5960e0d3e11f0800b00ab834f34fe9cd",                "com.docker.compose.container-number": "1",                "com.docker.compose.depends_on": "",                "com.docker.compose.image": "sha256:f652ca386ed135a4cbe356333e08ef0816f81b2ac8d0619af01e2b256837ed3e",                "com.docker.compose.oneoff": "False",                "com.docker.compose.project": "webserver",                "com.docker.compose.project.config_files": "/Users/brave/webserver/docker-compose.yml",                "com.docker.compose.project.working_dir": "/Users/brave/webserver",                "com.docker.compose.service": "nginx1",                "com.docker.compose.version": "2.2.1",                "maintainer": "NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e"            },            "StopSignal": "SIGQUIT"        },        "NetworkSettings": {            "Bridge": "",            "SandboxID": "f9f9647096145a373ac3b609c379685c0b1ba8abe185b5df65fe7d9cddfe7d19",            "HairpinMode": false,            "LinkLocalIPv6Address": "",            "LinkLocalIPv6PrefixLen": 0,            "Ports": {                "80/tcp": [                    {                        "HostIp": "0.0.0.0",                        "HostPort": "8081"                    }                ]            },            "SandboxKey": "/var/run/docker/netns/f9f964709614",            "SecondaryIPAddresses": null,            "SecondaryIPv6Addresses": null,            "EndpointID": "",            "Gateway": "",            "GlobalIPv6Address": "",            "GlobalIPv6PrefixLen": 0,            "IPAddress": "",            "IPPrefixLen": 0,            "IPv6Gateway": "",            "MacAddress": "",            "Networks": {                "webserver_default": {                    "IPAMConfig": null,                    "Links": null,                    "Aliases": [                        "webserver_nginx1_1",                        "nginx1",                        "b34bfd670a93"                    ],                    "NetworkID": "051b68777c6dfe07fcae9b75645a2c0ce921757ecf6c47781125d4275a805359",                    "EndpointID": "b45e60702647701e7858d65ff60ae365564a029f539d8b75ef508a98b261cf41",                    "Gateway": "172.23.0.1",                    "IPAddress": "172.23.0.3",                    "IPPrefixLen": 16,                    "IPv6Gateway": "",                    "GlobalIPv6Address": "",                    "GlobalIPv6PrefixLen": 0,                    "MacAddress": "02:42:ac:17:00:03",                    "DriverOpts": null                }            }        }    }]
复制代码

数据卷 Mounts "Source": "/var/lib/docker/volumes/webserver_data/_data"


MAC 独有方式。。。

可以进入 "/var/lib/docker/volumes/webserver_data/_data"查看到这两个文件



创建并配置网络


BravedeMacBook-Pro:webserver brave$ docker-compose down[+] Running 3/3 ⠿ Container webserver_nginx1_1  Removed                                   0.3s ⠿ Container webserver_nginx2_1  Removed                                   0.3s ⠿ Network webserver_default     Removed                                   0.1s  vi docker-compose.yml
复制代码

:set nu 查看行号


  3   nginx1:  4     image: nginx  5     networks:  6      - "myweb"   // myweb 网络  7      - "default" // 默认网络  可写可不写  8     volumes:  9      - "data:/data" 10      - "./front:/usr/share/nginx/html" 11     ports: 12       - "8081:80" 13   nginx2: 14     image: nginx 15     volumes: 16      - "data:/data" 17      - "./backend:/usr/share/nginx/html" 18     ports: 19       - "8082:80" 20 volumes: 21   data: 22     driver: local 23 networks:    // 创建网络 myweb 24   myweb: 25     driver: bridge
复制代码

启动 compose

BravedeMacBook-Pro:webserver brave$ docker compose up -d[+] Running 4/4 ⠿ Network webserver_myweb       Created                                   0.1s ⠿ Network webserver_default     Created                                   0.1s ⠿ Container webserver-nginx1-1  Started                                   0.9s ⠿ Container webserver-nginx2-1  Started                                   0.8s
复制代码

查看网络,创建了 webserver_default、webserver_myweb 两个网络:

BravedeMacBook-Pro:webserver brave$ docker network lsNETWORK ID     NAME                DRIVER    SCOPE0f0889985450   bridge              bridge    locala02b571b9ecb   dev_web             bridge    local04a72bf29d13   finance_web         bridge    local1b1664b7d542   host                host      localc57ec20004cf   none                null      local82fddc470497   webserver_default   bridge    local   // 新创建bb13d861b20a   webserver_myweb     bridge    local	 // 新创建
复制代码


用户头像

Brave

关注

还未添加个人签名 2018.12.13 加入

还未添加个人简介

评论

发布
暂无评论
【docker 总结】第六篇 - DockerCompose