tommica

tommica

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

Showing Posts 1 to 5

pdgonzalez872

pdgonzalez872

Thanks for sharing!

sukidhar

sukidhar

I was at a point blaming myself, why it is not working.. and thank you so much for sharing this and saving my time.

veimis

veimis

Thanks for the config/dev.exs ip tip!

zulkris

zulkris

two days i think whats wrong! exactly 0.0.0.0 !! Thanks

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

wintermeyer
A Fediverse server built with Elixir and Phoenix. A drop-in replacement for Mastodon. https://abuuba.com All 289 client API endpoints K...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews