fly49

fly49

Can't run Phoenix using docker-compose

Hi, I am trying to get Phoenix app work using containers and elixir releases.

I succeeded to run my app in a single container and connect it to local postgres using dockerfile and command below:

Dockerfile:

FROM elixir:1.9.0-alpine AS build

# install build dependencies
RUN apk add --no-cache build-base npm git python

# prepare build dir
WORKDIR /app

# install hex + rebar
RUN mix local.hex --force && \
    mix local.rebar --force

# set build ENV
ENV MIX_ENV=prod

# install mix dependencies
COPY mix.exs mix.lock ./
COPY config config
RUN mix do deps.get, deps.compile

# build assets
COPY assets/package.json assets/package-lock.json ./assets/
RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error

COPY priv priv
COPY assets assets
RUN npm run --prefix ./assets deploy
RUN mix phx.digest

# compile and build release
COPY lib lib
# uncomment COPY if rel/ exists
# COPY rel rel
RUN mix do compile, release

# prepare release image
FROM alpine:3.9 AS app
RUN apk add --no-cache openssl ncurses-libs

EXPOSE 4000
ENV PORT=4000 \
    MIX_ENV=prod \
    SHELL=/bin/bash

WORKDIR /app

RUN chown nobody:nobody /app

USER nobody:nobody

COPY --from=build --chown=nobody:nobody /app/_build/prod/rel/myapp ./

ENV HOME=/app

CMD ["bin/myapp", "start"]

Commands:

docker build -t test .
docker run \
  --network=host \
  -e DATABASE_URL=postgresql://postgres:postgres@localhost/myapp_dev \
  -e SECRET_KEY_BASE=...some key... \
  -e HOST=localhost \
  test    

But when I am trying to up two containers via docker-compose.yml

version: '3'

services:
  PostgreSQL:
    image: postgres
    container_name: postgres
    ports:
      - 5433:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
    hostname: postgres
    restart: always
    user: root
  elixir:
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
      - .env.list
    ports:
      - 4040:4000
    container_name: elixir
    volumes:
      - .:/app
    depends_on:
      - PostgreSQL

.env.list:

DATABASE_URL=postgresql://postgres:postgres@postgres/myapp_prod
SECRET_KEY_BASE=... some key...
HOST=localhost

I am getting an error:

Creating postgres ... done
Creating elixir   ... error

ERROR: for elixir  Cannot start service elixir: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "bin/myapp": stat bin/myapp: no such file or directory: unknown
ERROR: Encountered errors while bringing up the project.

I suppose that docker-compose should just run elixir container with according dockerfile as just it happens with “docker run …”, but it seems that I get different container which doesn’t have bin/ directory for some reason. Am I missing something?

Marked As Solved

pmangalakader

pmangalakader

it seems, volume mount for elixir container in the docker-compose overwrites the docker build container files. Try removing the mount and see if that solves the issue.

volumes:
      - .:/app
# remove this

Also Liked

Exadra37

Exadra37

Never use the root user in any docker image, just like you would not use it in a real server.

The database is open to the internet. You don’t need to expose the ports for the elixir service to be able to reach it.

This is not necessary, instead just change the name of the docker-compose service from PostgreSQL to postgres.

If is for running in localhost then you need to do it like 127.0.0.1:4040:4000, otherwise it may be reachable from the internet, unless you have other security measures in place.

The use of 4040:4000 is equivalent to 0.0.0.0:4040:4000, and 0.0.0.0 means it will be listening in all configured networks on your computer.

It’s unsafe to use the nobody user as I report on this issue and now being merged to the official docs.

pmangalakader

pmangalakader

@fly49 try this, the chown worked for me:

# Creates an unprivileged user to be used exclusively to run the Phoenix app
RUN addgroup -g 1000 -S "${USER}" && \
    adduser -s /bin/sh -u 1000 -G "${USER}" -h /home/"${USER}" -D "${USER}" && \
    su "${USER}"

RUN chown -R "${USER}:${USER}" "/home/${USER}"
# Everything from this line onwards will run in the context of the unprivileged user.
USER "${USER}"

the directory /home/user, the user directory was owned by root and hence, the error occurs.

fly49

fly49

@pmangalakader
Thanks you! It solves the issue!

@Exadra37
Thank you for detailed response, a lot of useful info for me as for docker beginner.
Also I took a look at the PR you mentioned and tried to build a fresh image with new dockerfile, but it has this line:

# changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/

It seems that “config/runtime.exs” file is not a default thing, at least there is no mentions in docs to create it, and as well I don’t have one.

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement