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 tried to do with this command:
docker run --env-file .env -p 8080:4000 romenigld/blog-prod
I`m getting this error:
Error loading shared library libstdc++.so.6: No such file or directory (needed by /app/erts-12.0.2/bin/beam.smp)
Error loading shared library libgcc_s.so.1: No such file or directory (needed by /app/erts-12.0.2/bin/beam.smp)
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_begin_catch: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt24__throw_out_of_range_fmtPKcz: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _Znwm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt20__throw_length_errorPKc: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_guard_release: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __popcountdi2: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt29_Rb_tree_insert_and_rebalancebPSt18_Rb_tree_node_baseS0_RS_: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt17__throw_bad_allocv: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_appendEPKcm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt18_Rb_tree_incrementPKSt18_Rb_tree_node_base: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_end_catch: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_guard_acquire: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEmmm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt19__throw_logic_errorPKc: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZSt18_Rb_tree_decrementPSt18_Rb_tree_node_base: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7reserveEm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_rethrow: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _Unwind_Resume: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZdlPvm: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv120__si_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __cxa_pure_virtual: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv117__class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv117__class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv117__class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv117__class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv117__class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv117__class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: _ZTVN10__cxxabiv121__vmi_class_type_infoE: symbol not found
Error relocating /app/erts-12.0.2/bin/beam.smp: __gxx_personality_v0: symbol not found
My Dockerfile:
FROM elixir:alpine AS build
# install build dependencies
RUN apk add --no-cache build-base npm git python3
# 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
RUN mix do compile, release
# prepare release image
FROM alpine:3.9 AS app
RUN apk add --no-cache openssl ncurses-libs
WORKDIR /app
RUN chown nobody:nobody /app
USER nobody:nobody
COPY --from=build --chown=nobody:nobody /app/_build/prod/rel/blog ./
ENV HOME=/app
CMD ["bin/blog", "start"]
my docker_dev_start.sh:
mix ecto.drop
mix ecto.setup
exec mix phx.server
my docker-compose.yml:
version: "3"
services:
app:
restart: on-failure # vai restartar na falha(outras opções [no, always, unless-stopped(quando força a ação)])
build:
context: .
dockerfile: Dockerfile.dev
command: /bin/sh docker_dev_start.sh
ports:
- "8080:4000"
depends_on:
- db
links:
- db
db:
restart: always
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- "5432:5432"
It looks like a missing runtime dependency in your final image. Try changing RUN apk add --no-cache openssl ncurses-libs to RUN apk add --no-cache openssl ncurses-libs libstdc++.
The beam has native runtime dependencies and OTP 24 added libc as runtime dependency to support the JIT. With that change it seems like bare alpine:3.9 no longer brings all the required runtime dependencies. You’ll need to make sure that all of those those are present in the app container.
This is awesome! @romenigld, thank you for sending the newer version, I was looking for something like in order to deploy my api to Heroku and this did it! I’m currently using your initial Dockerfile, I’ll try this one instead.
Note this appears to be fixed in the upcoming docs of Phoenix 1.6.0.
The beam has native runtime dependencies and OTP 24 added libc as runtime dependency to support the JIT.
With Alpine’ main selling point (if you will) being its small size, I’ve been wondering by how much the size of the runtime image would increase. Turns out: Not much. libstdc++ has 1.6MB and (its dependency) libgcc not more than 200KB.
However, I wonder if it still makes sense to use Alpine if you need to add (gnu)libc anyways? (I haven’t yet compared the image size with the Debian based -slim variant.) And even if it still is considerably smaller (and or has other benefits), should it be more or less advertised as default approach in the docs, now that we have the -slim variant?
a good tip is to keep the the underlying os image versions in sync (build and release section), specifying a concrete version of the build image is also helpful, then look at Dockerfile of the build image and use the underlying os image version in your release image, then it would be unlikely to run into such issues
my reason to use Alpine is not (mainly) size, but keep CVS reported security risks at minimum. Usually, ubuntu, even slim, as it packs more deps, it raises more security holes, which demands more maintenance.
User nobody is not recommended to use to run any app inside or outside a docker container.
Check this issue I open:
The fixed Dockerfile on the issue:
FROM hexpm/elixir:1.11.2-erlang-23.1.2-alpine-3.12.1 as build
# install build dependencies
RUN apk add --no-cache build-base npm git python3
# prepare build dir
WORKDIR /app
# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force
# set build ENV
ENV MIX_ENV=dev
# 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/runtime.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 add --no-cache openssl ncurses-libs libstdc++
ENV USER="phoenix"
ENV HOME=/home/"${USER}"
ENV APP_DIR="${HOME}/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}" \
-D "${USER}" && \
su "${USER}" sh -c "mkdir ${APP_DIR}"
# Everything from this line onwards will run in the context of the unprivileged user.
USER "${USER}"
WORKDIR "${APP_DIR}"
COPY --from=build --chown="${USER}":"${USER}" /app/_build/prod/rel/my_app ./
ENTRYPOINT ["bin/my_app"]
# Usage:
# * build: sudo docker build -t phoenix/my_app .
# * shell: sudo docker run --rm -it --entrypoint "" -p 80:4000 -p 443:4040 phoenix/my_app sh
# * run: sudo docker run --rm -it -p 80:4000 -p 443:4040 --name my_app phoenix/my_app
# * exec: sudo docker exec -it my_app sh
# * logs: sudo docker logs --follow --tail 100 my_app
#
# Extract the production release to your host machine with:
#
# ```
# mkdir archive
# sudo docker 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"]
I’ve followed the update Debian based guide, but am still getting errors on OTP 24 relating to libstdc++6.so.
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE} as cms
ARG SHORT_SHA
# Assign version number for AppSignal
ENV APP_REVISION=$SHORT_SHA
RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
WORKDIR "/app"
RUN chown nobody /app
# set runner ENV
ENV MIX_ENV="prod"
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/cms ./
USER nobody
ENV HOME=/app
CMD ["bin/cms", "start"]