Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Multi Stage Dockerfile and docker-compose Files #66

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml

# Ignore bundler config.
/.bundle

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore uploaded files in development.
/storage/*
!/storage/.keep

/public/assets
.byebug_history

# Ignore master key for decrypting credentials and more.
/config/master.key

/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity

README.md

**/.DS_Store

# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
flix-*.tar

# If NPM crashes, it generates a log, let's ignore it too.
npm-debug.log

# The directory NPM downloads your dependencies sources to.
/assets/node_modules/

# Since we are building assets from assets/,
# we ignore priv/static. You may want to comment
# this depending on your deployment strategy.
/priv/static/
/uploads

# Ignore VS Code application files.
/.metals/
/.vscode/
183 changes: 183 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
##
## Base
##

FROM elixir:1.12.3-alpine as base

# labels from https://github.com/opencontainers/image-spec/blob/master/annotations.md
LABEL [email protected]
LABEL org.opencontainers.image.created=$CREATED_DATE
LABEL org.opencontainers.image.revision=$SOURCE_COMMIT
LABEL org.opencontainers.image.title="Zero To GraphQL Using Phoenix"
LABEL org.opencontainers.image.url=https://hub.docker.com/u/conradwt/zero-phoenix
LABEL org.opencontainers.image.source=https://github.com/conradwt/zero-to-graphql-using-phoenix
LABEL org.opencontainers.image.licenses=MIT
LABEL com.conradtaylor.elixir_version=$ELIXIR_VERSION

# set this with shell variables at build-time.
# If they aren't set, then not-set will be default.
ARG CREATED_DATE=not-set
ARG SOURCE_COMMIT=not-set

# environment variables
ENV USER=darnoc
ENV APP_PATH /home/${USER}/app
ENV GID=1000
ENV HEX_HOME ${APP_PATH}/.hex
ENV MIX_HOME ${APP_PATH}/.mix
ENV PORT 4000
ENV TMP_PATH /tmp/
ENV UID=1000

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

# copy entrypoint scripts and grant execution permissions
# COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh
# COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh
# RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh

#
# https://pkgs.alpinelinux.org/packages?name=&branch=v3.14
#

# install build and runtime dependencies
RUN apk -U add --no-cache \
build-base=0.5-r2 \
bzip2=1.0.8-r1 \
ca-certificates=20191127-r5 \
curl=7.79.1-r0 \
fontconfig=2.13.1-r4 \
git=2.32.0-r0 \
inotify-tools=3.20.11.0-r0 \
npm=7.17.0-r0 \
postgresql-dev=13.4-r0 \
python3=3.9.5-r1 \
tini=0.19.0-r0 \
tzdata=2021a-r0 && \
rm -rf /var/cache/apk/* && \
mkdir -p $APP_PATH

ENV MIX_ENV=prod

EXPOSE ${PORT}
ENV PORT ${PORT}

WORKDIR ${APP_PATH}

# create the MIX_HOME directory
RUN mkdir -p ${MIX_HOME}

# create the HEX_HOME directory
RUN mkdir -p ${HEX_HOME}

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

# 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

RUN chown ${USER}:${USER} ${APP_PATH}

ENTRYPOINT ["/sbin/tini", "--"]

##
## Development
##

FROM base as dev

ENV MIX_ENV=dev

# install mix dependencies
RUN mix do deps.get, deps.compile

# build assets
RUN npm --prefix ./assets install

RUN npm --prefix ./assets rebuild node-sass

COPY test test

COPY dev.entrypoint.sh dev.entrypoint.sh

RUN chmod +x dev.entrypoint.sh

CMD ["mix", "phx.server"]

##
## Test
##

FROM dev as test

COPY . .

RUN mix test

##
## Production
##

FROM alpine:3.14.2 as prod

#
# https://pkgs.alpinelinux.org/packages?name=&branch=v3.14
#

RUN apk add --no-cache \
openssl=1.1.1l-r0 \
ncurses-libs=6.2_p20210612-r0

WORKDIR /app

ENV USER=darnoc
ENV UID=1000
ENV GID=1000

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

USER ${USER}:${USER}

COPY --from=base --chown=${USER}:${USER} /home/${USER}/app/_build/prod/rel/flix ./

CMD ["bin/flix", "start"]
Loading