forked from mirego/elixir-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
116 lines (86 loc) · 2.43 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Step 1 - hex dependencies
#
FROM hexpm/elixir:1.13.2-erlang-24.2.1-alpine-3.15.0 AS otp-dependencies
ENV MIX_ENV=prod
WORKDIR /build
# Install Alpine dependencies
RUN apk add --no-cache git
# Install Erlang dependencies
RUN mix local.rebar --force && \
mix local.hex --force
# Install hex dependencies
COPY mix.* ./
RUN mix deps.get --only prod
#
# Step 2 - npm dependencies + build the JS/CSS assets
#
FROM node:16.13-alpine3.14 AS js-builder
ENV NODE_ENV=prod
WORKDIR /build
# Install Alpine dependencies
RUN apk update --no-cache && \
apk upgrade --no-cache && \
apk add --no-cache git
# Copy hex dependencies
COPY --from=otp-dependencies /build/deps deps
# Install npm dependencies
COPY assets assets
RUN npm ci --prefix assets --no-audit --no-color --unsafe-perm --progress=false --loglevel=error
#
# Step 3 - build the OTP binary
#
FROM hexpm/elixir:1.13.2-erlang-24.2.1-alpine-3.15.0 AS otp-builder
ARG APP_NAME
ARG APP_VERSION
ENV APP_NAME=${APP_NAME} \
APP_VERSION=${APP_VERSION} \
MIX_ENV=prod
WORKDIR /build
# Install Alpine dependencies
RUN apk update --no-cache && \
apk upgrade --no-cache && \
apk add --no-cache git
# Install Erlang dependencies
RUN mix local.rebar --force && \
mix local.hex --force
# Copy hex dependencies
COPY mix.* ./
COPY --from=otp-dependencies /build/deps deps
RUN mix deps.compile
# Compile codebase
COPY config config
COPY lib lib
COPY priv priv
RUN mix compile
# Copy assets from step 1
COPY --from=js-builder /build/assets assets
RUN mix assets.deploy
# Build OTP release
COPY rel rel
RUN mix release
#
# Step 4 - build a lean runtime container
#
FROM alpine:3.15.0
ARG APP_NAME
ARG APP_VERSION
ENV APP_NAME=${APP_NAME} \
APP_VERSION=${APP_VERSION}
# Install Alpine dependencies
RUN apk update --no-cache && \
apk upgrade --no-cache && \
apk add --no-cache bash openssl libgcc libstdc++ ncurses-libs
WORKDIR /opt/elixir_boilerplate
# Copy the OTP binary from the build step
COPY --from=otp-builder /build/_build/prod/${APP_NAME}-${APP_VERSION}.tar.gz .
RUN tar -xvzf ${APP_NAME}-${APP_VERSION}.tar.gz && \
rm ${APP_NAME}-${APP_VERSION}.tar.gz
# Copy Docker entrypoint
COPY priv/scripts/docker-entrypoint.sh /usr/local/bin
RUN chmod a+x /usr/local/bin/docker-entrypoint.sh
# Create non-root user
RUN adduser -D elixir_boilerplate && \
chown -R elixir_boilerplate: /opt/elixir_boilerplate
USER elixir_boilerplate
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["start"]