Phoenix Docker

Hello!

I’m trying to dockerize a Phoenix app and it’s not working.

I’m using this image for the app:

And this one for postgres:

This is my dockerfile:


FROM bitwalker/alpine-elixir-phoenix:latest

#Set exposed ports

EXPOSE 5000

ENV PORT=5000 MIX_ENV=dev

#Install postgres client

RUN apk --update add postgresql-client

#Cache elixir deps

ADD mix.exs mix.lock ./

RUN mix do deps.get, deps.compile

#Same with npm deps

ADD assets/package.json assets/

RUN cd assets && \

npm install

ADD . .

USER default

CMD ["./entrypoint.sh"]

And this is my docker-compose file


version: "3"

services:

app:

image: dockerfile

build: .

ports:

-"4000:4000"

volumes:

-./src:/app

depends_on:

-db

environment:

PGUSER: postgres

PGPASSWORD: postgres

PGDATABASE: lists_dev

PGPORT: 5432

db:

image: postgres:10

environment:

POSTGRES_PASSWORD: postgres

POSTGRES_USER: postgres

PGDATA: /var/lib/postgresql/data

PGDATABASE: lists_dev

PGPORT: 5432

restart: always

This is entrypoing.sh


#!/bin/bash

#Docker entrypoint script.

#Wait until Postgres is ready

while pg_isready -q -p $PGPORT -U $PGUSER

do

echo "$(date) - waiting for database to start"

sleep 2

done

#Create, migrate, and seed database if it doesn't exist.

if [[ -z `psql -Atqc "\\list $PGDATABASE"` ]]; then

echo "Database $PGDATABASE does not exist. Creating..."

#createdb -E UTF8 $PGDATABASE -l en_US.UTF-8 -T template0

exec mix ecto.create

exec mix ecto.migrate

exec mix run priv/repo/seeds.exs

echo "Database $PGDATABASE created."

fi

exec mix phx.server

This is the error:

db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2020-12-30 02:23:12.160 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-12-30 02:23:12.160 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-12-30 02:23:12.170 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-12-30 02:23:12.197 UTC [25] LOG:  database system was shut down at 2020-12-30 02:23:01 UTC
db_1   | 2020-12-30 02:23:12.206 UTC [1] LOG:  database system is ready to accept connections
app_1  | psql: error: could not connect to server: No such file or directory
app_1  | 	Is the server running locally and accepting
app_1  | 	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
app_1  | Database lists_dev does not exist. Creating...
app_1  | ** (File.Error) could not make directory (with -p) "/opt/app/_build/dev/lib/lists/ebin": permission denied
app_1  |     (elixir 1.10.4) lib/file.ex:314: File.mkdir_p!/1
app_1  |     (mix 1.10.4) lib/mix/project.ex:667: Mix.Project.build_structure/2
app_1  |     (mix 1.10.4) lib/mix/tasks/compile.all.ex:24: Mix.Tasks.Compile.All.run/1
app_1  |     (mix 1.10.4) lib/mix/task.ex:330: Mix.Task.run_task/3
app_1  |     (mix 1.10.4) lib/mix/tasks/compile.ex:96: Mix.Tasks.Compile.run/1
app_1  |     (mix 1.10.4) lib/mix/task.ex:330: Mix.Task.run_task/3
app_1  |     lib/mix/ecto.ex:68: Mix.Ecto.ensure_repo/2
app_1  |     lib/mix/tasks/ecto.create.ex:50: anonymous fn/3 in Mix.Tasks.Ecto.Create.run/1
lists_app_1 exited with code 1

It could be an error from the image that I’m using or there is an error?

Thanks !

I believe you have a networking issue with docker compose. So the failure you are getting is related to creating the database within your entrypoint.sh script. This is happening in container A. The database exists in container B and you need to connect to it via the network.

The first error that stuck out to me is the difference in directories listed on line 6 and line 11.
6: “/var/run/postgresql/.s.PGSQL.5432”
11: “/tmp/.s.PGSQL.5432”

We should be looking in the same place.

If you read through the docker compose links for networking it should be a bit clearer what’s going wrong.

I would also take a look at their docs on controlling startup order for containers, they have a good example that covers exactly what you are trying to do.

You need to connect through hostname, not through socket. Host in your case is db.

1 Like