sc4224

sc4224

Docker-compose phoenix postgresql

I have a problem running my dockerized phoenix application with postgresql.

I specified my docker-compose.yml file to include two services: my phoenix service, build by a dockerfile and a latest postgres image taken from docker hub.

My dockerfile for phoenix looks like this:

# base image elixer to start with
FROM elixir:1.6

# install hex package manager
RUN mix local.hex --force
RUN mix local.rebar --force

# install the latest phoenix 
RUN mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez --force

# create app folder
RUN mkdir /app
COPY ./my_app /app
WORKDIR /app

# install dependencies
RUN mix deps.get

# run phoenix in *dev* mode on port 4000
# CMD mix phx.server

My docker-compose.yml file looks like this:

version: '3.6'
services:
  phoenix:
    build:
      context: .
      dockerfile: Dockerfile.phoenix.development
    ports:
      - 4000:4000
    volumes:
      - ./my_app:/app
    depends_on:
      - db
      - redis
    environment:
      GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
      GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
      FACEBOOK_CLIENT_ID: ${FACEBOOK_CLIENT_ID}
      FACEBOOK_CLIENT_SECRET: ${FACEBOOK_CLIENT_SECRET}
    command: mix ecto.create && mix ecto.migrate && mix phx.server
  db:
    container_name: db
    restart: always
    image: postgres:latest
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD="some_pass"
      - POSTGRES_USER="some_user"
    volumes:
          - ./data/db:/data/db

Here is the error:

phoenix_1  | 
phoenix_1  | 10:07:19.148 [error] GenServer #PID<0.234.0> terminating
phoenix_1  | ** (RuntimeError) Connect raised a KeyError error. The exception details are hidden, as
phoenix_1  | they may contain sensitive data such as database credentials.
phoenix_1  | 
phoenix_1  |     (elixir) lib/keyword.ex:377: Keyword.fetch!/2
phoenix_1  |     (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.startup/2
phoenix_1  |     (postgrex) lib/postgrex/protocol.ex:504: Postgrex.Protocol.handshake/2
phoenix_1  |     (db_connection) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
phoenix_1  |     (connection) lib/connection.ex:622: Connection.enter_connect/5
phoenix_1  |     (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
phoenix_1  | Last message: nil
phoenix_1  | State: Postgrex.Protocol
db         | 2018-07-24 10:07:19.149 UTC [28] LOG:  incomplete startup packet
phoenix_1  | ** (Mix) The database for MyApp.Repo couldn't be created: an exception was raised:
phoenix_1  |     ** (RuntimeError) Connect raised a KeyError error. The exception details are hidden, as
phoenix_1  | they may contain sensitive data such as database credentials.
phoenix_1  | 
phoenix_1  |         (elixir) lib/keyword.ex:377: Keyword.fetch!/2
phoenix_1  |         (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.startup/2
phoenix_1  |         (postgrex) lib/postgrex/protocol.ex:504: Postgrex.Protocol.handshake/2
phoenix_1  |         (db_connection) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
phoenix_1  |         (connection) lib/connection.ex:622: Connection.enter_connect/5
phoenix_1  |         (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

I’m able to run my phoenix app on localhost just fine without docker.

When I set this up with docker and docker-compose, I remember to switch the dev.exs file to reference the host “db”.

I wondering if this is an issue with my postgresql setup or my phoenix app setup

Marked As Solved

NobbZ

NobbZ

I do not understand that one…

ENTRYPOINT /usr/bin/entrypoint.sh
#!/usr/bin/env bash
set -ex

mix ecto.create
mix ecto.migrate

mix phx.server

Also Liked

NobbZ

NobbZ

TL;DR: Set them in a way, that makes you able to keep them in synch easily.


The environment key is always for the surrounding image only.

Usually you are passing those environment variables to the postgresql container to configure it.

Then you have to makje sure that all clients use the same credentials as passed into the postgres container. How you do this often depends on your overall architecture.

Most of the time its just hard-configuring them in the config/<env>.exs or passing them as environment variable into the client container as well.

But vault managed credentials are possible as well. Well, kind of… Changing the credentials for a repo requires it to kill the repos supervisor and restart it with new credentials as far as I understand…

cnck1387

cnck1387

Each service needs its own reference to environment variables. You’ll want to add it to the phoenix service too.

You could also put those ENV variables into a file, such as a .env file and then load it in both services with env_file in compose (instead of inlining and duplicating them with environment).

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement