According to the Docker Compose Docs: Networking:

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

For example, suppose your app is in a directory called myapp, and your docker-compose.yml looks like this:

version: "3"
    build: .
      - "8000:8000"
      - db
    image: postgres
      POSTGRES_PASSWORD: testpass
      - "8001:5432"

When you run docker-compose up, the following happens:

  • A network called myapp_default is created.
  • A container is created using web’s configuration. It joins the network myapp_default under the name web.
  • A container is created using db’s configuration. It joins the network myapp_default under the name db.

I put that exact docker-compose.yml in a directory with this Dockerfile:

FROM alpine
RUN ping -c 1 db

When I run docker-compose up, I get a connection error to the db container.

$ docker-compose up
Building web
Step 1/2 : FROM alpine
latest: Pulling from library/alpine
df20fa9351a1: Pull complete
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
 ---> a24bb4013296
Step 2/2 : RUN ping -c 1 db
 ---> Running in cf64bfcb72eb
ping: bad address 'db'
ERROR: Service 'web' failed to build : The command '/bin/sh -c ping -c 1 db' returned a non-zero code: 1

If I put a node app in the Dockerfile, I can't make database connections to the db URI at postgres://postgres:postgres@db/postgres either.

I've tried using links and depends_on as well as the healthcheck for postgres advised in other SE answers. (EDIT: Added depends_on to compose file). Nothing has worked. Why can't I connect to the db container?

1 Answer 1


The Dockerfile specifies how a container image is built. The Dockerfile’s RUN command is trying to ping the db container, which is not yet running during the build phase.

Once the container images have been created, compose will start the containers and they will be able to address each other via their service names (db and web).

To accomplish what you want, an adjustment to your Dockerfile will need to be made. Replace RUN with CMD. This will allow the container to execute your ping command when it launches.

FROM alpine
CMD ping -c 1 db

Now your docker-compose up will build the web image, launch the db container and then launch your web container which will successfully execute a ping:

> docker-compose up
Creating network "simple_default" with the default driver
Building web
Step 1/2 : FROM alpine
 ---> a24bb4013296
Step 2/2 : CMD ping -c 1 db
 ---> Running in 0c210f0936fd
Removing intermediate container 0c210f0936fd
 ---> 7c0ed50ad4e8
Successfully built 7c0ed50ad4e8
Successfully tagged simple_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating simple_db_1 ... done
Creating simple_web_1 ... done
Attaching to simple_db_1, simple_web_1

... output omitted

web_1  | PING db ( 56 data bytes
web_1  | 64 bytes from seq=0 ttl=64 time=0.218 ms
web_1  |
web_1  | --- db ping statistics ---
web_1  | 1 packets transmitted, 1 packets received, 0% packet loss
web_1  | round-trip min/avg/max = 0.218/0.218/0.218 ms
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting default time zone ... Etc/UTC
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
simple_web_1 exited with code 0
  • perhaps you could add a word about how depends_on could be used to explicitly state their dependency? Commented Sep 18, 2020 at 8:14
  • Like I said, depends_on has no effect.
    – ki9
    Commented Sep 18, 2020 at 20:31
  • Why isn't db running?
    – ki9
    Commented Sep 18, 2020 at 20:38
  • I believe since docker-compose version: "3" that depends_on: has been deprecated.
    – K8sN0v1c3
    Commented Sep 23, 2020 at 3:18
  • 1
    @D0ck3rN00b13 The current docs don't seem to indicate deprecation: docs.docker.com/compose/compose-file/#depends_on
    – swysocki
    Commented Sep 23, 2020 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.