-
Notifications
You must be signed in to change notification settings - Fork 26
/
Dockerfile
81 lines (67 loc) · 2.83 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
FROM nebo15/alpine-elixir:1.6.4 as builder
MAINTAINER Nebo#15 [email protected]
# Always build with production environment
ENV MIX_ENV=prod
# `/opt` is a common place for third-party provided packages that are not part of the OS itself
WORKDIR /opt/app
# Required in elixir_make
RUN apk add --update --no-cache make
# Install and compile project dependencies
# We do this before all other files to make container build faster
# when configuration and dependencies are not changed
COPY mix.* ./
COPY config ./config
RUN mix deps.get --only prod
RUN mix deps.compile
# Copy rest of project files and build an OTP application
COPY . .
RUN mix compile
RUN mix release --verbose
# Release is packaged in a tarball that contains everything the application
# needs to run. We remove all other build artifacts and unarchived tarball
# to a well-known folder
RUN set -xe; \
RELEASE_TARBALL_PATH=$(find _build/${MIX_ENV}/rel/*/releases -maxdepth 2 -name *.tar.gz) && \
RELEASE_TARBALL_FILENAME="${RELEASE_TARBALL_PATH##*/}" && \
RELEASE_APPLICATION_NAME="${RELEASE_TARBALL_FILENAME%%.*}" && \
cp ${RELEASE_TARBALL_PATH} /opt/${RELEASE_TARBALL_FILENAME} && \
cd /opt && \
rm -rf /opt/app/ && \
mkdir -p /opt/${RELEASE_APPLICATION_NAME} && \
tar -xzf ${RELEASE_TARBALL_FILENAME} -C ${RELEASE_APPLICATION_NAME} && \
rm ${RELEASE_TARBALL_FILENAME}
# Build a container for runtime
# We are using Linux Alpine image with pre-installed Erlang,
# pure alpine with ERTS from tarball won't work because Erlang VM
# has lots of native dependencies
FROM nebo15/alpine-erlang:20.2.2
MAINTAINER Nebo#15 [email protected]
ENV \
# Application name
APPLICATION_NAME=annon_api \
# Common that we want to expose from a container,
# make sure that you change this variables after updating
# them in config.exs
GATEWAY_PUBLIC_PORT=4000 \
GATEWAY_PUBLIC_HTTPS_PORT=4000 \
GATEWAY_MANAGEMENT_PORT=4001 \
GATEWAY_PRIVATE_PORT=4443 \
# Replace ${VAR_NAME} in sys.config (generated with your application configuration)
# at the start time with actual environment variables values
REPLACE_OS_VARS=true
# Bash is required by Distillery
RUN apk add --update --no-cache bash
# Copy OTP release from a builder stage
COPY --from=builder /opt/${APPLICATION_NAME} /opt/${APPLICATION_NAME}
# Fix file permissions
RUN set xe; \
chmod -R 777 /opt/${APPLICATION_NAME}
WORKDIR /opt/${APPLICATION_NAME}
# Change user to "default" to limit runtime privileges
USER default
# Exposes this port from the docker container to the host machine
EXPOSE ${GATEWAY_PUBLIC_PORT} ${GATEWAY_PUBLIC_HTTPS_PORT} ${GATEWAY_MANAGEMENT_PORT} ${GATEWAY_PRIVATE_PORT}
# The command to run when this image starts up
# We start application in foreground mode to keep
# the container running and to redirect logs to the `STDOUT`
CMD bin/${APPLICATION_NAME} foreground