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"
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
but if I do:
root@62cafc299e8a:/app# ping app     
ping: app: No address associated with hostname
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"
        }
    }
]
So why is that? Also tried with aliases, without success. :(
Containers can only communicate with each other if they share a network. Containers that don't share a network cannot communicate with one another. That's one of the isolation features provided by Docker. A container can belong to more than one network, and a network can have multiple containers inside.
The general goal of the hostname is that computers on the network know each other and thus communicate between themselves. Similarly, the main goal here is to ensure that containers can communicate with each other successfully within a Docker network.
The answer is yes!! But using normal docker run commands, you won't be able to see or interact with the these applications. You need to connect the display with the container in order to do so.
Your app container is most likely not running. Its appearance in docker network inspect means that the container exists but it may be exited (i.e. is not running). You can check with docker ps -a, for example:
$ 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
app exists but is not running: you won't be able to ping it even if it exists in the networkpostgres exists and is running: you will be able to ping itIt's probably due to the fact that docker-compose run --rm --service-ports nuxt bash will only create and run the nuxt container, it won't run app nor postgres. You are able to ping postgres because it was already running before you used docker-compose run nuxt bash
To be able to ping other containers after running docker-compose run nuxt ..., you should either:
docker-compose up -d)depends_on the container you are trying to run, for example:
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
Even with that, your container may fail to start (or exit right after start) and you won't be able to ping it. Check with docker ps -a that it is running and docker logs to see why it may have exited.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With