docker-compose up 没有找到其他容器主机

docker-compose up doesn't find the other container hosts

由于未知原因,我今天早上在我的项目中尝试 运行 一个 docker-compose up 时 运行 遇到了 docker 错误。

我的 Web 容器无法连接到数据库主机并且 nc 仍在返回

web_1 | nc: bad address 'db'

有我的 docker-compose 定义的相关部分:

version: '3.2'

services:
  web:
    build: ./app
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./app/:/usr/src/app/
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=
      - POSTGRES_PASSWORD=
      - POSTGRES_DB=
  mailhog:
     # mailhog declaration
volumes:
  postgres_data:

我怀疑网络坏了,事实确实如此。这是我检查与该项目相关的 docker 网络时得到的: (docker network inspect my_docker_network)

[
    {
        "Name": "my_docker_network",
        "Id": "f09c148d9f3253d999e276c8b1061314e5d3e1f305f6124666e2e32a8e0d9efd",
        "Created": "2020-11-18T13:30:29.710456682-05: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": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {}, // <=== This is empty !
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "my-project"
        }
    }
]

版本:

Docker:18.09.1,构建 4c52b90

Docker-撰写:1.21.0,构建未知

我能够通过 运行 docker-compose down && docker-compose up 解决这个问题,但如果您的故障删除了所有卷,那么,您的数据...

现在网络检查好了:

[
    {
        "Name": "my_docker_network",
        "Id": "236c45042b03c3a2922d9a9fabf644048901c66b3c1fd15507aca2c464c1d7ef",
        "Created": "2020-12-04T12:04:40.765889533-05: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": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0939787203f2e222f2db380e8d5b36928e95bc7242c58df56b3e6e419efdd280": {
                "Name": "my_docker_db_1",
                "EndpointID": "af206a7e957682d3d9aee2ec0ffae2c51638cbe8821d3b20eb786165a0159c9d",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            },
            "ae90bd27539e89d0b26e0768aec765431ee623f45856e13797f3ba0262cca3f2": {
                "Name": "my_docker_web_1",
                "EndpointID": "09b5cefed6c5b49d31497419fd5784dcd887a23875e6c998209615c7ec8863f4",
                "MacAddress": "02:42:ac:13:00:04",
                "IPv4Address": "172.19.0.4/16",
                "IPv6Address": ""
            },
            "f2d3e46ab544b146bdc0aafba9fddb4e6c9d9ffd02c2015627516c7d6ff17567": {
                "Name": "my_docker_mailhog_1",
                "EndpointID": "242a693e6752f05985c377cd7c30f6781f0576bcd5ffede98f77f82efff8c78f",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "my_docker_project"
        }
    }
]

但是:有人知道发生了什么以及如何防止这个问题再次出现吗?

我有同样的问题,但我的撰写文件中有 rabbitmq 服务。起初我通过删除我机器上所有现有的容器和卷来解决它,(但它又一次发生了)但后来我将 rabbitmq 图像版本更新到 docker-compose.yml:

中的最新版本
image: rabbitmq:latest

之后问题没有再出现...