darnahsan

darnahsan

Setting up a phoneix project in a container , works fine on the cli

Dockerfile

FROM hexpm/elixir:1.11.2-erlang-23.1.3-debian-buster-20201012 AS base

# install build dependencies
RUN apt-get -qq update && \
    apt-get -qq -y install build-essential npm git python --fix-missing --no-install-recommends


# prepare build dir
WORKDIR /app

ARG MIX_ENV
ARG RELEASE_LEVEL

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

# Update timezone
ENV TZ=Asia/Singapore

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

# set build ENV

ENV MIX_ENV=$MIX_ENV

# install mix dependencies
COPY mix.exs mix.lock ./
COPY config config
RUN mix deps.get --only $MIX_ENV
RUN mix 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


FROM debian:buster-slim AS app

RUN apk add --no-cache openssl ncurses-libs

WORKDIR /app

ARG MIX_ENV
ARG RELEASE_LEVEL

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

# Update timezone
ENV TZ=Asia/Singapore

# Exposes port to the host machine
EXPOSE 8080

# Install stable dependencies that don't change often
RUN apt-get update && \
  apt-get install -y --no-install-recommends \
  apt-utils \
  openssl \
  curl \
  wget && \
  rm -rf /var/lib/apt/lists/*


RUN chown nobody:nobody /app

USER nobody:nobody

COPY --from=build --chown=nobody:nobody /app/_build/${MIX_ENV}/rel/ghost_rider ./

ENV HOME=/app

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

fails with following error

Step 15/35 : RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
 ---> Running in 2b08ff3a7d3a
npm ERR! (intermediate value)(intermediate value)(intermediate value).then is not a function

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-02T02_23_33_826Z-debug.log
ERROR: Service 'ghost-rider' failed to build : The command '/bin/sh -c npm --prefix ./assets ci --progress=false --no-audit --loglevel=error' returned a non-zero code: 1

it works fine on the project via cli

npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
.....
......
........
added 948 packages in 12.791s

deps

[
      {:phoenix, "~> 1.5.7"},
      {:phoenix_ecto, "~> 4.1"},
      {:ecto_sql, "~> 3.4"},
      {:ecto_psql_extras, "~> 0.2"},
      {:postgrex, ">= 0.0.0"},
      {:phoenix_html, "~> 2.11"},
      {:phoenix_live_reload, "~> 1.2", only: :dev},
      {:phoenix_live_dashboard, "~> 0.3 or ~> 0.2.9"},
      {:telemetry_metrics, "~> 0.4"},
      {:telemetry_poller, "~> 0.4"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:finch, "~> 0.5.2"},
      {:recaptcha, "~> 3.0"},
      {:ink, "~> 1.0"},
      {:credo, "~> 1.5", only: [:dev, :test], runtime: false},
      {:sentry, "8.0.3"},
      {:hackney, "~> 1.8"}
    ]

Showing Posts 1 to 3

darnahsan

darnahsan OP

# https://forum.elixirforum.com/t/could-use-some-feedback-on-this-multistage-dockerfile-1st-elixir-phoenix-deployment/30862/10?

########################
### Dependency stage ###
########################
FROM hexpm/elixir:1.11.2-erlang-23.1.3-debian-buster-20201012 AS deps

# install build dependencies
RUN apt-get -qq update && \
    apt-get -qq -y install build-essential npm git python --fix-missing --no-install-recommends


# prepare build dir
WORKDIR /app

ARG MIX_ENV
ARG RELEASE_LEVEL

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

# Update timezone
ENV TZ=Asia/Singapore

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

# set build ENV
ENV MIX_ENV=${MIX_ENV}
ENV RELEASE_LEVEL=${RELEASE_LEVEL}

COPY mix.exs mix.lock ./
COPY config config

# install mix dependencies
RUN mix deps.get --only ${MIX_ENV}
RUN mix deps.compile



########################
# Build Phoenix assets #
########################
# Using stretch for now because it includes Python
# Otherwise you get errors, could use a smaller image though
FROM node:14.15.1-stretch AS assets
WORKDIR /app/assets

COPY --from=deps /app/deps /app/deps/
COPY assets/package.json assets/package-lock.json ./
RUN npm ci --progress=false --no-audit --loglevel=error

COPY priv ./
COPY assets/ ./

RUN npm run deploy

#########################
# Create Phoenix digest #
#########################
FROM deps AS digest
COPY --from=assets /app/priv ./priv
RUN mix phx.digest

#######################
#### Create release ###
#######################
FROM digest AS release
ARG MIX_ENV
ENV MIX_ENV=${MIX_ENV}
COPY lib ./lib
RUN mix do compile, release

#################################################
# Create the actual image that will be deployed #
#################################################
FROM debian:buster-slim AS deploy

# Install stable dependencies that don't change often
RUN apt-get update && \
  apt-get install -y --no-install-recommends \
  apt-utils \
  openssl \
  curl \
  wget && \
  rm -rf /var/lib/apt/lists/*


# Set WORKDIR after setting user to nobody so it automatically gets the right permissions
# When the app starts it will need to be able to create a tmp directory in /app
WORKDIR /app

ARG MIX_ENV
ARG RELEASE_LEVEL

ENV MIX_ENV=${MIX_ENV}
ENV RELEASE_LEVEL=${RELEASE_LEVEL}

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

# Update timezone
ENV TZ=Asia/Singapore

COPY --from=release /app/_build/${MIX_ENV}/rel/ghost_rider ./

ENV HOME=/app

# Exposes port to the host machine
EXPOSE 8080

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

got the build to work following

Could use some feedback on this multistage dockerfile (1st elixir/phoenix deployment) - #10 by otijhuis?

but seems that doesn’t take priv into account and have missing files for tailwind. any examples docker file that uses tailwind with phoenix ?

Aetherus

Aetherus

Haven’t tried your docker image so maybe I’m wrong.

I often find myself using too new nodejs and npm. Nodejs 10 is usually fine, but Nodejs 12 is often no good.

darnahsan

darnahsan OP

Thanks for sharing your input

got it to work finally with some changes :grin:

# https://forum.elixirforum.com/t/could-use-some-feedback-on-this-multistage-dockerfile-1st-elixir-phoenix-deployment/30862/10?

########################
### Dependency stage ###
########################
FROM hexpm/elixir:1.11.2-erlang-23.1.3-debian-buster-20201012 AS deps

# install build dependencies
RUN apt-get -qq update && \
    apt-get -qq -y install build-essential npm git python --fix-missing --no-install-recommends


# prepare build dir
WORKDIR /app

ARG MIX_ENV
ARG RELEASE_LEVEL

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

# Update timezone
ENV TZ=Asia/Singapore

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

# set build ENV
ENV MIX_ENV=${MIX_ENV}
ENV RELEASE_LEVEL=${RELEASE_LEVEL}

COPY mix.exs mix.lock ./
COPY config config

# install mix dependencies
RUN mix deps.get --only ${MIX_ENV}
RUN mix deps.compile



########################
# Build Phoenix assets #
########################
# Using stretch for now because it includes Python
# Otherwise you get errors, could use a smaller image though
FROM node:14.15.1-stretch AS assets
WORKDIR /app/assets

COPY --from=deps /app/deps /app/deps/
COPY assets/package.json assets/package-lock.json ./
RUN npm ci --progress=false --no-audit --loglevel=error

COPY assets/ ./

RUN npm run deploy

#########################
# Create Phoenix digest #
#########################
FROM deps AS digest
COPY priv priv
RUN mix phx.digest

#######################
#### Create release ###
#######################
FROM digest AS release
ARG MIX_ENV
ENV MIX_ENV=${MIX_ENV}
COPY lib ./lib
RUN mix do compile, release

#################################################
# Create the actual image that will be deployed #
#################################################
FROM debian:buster-slim AS deploy

# Install stable dependencies that don't change often
RUN apt-get update && \
  apt-get install -y --no-install-recommends \
  apt-utils \
  openssl \
  curl \
  wget && \
  rm -rf /var/lib/apt/lists/*


# Set WORKDIR after setting user to nobody so it automatically gets the right permissions
# When the app starts it will need to be able to create a tmp directory in /app
WORKDIR /app

ARG MIX_ENV
ARG RELEASE_LEVEL

ENV MIX_ENV=${MIX_ENV}
ENV RELEASE_LEVEL=${RELEASE_LEVEL}

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

# Update timezone
ENV TZ=Asia/Singapore

COPY --from=release /app/_build/${MIX_ENV}/rel/ghost_rider ./

ENV HOME=/app

# Exposes port to the host machine
EXPOSE 8080

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

I will try to submit to the official hex docs for deployment as the recipe there is incomplete in my view and not so convenient for 1st time users of phoenix

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews