# Elixir+Phoenix - running a dev setup inside docker

**URL:** <https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269>\
**Category:** Questions\
**Tags:** docker, phoenix, docker-compose\
**Created:** [October 22, 2021, 9:09am UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269 "2021-10-22T09:09:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommica](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tommica/32/23212_2.png) [@tommica](https://forum.elixirforum.com/u/tommica)\
**Post date:** [October 22, 2021, 9:09am UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269/1 "2021-10-22T09:09:59Z")

</div>

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

```elixir
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

```elixir
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` )

```elixir
#!/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

```elixir
...
# Ignore docker related files.
/_pgdata

```

config/dev.exs

```elixir
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** ):

```elixir
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`

---

<div class="post-metadata">

**Author:** ![pdgonzalez872](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pdgonzalez872/32/28792_2.png) [@pdgonzalez872](https://forum.elixirforum.com/u/pdgonzalez872)\
**Post date:** [October 24, 2021, 6:55pm UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269/2 "2021-10-24T18:55:05Z")

</div>

Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![sukidhar](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sukidhar/32/24803_2.png) [@sukidhar](https://forum.elixirforum.com/u/sukidhar)\
**Post date:** [March 18, 2022, 6:11am UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269/3 "2022-03-18T06:11:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![veimis](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/veimis/32/23211_2.png) [@veimis](https://forum.elixirforum.com/u/veimis)\
**Post date:** [September 11, 2022, 6:46pm UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269/4 "2022-09-11T18:46:14Z")

</div>

Thanks for the config/dev.exs ip tip!

---

<div class="post-metadata">

**Author:** ![zulkris](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zulkris/32/28981_2.png) [@zulkris](https://forum.elixirforum.com/u/zulkris)\
**Post date:** [February 14, 2023, 1:59am UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269/5 "2023-02-14T01:59:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Exadra37](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/exadra37/32/9183_2.png) [@Exadra37](https://forum.elixirforum.com/u/Exadra37)\
**Post date:** [February 17, 2023, 11:12pm UTC](https://forum.elixirforum.com/t/elixir-phoenix-running-a-dev-setup-inside-docker/43269/6 "2023-02-17T23:12:18Z")

</div>

> [@tommica](#):
>
> ```elixir
> EXPOSE 4000
> 
> ```

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:

> [@Use Fly.io internal DNS for resolving Database\_URL](https://forum.elixirforum.com/t/use-fly-io-internal-dns-for-resolving-database-url/38889/8):
>
> # These two environment variables will be overwritten when the application is started. # They are needed here to satisfy the env-variable checks in `prod.secret.exs` ENV SECRET\_KEY\_BASE=nokey ENV DATABASE\_URL=nodb Instead you can delete prod.secret.exs, delete also everything from the prod.exs file but don’t delete it, and move everything inside such files to runtime.exs. ADD . . This may cause issues when the target you are building for is using different Phoenix/Elixir/Erlang versions from…
