forked from erlang/docker-erlang-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.very_small_image
176 lines (134 loc) · 5.65 KB
/
Dockerfile.very_small_image
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# %CopyrightBegin%
#
# Copyright Ericsson AB 2019. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# %CopyrightEnd%
# Sources:
# * Dockerfile created by Rickard Green (rickard.s.green@ericsson.com)
# https://gist.github.com/rickard-green/8930e6643800440fde470f9e2e1ff2be),
# * The other Dockerfile in this directory created by Lukas Larsson
# (lukas.larsson@erlang-solutions.com)
# * Makefile by Matthias Lang (matthias@corelatus.se)
# http://erlang.org/pipermail/erlang-questions/2016-February/087935.html
#
# Created by Kjell Winblad (kjell.a.winblad@ericsson.com).
# Use the following command to build an image using this docker file:
#
# docker build --file Dockerfile.very_small_image -t erlang-dockerwatch .
# Build stage 0
FROM alpine:latest as build
##############################################
# Build Erlang/OTP
##############################################
ENV FETCH_DEPS="git ca-certificates"
ENV BUILD_DEPS="dpkg-dev dpkg gcc g++ libc-dev linux-headers make autoconf tar zip openssl-dev"
ENV CONFIGURE_FLAGS="--without-termcap --disable-hipe --without-javac"
ENV CFLAGS="-O2 -g"
# Fetch dependencies...
RUN set -xe \
&& apk add --no-cache --virtual .fetch-deps $FETCH_DEPS \
&& apk add --no-cache --virtual .build-deps $BUILD_DEPS
ENV WDIR /usr/local/src
WORKDIR $WDIR
# Determine which branch to build
ARG OTP
ENV BRANCH "maint-${OTP:-21}"
ENV OTP_GIT_URL="https://github.com/erlang/otp.git"
ENV ERL_TOP="/usr/local/src/erlang-otp-source-$BRANCH"
ENV DESTDIR="/usr/local/src/erlang-otp-install"
ENV DEST_OTP_ROOT="$DESTDIR/usr/local/lib/erlang"
# Fetch, build, and install OTP
RUN set -xe \
&& git clone --branch $BRANCH --depth 1 $OTP_GIT_URL $ERL_TOP \
&& cd $ERL_TOP \
&& ./otp_build autoconf \
&& ./configure $CONFIGURE_FLAGS --build="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
&& make -j$(getconf _NPROCESSORS_ONLN) \
&& make install DESTDIR=$DESTDIR \
&& rm -rf $ERL_TOP
# Cleanup OTP installation
RUN set -xe \
&& find $DESTDIR -regex "$DEST_OTP_ROOT/\(lib/\|erts-\).*/\(man\|doc\|obj\|c_src\|emacs\|info\|examples\)" | xargs rm -rf \
&& find $DESTDIR -name src | xargs -r find | grep -v '\.hrl$' | xargs rm -v || true \
&& find $DESTDIR -name src | xargs -r find | xargs rmdir -vp || true
# Remove unnecessary files
RUN set -xe \
&& rm -rf $DEST_OTP_ROOT/Install \
&& rm -rf $DEST_OTP_ROOT/releases \
&& rm -rf $DEST_OTP_ROOT/bin/start_clean.boot \
&& rm -rf $DEST_OTP_ROOT/erts*/lib/
RUN cp -r /usr/local/src/erlang-otp-install/* /
##############################################
# Create release of application
##############################################
RUN set -xe && apk add --no-cache --virtual otp-runtime-deps $(cat /usr/local/lib/erlang/otp-runtime-deps)
# Install Rebar3
RUN mkdir -p /buildroot/rebar3/bin
ADD https://s3.amazonaws.com/rebar3/rebar3 /buildroot/rebar3/bin/rebar3
RUN chmod a+x /buildroot/rebar3/bin/rebar3
# Setup Environment
ENV PATH=/buildroot/rebar3/bin:$PATH
RUN apk add --no-cache zip binutils
# Reset working directory
WORKDIR /buildroot
# Copy our Erlang test application
COPY dockerwatch dockerwatch
# And build the release
WORKDIR dockerwatch
RUN rebar3 as prod release
# Strip binaries
RUN set -xe \
&& scanelf --nobanner -E ET_EXEC -BF '%F' --recursive /buildroot | xargs -r strip --strip-all \
&& scanelf --nobanner -E ET_DYN -BF '%F' --recursive /buildroot | xargs -r strip --strip-unneeded
ENV RELDIR="/buildroot/dockerwatch/_build/prod/rel/dockerwatch"
# Strip release
RUN set -xe \
&& erl -noshell -noinput -eval "beam_lib:strip_release(\"$RELDIR\"),init:stop()" \
&& rm -rf /usr/local/lib/erlang
#Determine and save runtime dependencies for each application
RUN set -xe \
&& touch /otp-runtime-deps \
&& for app in $RELDIR/erts-[0-9.]* $RELDIR/lib/[a-zA-Z_]*-[0-9.]*; do \
scanelf --needed --nobanner --format '%n#p' --recursive $app \
| tr ',' '\n' \
| sort -u \
| sed 's/^[ \t\r]*\([^ \t\r\n][^ \t\r\n]*\)[ \t\r]*$/so:\1/g' \
> $app/otp-app-runtime-deps \
&& case $app in \
*/erts-[0-9]*) echo "lksctp-tools" >> $app/otp-app-runtime-deps;; \
*/wx-[0-9]*) echo "ttf-freefont" >> $app/otp-app-runtime-deps;; \
*/jinterface-[0-9]*) echo openjdk8-jre >> $app/otp-app-runtime-deps;; \
*) ;; \
esac \
&& cat $app/otp-app-runtime-deps >> /otp-runtime-deps || exit 1; \
done \
&& cat /otp-runtime-deps | sort -u > /otp-runtime-deps-sorted
# Compress libraries
# (asn and crypto contains nif libraries. The runtime system
# cannot load these libraries when they are in .ez files)
RUN set -xe \
&& cd $RELDIR/lib/ \
&& ls . | grep -v asn | grep -v crypto | xargs -n1 sh -c 'zip -mr $1.ez $1' -
# Build stage 1
FROM alpine:latest as install
COPY --from=build /otp-runtime-deps-sorted /otp-runtime-deps
# Install dependencies
RUN set -xe && apk add --no-cache --virtual otp-runtime-deps $(cat /otp-runtime-deps)
# Install the released application
COPY --from=build /buildroot/dockerwatch/_build/prod/rel/dockerwatch /dockerwatch
# Expose relevant ports
EXPOSE 8080
EXPOSE 8443
CMD ["/dockerwatch/bin/dockerwatch", "foreground"]