<p>The Dockerfile specifies how a container image is <em>built</em>.  The Dockerfile’s <code>RUN</code> command is trying to ping the <code>db</code> container, which is not yet running during the build phase.</p>
<p>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 (<code>db</code> and <code>web</code>).</p>
<p>To accomplish what you want, an adjustment to your Dockerfile will need to be made.  Replace <code>RUN</code> with <code>CMD</code>.  This will allow the container to execute your ping command when it launches.</p>
<pre><code>FROM alpine
CMD ping -c 1 db
</code></pre>
<p>Now your <code>docker-compose up</code> will build the web image, launch the <code>db</code> container and then launch your <code>web</code> container which will successfully execute a ping:</p>
<pre><code>&gt; docker-compose up
Creating network &quot;simple_default&quot; with the default driver
Building web
Step 1/2 : FROM alpine
 ---&gt; a24bb4013296
Step 2/2 : CMD ping -c 1 db
 ---&gt; Running in 0c210f0936fd
Removing intermediate container 0c210f0936fd
 ---&gt; 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 (172.19.0.2): 56 data bytes
web_1  | 64 bytes from 172.19.0.2: 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
</code></pre>

CC BY-SA 4.0
2020-09-18T08:14:50.900Z
perhaps you could add a word about how [depends_on](https://docs.docker.com/compose/compose-file/#depends_on) could be used to explicitly state their dependency?
Bruce Becker
CC BY-SA 4.0
2020-09-18T20:31:18.533Z
Like I said, `depends_on` has no effect.
ki9
CC BY-SA 4.0
2020-09-18T20:38:06.987Z
Why isn't db running?
ki9
CC BY-SA 4.0
2020-09-23T03:18:59.983Z
I believe since `docker-compose` `version: "3"` that `depends_on:` has been deprecated.
CoolElectricity
CC BY-SA 4.0
2020-09-23T13:12:12.660Z
@D0ck3rN00b13 The current docs don't seem to indicate deprecation: https://docs.docker.com/compose/compose-file/#depends_on
swysocki
CC BY-SA 4.0
2020-09-23T14:20:06.533Z
Ah, you are correct. I picked up that idea from a random post I came across that implied as much, but according to the documentation its actually changed, rather than deprecated:

> Version 3 no longer supports the condition form of depends_on.
>The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.`
CoolElectricity
CC BY-SA 4.0
2020-09-24T01:42:19.953Z
@swysocki Thanks for [this edit](https://devops.stackexchange.com/revisions/12435/2): switching to a `CMD` directive solved it.  For the record, the latest postgres image also requires the `POSTGRES_PASSWORD` env var.  I will add that into the question.
ki9
CC BY-SA 4.0
2020-09-18T01:41:56.757Z
2020-09-22T19:44:49.707Z
2020-09-22T19:44:49.707Z
swysocki
<p>According to the <a href="https://docs.docker.com/compose/networking/" rel="nofollow noreferrer">Docker Compose Docs: Networking</a>:</p>
<blockquote>
<p>By default Compose sets up a single <a href="https://docs.docker.com/engine/reference/commandline/network_create/" rel="nofollow noreferrer">network</a> for your app. Each container for a service joins the default network and is both <em>reachable</em> by other containers on that network, and <em>discoverable</em> by them at a hostname identical to the container name.</p>
<p>For example, suppose your app is in a directory called <code>myapp</code>, and your <code>docker-compose.yml</code> looks like this:</p>
<pre><code>version: &quot;3&quot;
services:
  web:
    build: .
    ports:
      - &quot;8000:8000&quot;
    depends_on: 
      - db
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: testpass
    ports:
      - &quot;8001:5432&quot;
</code></pre>
<p>When you run docker-compose up, the following happens:</p>
<ul>
<li>A network called <code>myapp_default</code> is created.</li>
<li>A container is created using <code>web</code>’s configuration. It joins the network <code>myapp_default</code> under the name <code>web</code>.</li>
<li>A container is created using <code>db</code>’s configuration. It joins the network <code>myapp_default</code> under the name <code>db</code>.</li>
</ul>
</blockquote>
<p>I put that exact <code>docker-compose.yml</code> in a directory with this <code>Dockerfile</code>:</p>
<pre><code>FROM alpine
RUN ping -c 1 db
</code></pre>
<p>When I run <code>docker-compose up</code>, I get a connection error to the db container.</p>
<pre><code>$ 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
 ---&gt; a24bb4013296
Step 2/2 : RUN ping -c 1 db
 ---&gt; 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
</code></pre>
<p>If I put a node app in the Dockerfile, I can't make database connections to the db URI at <code>postgres://postgres:postgres@db/postgres</code> either.</p>
<p>I've tried using <code>links</code> and <code>depends_on</code> as well as the <code>healthcheck</code> for postgres advised in other SE answers.  (EDIT: Added <code>depends_on</code> to compose file).  Nothing has worked.  Why can't I connect to the <code>db</code> container?</p>

<docker-compose>
Docker-compose containers can't connect to each other
ki9
# Docker-compose containers can't connect to each other

(Asked by: username_0 on 2020-09-17)

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"
services:
  web:
    build: .
    ports:
      - "8000:8000"
    depends_on: 
      - db
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: testpass
    ports:
      - "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?

**Tags:** docker-compose


---

## 1 answers

### Answer by username_1 on 2020-09-18. Score: 7 | Accepted answer

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 (172.19.0.2): 56 data bytes
web_1  | 64 bytes from 172.19.0.2: 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

> **Comments:**
> **username_2**: perhaps you could add a word about how [depends_on](https://docs.docker.com/compose/compose-file/#depends_on) could be used to explicitly state their dependency?  
> **username_0**: Like I said, `depends_on` has no effect.  
> **username_0**: Why isn't db running?  
> **username_3**: I believe since `docker-compose` `version: "3"` that `depends_on:` has been deprecated.  
> **username_1**: @D0ck3rN00b13 The current docs don't seem to indicate deprecation: https://docs.docker.com/compose/compose-file/#depends_on  
> **username_3**: Ah, you are correct. I picked up that idea from a random post I came across that implied as much, but according to the documentation its actually changed, rather than deprecated:  > Version 3 no longer supports the condition form of depends_on. >The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.`  
> **username_0**: @username_1 Thanks for [this edit](https://devops.stackexchange.com/revisions/12435/2): switching to a `CMD` directive solved it.  For the record, the latest postgres image also requires the `POSTGRES_PASSWORD` env var.  I will add that into the question.  

---

