docker-compose hostname to communicate between containers works with postgres but not app

Aug*_*ger 5 docker docker-network

I have the following docker-compose.yml:

services:
  postgres:
    image: "postgres:11.0-alpine"
  app:
    build: .
    ports:
      - "4000:4000"
    depends_on:
      - postgres
      - nuxt
  nuxt:
    image: node:latest
    ports:
      - "3000:3000"
Run Code Online (Sandbox Code Playgroud)

I need nuxt service to communicate with app.

Within the nuxt service (docker-compose run --rm --service-ports nuxt bash), if I run

root@62cafc299e8a:/app# ping postgres 
PING postgres (172.18.0.2) 56(84) bytes of data.
64 bytes from avril_postgres_1.avril_default (172.18.0.2): icmp_seq=1 ttl=64 time=0.283 ms
64 bytes from avril_postgres_1.avril_default (172.18.0.2): icmp_seq=2 ttl=64 time=0.130 ms
64 bytes from avril_postgres_1.avril_default (172.18.0.2): icmp_seq=3 ttl=64 time=0.103 ms
Run Code Online (Sandbox Code Playgroud)

but if I do:

root@62cafc299e8a:/app# ping app     
ping: app: No address associated with hostname
Run Code Online (Sandbox Code Playgroud)

Why does it work for postgres but not with app?

If I do docker network inspect 4fcb63b4b1c9, they appear to all be on the same network:

[
    {
        "Name": "myapp_default",
        "Id": "4fcb63b4b1c9fe37ebb26e9d4d22c359c9d5ed6153bd390b6f0b63ffeb0d5c37",
        "Created": "2019-05-16T16:46:27.820758377+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "53b726bdd01159b5f18e8dcb858e979e6e2f8ef68c62e049b824899a74b186c3": {
                "Name": "myapp_app_run_c82e91ca4ba0",
                "EndpointID": "b535b6ca855a5dea19060b2f7c1bd82247b94740d4699eff1c8669c5b0677f78",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "62cafc299e8a90fd39530bbe4a6af8b86098405e54e4c9e61128539ffd5ba928": {
                "Name": "myapp_nuxt_run_3fb01bb2f778",
                "EndpointID": "7eb8f5f8798baee4d65cbbfe5f0f5372790374b48f599f32490700198fa6d54c",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "9dc1c848b2e347876292650c312e8aaf3f469f2efa96710fb50d033b797124b4": {
                "Name": "myapp_postgres_1",
                "EndpointID": "a925438ad5644c03731b7f7c926cff095709b2689fd5f404e9ac4e04c2fbc26a",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "myapp",
            "com.docker.compose.version": "1.23.2"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

So why is that? Also tried with aliases, without success. :(

Pie*_* B. 7

您的app容器很可能没有运行。它的出现docker network inspect表示该容器存在,但可以退出(即未运行)。您可以使用进行检查docker ps -a,例如:

$ docker ps -a
CONTAINER ID  ...  STATUS   ...                           NAMES
fe908e014fdd       Exited (0) Less than a second ago      so_app_1
3b2ca418c051       Up 2 minutes                           so_postgres_1
Run Code Online (Sandbox Code Playgroud)
  • 容器app存在但未运行:即使网络中存在容器,您也无法对其执行ping操作
  • 容器postgres存在并正在运行:您将能够对其执行ping操作

这可能是由于docker-compose run --rm --service-ports nuxt bash 只能创建和运行nuxt容器,而不能运行app也不postgres您可以ping通,postgres因为它在您使用之前已经在运行docker-compose run nuxt bash

为了ping在运行之后能够访问其他容器docker-compose run nuxt ...,您应该:

  • 使其他容器之前已经运行(例如使用docker-compose up -d
  • 使其他容器depends_on成为您要运行的容器,例如:
    nuxt:
      image: node:latest
      ports:
        - "3000:3000"
      # this will ensure posgres and app are run as well when using docker-compose run
      depends_on:
        - app
        - nuxt
    
    Run Code Online (Sandbox Code Playgroud)

即使这样,您的容器也可能无法启动(或在启动后立即退出),并且您将无法ping通它。检查docker ps -a它是否正在运行,并docker logs查看为什么它可能退出了。