tommica

tommica

Elixir+Phoenix - running a dev setup inside docker

I’m a complete noob when it comes to docker and docker compose, but I wanted to share my configuration to get a phoenix project up and running - I would really appreciate if someone could go through these and tell me what I am doing wrong (or missed, or something else) - I really want to learn to work with proper CI/CD tools

Dockerfile

FROM elixir:latest

EXPOSE 4000

RUN apt-get update && \
    apt-get install -y postgresql-client && \
    apt-get install -y inotify-tools && \
    apt-get install -y nodejs && \
    curl -L https://npmjs.org/install.sh | sh && \
    mix local.hex --force && \
    mix archive.install hex phx_new --force && \
    mix local.rebar --force

ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

docker-compose.yml

version: "3"

services:
  phoenix:
    build: .
    volumes:
      - .:/app
    ports:
      - "4000:4000"
    environment:
      PGUSER: postgres
      PGPASSWORD: postgres
      PGDATABASE: myapp_dev
      PGHOST: db
      PGPORT: 5432
    depends_on:
      - db
    command:
      - "./entrypoint.sh"
  db:
    image: postgres:13.4-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      PGDATA: /var/lib/postgresql/data/pgdata
    restart: always
    volumes:
      - ./_pgdata:/var/lib/postgresql/data

entrypoint.sh (remember to make it executable by running chmod +x entrypoint.sh )

#!/bin/bash

set -e

# Ensure the app's dependencies are installed
mix deps.get

if [[ -f assets/package.json ]]; then
  # Install the app's dependencies with npm
  cd assets
  npm install
  cd ..
fi

# Wait until Postgres is ready
while ! pg_isready -q -h $PGHOST -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
  mix ecto.create
  mix ecto.migrate
  mix run priv/repo/seeds.exs
  echo "Database $PGDATABASE created."
fi

mix phx.server

.gitignore

...
# Ignore docker related files.
/_pgdata

config/dev.exs

username: System.get_env("PGUSER", "postgres"),
password: System.get_env("PGPASSWORD", "postgres"),
database: System.get_env("PGDATABASE", "myapp_dev"),
hostname: System.get_env("PGHOST", "localhost"),
port: String.to_integer(System.get_env("PGPORT", "5432")),

And finally again in config/dev.exs change this too (this is what stumped me for a while, and when I realized the issue, I felt like an idiot - by default phoenix binds to localhost only, so it is only binded to the port 4000 inside the VM, so no matter how much you expose port 4000 and forward it, it will not be accessible outside of docker, so curl localhost:4000 would just return an empty response):

http: [ip: {127, 0, 0, 1}, port: 4000],
to
http: [ip: {0, 0, 0, 0}, port: 4000],

Then just run docker-compose build and docker-compose up

First Post!

pdgonzalez872

pdgonzalez872

Thanks for sharing!

Last Post!

Exadra37

Exadra37

Not required for many years. It was only in the begin of docker that was necessary to declare it.


Your App is running as root and that’s a bad security practice, even for development.

You should create a unprivileged user and use it instead. See my detailed reply to another topic where I show how to do it:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement