JulienCorb
I am creating a Docker container for my umbrella app that so far contains just one phoenix API app.
This application has simply one DB table that is made of political parties, called “parties”.
when I run docker-compose up and visit http://localhost:4001/api/v1/parties I get the following error: ERROR 42P01 (undefined_table) relation "parties" does not exist
Obviously my table does not exists. However I do run my migrations in my Dockerfile:
# ./Dockerfile
# Elixir base image to start with
FROM elixir:1.8
# 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/phoenix_new.ez --force
# install NodeJS and NPM
RUN curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
RUN bash nodesource_setup.sh
RUN apt-get install nodejs
RUN apt-get install -y inotify-tools
# create our app folder and copy our code in it and set it as default
RUN mkdir /app
COPY . /app
WORKDIR /app
# install dependencies
RUN mix deps.get
RUN mix deps.compile
# We can't separate the command as each command create a temporary container
# and each container as it own temporary variables
# run migrations
CMD mix ecto.migrate
# run phoenix server. The CMD is run each time the container is launched.
CMD mix phx.server
and here is my docker-compose.yml :
# the version of docker compose we use
version: '2'
services:
# the first container will be called postgres
postgres:
# the image is the last official postgres image
image: postgres
# the volumes allow us to have a shared space between our computer and the docker vm
volumes:
- "./.data/postgres:/var/lib/postgresql"
# set up environment variable for the postgres instance
environment:
POSTGRES_USER: ${PSQL_USER}
POSTGRES_PASSWORD: ${PSQL_PWD}
POOL: 100
# the port to listen
ports:
- "5432:5432"
# the second container is called redis
redis:
# the image is the last official redis image of the version 5
image: redis:5
ports:
- "6379:6379"
volumes:
- ./.data/redis:/var/lib/redis
# set up environment variable for the redis instance
environment:
REDIS_PASSWORD: ${REDIS_PWD}
# our last container is called elixir
elixir:
# build use a path to a Dockerfile
build: .
# we set multiple ports as each of our application (but database) will use a different port
ports:
- "4001:4001"
- "4101:4101"
# we share the entire app with the container, but the libs
volumes:
- ".:/app"
- "/app/deps"
- "/app/apps/admin/assets/node_modules"
# the container will not start if the postgres container isn't running
depends_on:
- postgres
# set up environment variable for the phoenix instance
environment:
POSTGRES_USER: ${PSQL_USER}
POSTGRES_PASSWORD: ${PSQL_PWD}
POSTGRES_DB_TEST: ${PSQL_DB_TEST}
POSTGRES_DB_DEV: ${PSQL_DB}
POSTGRES_HOST: ${PSQL_HOST}
What is wrong with my code ?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
When you build the container, the database does not run. The build of your container should fail.
You need to move creating and migrating the database to your entrypoint.
JulienCorb
Hi @NobbZ, thank you very much for your help !
Indeed I was silencing the postgres’ container logs, I just realised I get
[error] Postgrex.Protocol (#PID<0.279.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (172.18.0.2:5432): connection refused - :econnrefusedI manually created my DB though by hand :
docker-compose exec postgres psql user userCREATE DATABASE test_dev; CREATE DATABASE test_test;So I guess all I need is to run the migrations ?
Also I do not understand exactly what you mean by
Does it mean I have to move something in my app’s
endpoint.ex?NobbZ
Entrypoint in this context, is what docker runs as
ENTRYPOINT. You are usingCMDthough.I never really got the difference of these 2, but started to use
ENTRYPOINTonly, about 2 years back, and never had any issues because of this.Basically, you need to run a script which as
CMDorENTRYPOINTthat creates your DB if it did not exist yet, then runs your migrations and only then starts the application.Usually a shell script like this is used:
Put this as
entrypoint.shin your project, andchmod +xit. Make sure its in your container and then useENTRYPOINT entrypoint.shinstead ofCMD.NobbZ
I just realise, you are using two
CMD, this is not allowed, only the lastCMDspecified will be used, it overwrites any previous setCMD.On my first read I saw this as a
RUN.JulienCorb
Thank you very much !!
I think I’m almost there
I created
entrypoint.shat the root of my project, then ranchmod +x /Users/juliencorbin/political_project/entrypoint.shbut got
elixir_1 | /bin/sh: 1: entrypoint.sh: not founddid I run the chmod command wrong ?
NobbZ
Try using
/app/entrypoint.shjust to be sure.JulienCorb
Ok it worked !
but it couldn’t migrate the DB
here are the logs :
maybe I should remove the DB I created by hand ?
NobbZ
No, use the hostname
postgresto connect to the database rather than an IP that you’ll never know if it will be correct after the next restart of the database.Never rely on IPs inside of your
composed network, only ever use the service names, docker provides a DNS under the hood that will let them resolve properly.JulienCorb
oh @NobbZ you are my man, thank you so much ! You just turned my bad day into a dream day
Now I’m on my way to build a kickass umbrella app !