# Docker container cannot access postgres

**URL:** <https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489>\
**Category:** Questions\
**Tags:** docker, phoenix\
**Created:** [September 21, 2020, 6:38am UTC](https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489 "2020-09-21T06:38:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Papillon6814](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/papillon6814/32/22367_2.png) [@Papillon6814](https://forum.elixirforum.com/u/Papillon6814)\
**Post date:** [September 21, 2020, 6:38am UTC](https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489/1 "2020-09-21T06:38:41Z")

</div>

Hello.  
I’m trying to make a container of database server which is in phoenix.

`Dockerfile`

```elixir
FROM erlang:23

# elixir expects utf8.
ENV ELIXIR_VERSION="v1.10.4" \
	LANG=C.UTF-8

RUN set -xe \
	&& ELIXIR_DOWNLOAD_URL="https://github.com/elixir-lang/elixir/archive/${ELIXIR_VERSION}.tar.gz" \
	&& ELIXIR_DOWNLOAD_SHA256="8518c78f43fe36315dbe0d623823c2c1b7a025c114f3f4adbb48e04ef63f1d9f" \
	&& curl -fSL -o elixir-src.tar.gz $ELIXIR_DOWNLOAD_URL \
	&& echo "$ELIXIR_DOWNLOAD_SHA256 elixir-src.tar.gz" | sha256sum -c - \
	&& mkdir -p /usr/local/src/elixir \
	&& tar -xzC /usr/local/src/elixir --strip-components=1 -f elixir-src.tar.gz \
	&& rm elixir-src.tar.gz \
	&& cd /usr/local/src/elixir \
	&& make install clean

WORKDIR /app

RUN apt-get -y update &&\
    mix local.hex --force && \
    mix archive.install hex phx_new --force

```

`docker-compose.yml`

```elixir
version: '3'

services:
  PostgreSQL:
    image: postgres
    container_name: postgres
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
    hostname: localhost
    restart: always
    user: root
  elixir:
    build: .
    ports:
      - 4040:4000
    container_name: elixir
    volumes:
      - .:/app
    depends_on:
      - PostgreSQL
    command: /bin/bash
    tty: true
    stdin_open: true

```

In my `Dockerfile`, I can not use `elixir:10.4` image directly due to erlang version. I copied the content of `Dockerfile` from [docker-elixir](https://github.com/c0b/docker-elixir/blob/a8d582c328db5864a4e8e5f869900e3a52265f38/1.10/Dockerfile).

I can run phoenix server without ecto, but cannot run with ecto… When the server access to postgres, it shows errors.

```elixir
[error] `inotify-tools` is needed to run `file_system` for your system, check https://github.com/rvoicilas/inotify-tools/wiki for more information about how to install it. If it's already installed but not be found, appoint executable file with `config.exs` or `FILESYSTEM_FSINOTIFY_EXECUTABLE_FILE` env.
[warn] Could not start Phoenix live-reload because we cannot listen to the file system.
You don't need to worry! This is an optional feature used during development to
refresh your browser when you save files and it does not affect production.

[error] Postgrex.Protocol (#PID<0.383.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.389.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.386.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.380.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.388.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.382.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.385.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.381.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.387.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.384.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[info] Running MilkWeb.Endpoint with cowboy 2.8.0 at 0.0.0.0:4000 (http)
[info] Access MilkWeb.Endpoint at http://localhost:4000
[error] Postgrex.Protocol (#PID<0.386.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.383.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.380.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.389.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.388.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.381.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.385.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.384.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.382.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.387.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

```

I’ve checked database config in my dev.exs.

```elixir
use Mix.Config

# Configure your database
config :milk, Milk.Repo,
  username: "postgres",
  password: "postgres",
  database: "milk_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

```

I entered these command in the phoenix root directory.(The Dockerfile and docker-compose.yml exist there)

```elixir
> docker-compose up -d --build 15:32
Building elixir
Step 1/5 : FROM erlang:23
 ---> d827a7cccc2f
Step 2/5 : ENV ELIXIR_VERSION="v1.10.4" LANG=C.UTF-8
 ---> Using cache
 ---> 12333fd4f990
Step 3/5 : RUN set -xe && ELIXIR_DOWNLOAD_URL="https://github.com/elixir-lang/elixir/archive/${ELIXIR_VERSION}.tar.gz" && ELIXIR_DOWNLOAD_SHA256="8518c78f43fe36315dbe0d623823c2c1b7a025c114f3f4adbb48e04ef63f1d9f" && curl -fSL -o elixir-src.tar.gz $ELIXIR_DOWNLOAD_URL && echo "$ELIXIR_DOWNLOAD_SHA256 elixir-src.tar.gz" | sha256sum -c - && mkdir -p /usr/local/src/elixir && tar -xzC /usr/local/src/elixir --strip-components=1 -f elixir-src.tar.gz && rm elixir-src.tar.gz && cd /usr/local/src/elixir && make install clean
 ---> Using cache
 ---> adde3f1e08c1
Step 4/5 : WORKDIR /app
 ---> Using cache
 ---> 9465f23940f9
Step 5/5 : RUN apt-get -y update && mix local.hex --force && mix archive.install hex phx_new --force
 ---> Using cache
 ---> d44a916ff543

Successfully built d44a916ff543
Successfully tagged milk_elixir:latest
postgres is up-to-date
elixir is up-to-date

> docker ps -a 15:36
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce17c652d127 postgres "docker-entrypoint.s…" 25 minutes ago Up 25 minutes 0.0.0.0:5432->5432/tcp postgres
3bc705d314fc milk_elixir "/bin/bash" 34 minutes ago Up 34 minutes 0.0.0.0:4040->4000/tcp elixir

> docker exec -it elixir bash 15:36
root@3bc705d314fc:/app# mix phx.server
[error] `inotify-tools` is needed to run `file_system` for your system, check https://github.com/rvoicilas/inotify-tools/wiki for more information about how to install it. If it's already installed but not be found, appoint executable file with `config.exs` or `FILESYSTEM_FSINOTIFY_EXECUTABLE_FILE` env.
[warn] Could not start Phoenix live-reload because we cannot listen to the file system.
You don't need to worry! This is an optional feature used during development to
refresh your browser when you save files and it does not affect production.

[error] Postgrex.Protocol (#PID<0.388.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.385.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.387.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.381.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.383.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.386.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.384.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.389.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.382.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[error] Postgrex.Protocol (#PID<0.380.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
[info] Running MilkWeb.Endpoint with cowboy 2.8.0 at 0.0.0.0:4000 (http)
[info] Access MilkWeb.Endpoint at http://localhost:4000
^C

```

I don’t know where the setting is incorrect.  
I can show any information about the project. Help me!

---

<div class="post-metadata">

**Author:** ![darnahsan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/darnahsan/32/17164_2.png) [@darnahsan](https://forum.elixirforum.com/u/darnahsan)\
**Post date:** [September 21, 2020, 6:42am UTC](https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489/2 "2020-09-21T06:42:50Z")

</div>

> [@Papillon6814](#):
>
> `Could not start Phoenix live-reload because we cannot listen to the file system.`

> **[Installation — Phoenix v1.8.8](https://phoenix.hexdocs.pm/installation.html)**

> Mac OS X and Windows users already have a filesystem watcher but Linux users must install inotify-tools.

You might need inotify-tools as in dev mode they are required to provide hot reloading for phoenix

---

<div class="post-metadata">

**Author:** ![muelthe](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/muelthe/32/35411_2.png) [@muelthe](https://forum.elixirforum.com/u/muelthe)\
**Post date:** [September 21, 2020, 6:56am UTC](https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489/3 "2020-09-21T06:56:04Z")

</div>

A couple of things that I can think of to check, initially:

- Is the **postgres** container starting/running correctly? `docker container ls` and `docker container logs <container_name>` could help.
- If the **postgres** container is starting, can you connect via bash (or whatever your flavour is depending on OS etc.) and connect to the psql service, or if you prefer using the tools, pgadmin.

**Edit:** apologies, I see that your logs show the container is running (it’s still early for me 😉 )

---

<div class="post-metadata">

**Author:** ![jukvalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jukvalim/32/19199_2.png) [@jukvalim](https://forum.elixirforum.com/u/jukvalim)\
**Post date:** [September 21, 2020, 7:10am UTC](https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489/4 "2020-09-21T07:10:35Z")

</div>

Docker containers run via docker-compose are not by default accessible to each other using “localhost” hostname, to reach a container from another container you need to use a hostname that is the [same as container name](https://docs.docker.com/compose/networking/). So you need to configure your database like this.

```elixir
username: "postgres",
password: "postgres",
database: "milk_dev",
hostname: "postgres",

```

Might be you also need to remove “hostname: localhost” from PostgreSQL service configuration.

---

<div class="post-metadata">

**Author:** ![Papillon6814](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/papillon6814/32/22367_2.png) [@Papillon6814](https://forum.elixirforum.com/u/Papillon6814)\
**Post date:** [September 21, 2020, 11:04am UTC](https://forum.elixirforum.com/t/docker-container-cannot-access-postgres/34489/5 "2020-09-21T11:04:46Z")

</div>

Thank you!  
It worked!
