From f46218277f7a932eaaa625125dcc7d2f55250200 Mon Sep 17 00:00:00 2001 From: Okke Tijhuis Date: Sun, 26 Apr 2020 09:19:47 +0200 Subject: [PATCH] improve docker example (#3796) - add python (needed for nodegyp rebuild, will display error otherwise) - mkdir isn't needed, WORKDIR will create the directory if it doesn't exist - combine commands where possible to reduce the amount of docker layers - copy package*.json and install packages before doing the deploy so the install won't happen every time you modify a js/css file or other asset - bash not needed, sh/ash is available - install ncurses-libs, the app wouldn't start otherwise - do the chown when /app is still empty and add chown to the copy command, this saves docker from creating a large layer (about 10Mb) just for the permission changes - add a default cmd - use apk --no-cache so the cache doesn't get added to the image --- guides/deployment/releases.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/guides/deployment/releases.md b/guides/deployment/releases.md index f76fc8bfd3..dfaef3bf8b 100644 --- a/guides/deployment/releases.md +++ b/guides/deployment/releases.md @@ -175,10 +175,9 @@ Here is an example Docker file to run at the root of your application covering a FROM elixir:1.9.0-alpine AS build # install build dependencies -RUN apk add --update build-base npm git +RUN apk add --no-cache build-base npm git python # prepare build dir -RUN mkdir /app WORKDIR /app # install hex + rebar @@ -191,35 +190,38 @@ ENV MIX_ENV=prod # install mix dependencies COPY mix.exs mix.lock ./ COPY config config -RUN mix deps.get -RUN mix deps.compile +RUN mix do deps.get, deps.compile # build assets -COPY assets assets +COPY assets/package.json assets/package-lock.json ./assets/ +RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error + COPY priv priv -RUN cd assets && npm install && npm run deploy +COPY assets assets +RUN npm run --prefix ./assets deploy RUN mix phx.digest -# build project +# compile and build release COPY lib lib -RUN mix compile - -# build release (uncomment COPY if rel/ exists) +# uncomment COPY if rel/ exists # COPY rel rel -RUN mix release +RUN mix do compile, release # prepare release image FROM alpine:3.9 AS app -RUN apk add --update bash openssl +RUN apk add --no-cache openssl ncurses-libs -RUN mkdir /app WORKDIR /app -COPY --from=build /app/_build/prod/rel/my_app ./ -RUN chown -R nobody: /app -USER nobody +RUN chown nobody:nobody /app + +USER nobody:nobody + +COPY --from=build --chown=nobody:nobody /app/_build/prod/rel/my_app ./ ENV HOME=/app + +CMD ["bin/my_app", "start"] ``` At the end, you will have an application in `/app` ready to run as `bin/my_app start`.