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.

fly49

fly49

Exact as here

FROM hexpm/elixir:1.11.2-erlang-23.1.2-alpine-3.12.1 as build

# install build dependencies
RUN apk upgrade --no-cache
RUN apk add --no-cache build-base npm git python3 curl

# prepare build dir
WORKDIR /app

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

# set build ENV
ARG MIX_ENV="prod"
ENV MIX_ENV="${MIX_ENV}"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config
# Dependencies sometimes use compile-time configuration. Copying
# these compile-time config files before we compile dependencies
# ensures that any relevant config changes will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/$MIX_ENV.exs config/
RUN mix deps.compile

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

COPY priv priv

# Note: if your project uses a tool like https://purgecss.com/,
# which customizes asset compilation based on what it finds in
# your Elixir templates, you will need to move the asset compilation step
# down so that `lib` is available.
COPY assets assets
# use webpack to compile npm dependencies - https://www.npmjs.com/package/webpack-deploy
RUN npm run --prefix ./assets deploy
RUN mix phx.digest

# compile and build the release
COPY lib lib
RUN mix compile
# changes to config/runtime.exs don't require recompiling the code
COPY config/releases.exs config/
# uncomment COPY if rel/ exists
# COPY rel rel
RUN mix release

# Start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM alpine:3.12.1 AS app
RUN apk upgrade --no-cache
RUN apk add --no-cache openssl ncurses-libs

ARG MIX_ENV="prod"
ENV USER="elixir"

WORKDIR "/home/${USER}/app"
# 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/elixir \
   -D "${USER}" \
  && su "${USER}"

# Everything from this line onwards will run in the context of the unprivileged user.
USER "${USER}"


COPY --from=build --chown="${USER}":"${USER}" /app/_build/"${MIX_ENV}"/rel/my_app ./

ENTRYPOINT ["bin/my_app"]

# Usage:
#  * build: sudo docker image build -t elixir/my_app .
#  * shell: sudo docker container run --rm -it --entrypoint "" -p 127.0.0.1:4000:4000 elixir/my_app sh
#  * run:   sudo docker container run --rm -it -p 127.0.0.1:4000:4000 --name my_app elixir/my_app
#  * exec:  sudo docker container exec -it my_app sh
#  * logs:  sudo docker container logs --follow --tail 100 my_app
#
# Extract the production release to your host machine with:
#
# ```
# mkdir archive
# sudo docker container run --rm -it --entrypoint "" -v "$PWD/archive:/home/phoenix/archive"  phoenix/my_app sh -c "tar zcf /home/phoenix/archive/app.tar.gz ."
# ls -al archive
# ````
CMD ["start"]
fly49

fly49

Thank you for the answer! I got suck a the creating and migrating database on containers initialization, tried the trick from here, but it doesn’t work for me… Could you recommend me something?

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement