-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile
More file actions
344 lines (296 loc) · 11.8 KB
/
Dockerfile
File metadata and controls
344 lines (296 loc) · 11.8 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Have a common base for the entire Debian build branch.
FROM debian:13.4-slim AS base
ARG DEBIAN_FRONTEND=noninteractive
LABEL maintainer="Jonas Alfredsson <jonas.alfredsson@protonmail.com>"
# Have a common base for the entire Alpine build branch.
FROM alpine:3.23 AS base-alpine
LABEL maintainer="Jonas Alfredsson <jonas.alfredsson@protonmail.com>"
RUN apk add --no-cache \
ca-certificates
################################################################################
#
# Beginning of the "downloader" preparation build target.
#
################################################################################
# We start with a minimal base image we can reuse for the different build stages.
FROM base AS build-base
RUN apt-get update && \
apt-get install -y \
apt-transport-https \
&& \
apt-get install -y \
curl \
gnupg2 \
xz-utils
# The downloader stage will be used to fetch and unpack all the source code.
FROM build-base AS downloader
# Import the public keys that can be used for verifying the downloaded package.
# Keyblock can be found here: https://www.isc.org/pgpkey/
RUN --mount=type=bind,source=./isc-keyblock.asc,target=/isc-keyblock.asc \
install -m 0700 -o root -g root -d /root/.gnupg && \
gpg2 --import /isc-keyblock.asc && \
gpg2 --update-trustdb
# Continue working in this directory.
WORKDIR /downloads
# Download and unpack the correct tarball (also verify the signature).
ARG BIND_VERSION
RUN curl -LORf "https://downloads.isc.org/isc/bind9/${BIND_VERSION}/bind-${BIND_VERSION}.tar.xz{,.asc}" && \
gpg2 --no-options --verbose --keyid-format 0xlong --keyserver-options auto-key-retrieve=true \
--verify "./bind-${BIND_VERSION}.tar.xz.asc" "./bind-${BIND_VERSION}.tar.xz"
# Change to a new workdir to make sure we are in a clean workspace.
WORKDIR /source
# Extract the archive we downloaded to this clean workspace.
RUN tar -xvf /downloads/bind-${BIND_VERSION}.tar.xz --strip-components=1
# As a last step for this build target we copy the "meson setup" script which
# we use to keep the configuration in sync between both the Debian and Alpine
# build tracks.
COPY ./meson-setup.sh ./
################################################################################
#
# Beginning of the Debian build flow.
#
################################################################################
# Continue in a new stage to build what we have downloaded (Debian).
FROM build-base AS builder
RUN apt-get install -y \
# These initial packages are more related to extracting and compiling the code.
build-essential \
perl \
pkg-config \
protobuf-c-compiler \
netcat-openbsd \
# Below here are all the libraries needed by Bind for all features.
libssl-dev=3* \
liburcu-dev \
libuv1-dev \
libcap-dev \
libnghttp2-dev \
libxml2-dev \
zlib1g-dev \
liblmdb-dev \
libmaxminddb-dev \
libprotobuf-c-dev \
libidn2-dev \
libedit-dev \
libkrb5-dev \
libfstrm-dev \
libjson-c-dev \
libcmocka-dev \
libjemalloc-dev
# Needed in order to install Python packages via PIP after PEP 668 was
# introduced, but I believe this is safe since we are in a container without
# any real need to cater to other programs/environments.
ARG PIP_BREAK_SYSTEM_PACKAGES=1
# Install Python and then meson from pip to get up to date release.
RUN apt-get install -qq -y \
python3 \
&& \
# Install the latest version of PIP, Setuptools and Wheel.
curl -L 'https://bootstrap.pypa.io/get-pip.py' | python3 && \
# Install the necessary pip packages.
pip install meson ninja
# Use the coming "source" folder as our workdir.
WORKDIR /source
# Mount the "source" folder from the download stage and prepare for building.
RUN --mount=type=bind,from=downloader,source=/source,target=/source \
# Create an output directory we can write to.
mkdir /build && \
./meson-setup.sh "/build"
# We are now ready to compile the program.
RUN --mount=type=bind,from=downloader,source=/source,target=/source \
meson compile -C /build
# And finally we install it. HOWEVER, this is done just so all files are sent
# to their correct places, which is then logged by meson.
RUN --mount=type=bind,from=downloader,source=/source,target=/source \
meson install -C /build && \
# Sort the log file for simpler handling of directories later.
sort /build/meson-logs/install-log.txt -o /build/meson-logs/install-log.txt
# Finally we inject the "copier" script into this image, which will be used to
# perform the "meson install" step again, but without needing all the
# dependencies.
COPY copier.sh /
# This will be the final stage which will just contain the Bind binaries and its
# dependencies.
FROM base AS final
# The Debian packages create the "bind" (101/101) user on the system, and in
# order to be compatible with this we do the same.
# NOTE: Alpine uses "named" (100/101) instead.
ENV BIND_USER=bind
# We need to do some platfrom specific workarounds in the build script, so bring
# this information in to the build environment.
ARG TARGETPLATFORM
RUN apt-get update && \
# First we install some stuff needed during this initial configuration.
apt-get install -y \
apt-transport-https \
adduser \
&& \
# Create the group and user for our Bind process.
addgroup --system --gid 101 ${BIND_USER} && \
adduser --system --disabled-login --no-create-home --shell /bin/false --gecos "Bind user" \
--ingroup ${BIND_USER} --uid 101 ${BIND_USER} \
&& \
# Install all the runtime dependencies.
apt-get install -y \
openssl \
$(if [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then echo "liburcu8t64"; else echo "liburcu8"; fi) \
$(if [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then echo "libuv1t64"; else echo "libuv1"; fi) \
libcap2 \
libnghttp2-14 \
zlib1g \
liblmdb0 \
libmaxminddb0 \
libprotobuf-c1 \
libidn2-0 \
libedit2 \
libkrb5-3 \
libgssapi-krb5-2 \
libfstrm0 \
libjson-c5 \
libjemalloc2 \
&& \
# After this we create a couple of folders that should exist and be writable
# for the Bind process.
install -m 0770 -o "${BIND_USER}" -g "${BIND_USER}" -d "/var/cache/bind" && \
install -m 0775 -o "${BIND_USER}" -g "${BIND_USER}" -d "/var/log/bind" && \
install -m 0775 -o "${BIND_USER}" -g "${BIND_USER}" -d "/var/lib/bind" && \
install -m 0775 -o "root" -g "${BIND_USER}" -d "/run/named" && \
install -m 0775 -o "root" -g "${BIND_USER}" -d "/etc/bind/local-config" && \
mkdir /entrypoint.d \
&&\
# Perform some cleanup afterwards to keep size minimal.
apt-get purge -y \
apt-transport-https \
adduser \
&& \
apt-get -y autoremove && \
apt-get -y clean && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /tmp/*
# Now we have all the paths for all the files that were installed through
# meson, so we only want to copy these over to our otherwise clean image.
RUN --mount=type=bind,from=builder,source=/,target=/source \
/source/copier.sh "/source"
# Finally we copy all of our default configuration files, as well as our
# entrypoint, into the final container and update the settings to reflect this.
COPY ./root/ /
ENTRYPOINT [ "/entrypoint.sh" ]
CMD []
# Bind uses both TCP and UDP on port 53. Port 953 is used for rndc communication.
EXPOSE 53 53/udp 953
################################################################################
#
# Beginning of the Alpine build flow.
#
################################################################################
# Continue in a new stage to build what we have downloaded (Alpine).
FROM base-alpine AS builder-alpine
RUN set -e && apk add --no-cache \
# These initial packages are more related to extracting and compiling the code.
g++ \
make \
perl \
curl \
pkgconfig \
protobuf-c-compiler \
netcat-openbsd \
# Below here are all the libraries needed by Bind for all features.
openssl-dev \
userspace-rcu-dev \
libuv-dev \
libcap-dev \
nghttp2-dev \
libxml2-dev \
zlib-dev \
lmdb-dev \
libmaxminddb-dev \
protobuf-c-dev \
libidn2-dev \
libedit-dev \
krb5-dev \
fstrm-dev \
json-c-dev \
cmocka-dev \
jemalloc-dev
# Needed in order to install Python packages via PIP after PEP 668 was
# introduced, but I believe this is safe since we are in a container without
# any real need to cater to other programs/environments.
ARG PIP_BREAK_SYSTEM_PACKAGES=1
# Install Python and then meson from pip to get the same version as Debian.
RUN apk add --no-cache \
python3 \
&& \
# Install the latest version of PIP, Setuptools and Wheel.
curl -L 'https://bootstrap.pypa.io/get-pip.py' | python3 && \
# Install the necessary pip packages.
pip install meson ninja
# Use the coming "source" folder as our workdir.
WORKDIR /source
# Mount the "source" folder from the download stage and prepare for building.
RUN --mount=type=bind,from=downloader,source=/source,target=/source \
# Create an output directory we can write to.
mkdir /build && \
./meson-setup.sh "/build"
# We are now ready to compile the program.
RUN --mount=type=bind,from=downloader,source=/source,target=/source \
meson compile -C /build
# And finally we install it. HOWEVER, this is done just so all files are sent
# to their correct places, which is then logged by meson.
RUN --mount=type=bind,from=downloader,source=/source,target=/source \
meson install -C /build && \
# Sort the log file for simpler handling of directories later.
sort /build/meson-logs/install-log.txt -o /build/meson-logs/install-log.txt
# Finally we inject the "copier" script into this image, which will be used to
# perform the "meson install" step again, but without needing all the
# dependencies.
COPY copier.sh /
# This will be the final stage which will just contain the Bind binaries and its
# dependencies.
FROM base-alpine AS final-alpine
# The Alpine packages create the "named" (100/101) user on the system, and in
# order to be compatible with this we do the same.
# NOTE: Debian uses "bind" (101/101) instead.
ENV BIND_USER=named
RUN set -e && \
# Create the group and user for our Bind process.
addgroup -S -g 101 ${BIND_USER} && \
adduser -S -D -H -s /sbin/nologin -g "Bind user" \
-G ${BIND_USER} -u 100 ${BIND_USER} \
&& \
# Install all the runtime dependencies.
set -e && apk add --no-cache \
libssl3 \
userspace-rcu \
libuv \
libcap \
nghttp2 \
zlib \
lmdb \
libmaxminddb \
protobuf-c \
libidn2 \
libedit \
krb5 \
fstrm \
json-c \
jemalloc \
&& \
# After this we create a couple of folders that should exist and be writable
# for the Bind process.
install -m 0770 -o "${BIND_USER}" -g "${BIND_USER}" -d "/var/cache/bind" && \
install -m 0775 -o "${BIND_USER}" -g "${BIND_USER}" -d "/var/log/bind" && \
install -m 0775 -o "${BIND_USER}" -g "${BIND_USER}" -d "/var/lib/bind" && \
install -m 0775 -o "root" -g "${BIND_USER}" -d "/run/named" && \
install -m 0775 -o "root" -g "${BIND_USER}" -d "/etc/bind/local-config" && \
mkdir /entrypoint.d
# Now we have all the paths for all the files that were installed through
# meson, so we only want to copy these over to our otherwise clean image.
RUN --mount=type=bind,from=builder-alpine,source=/,target=/source \
/source/copier.sh "/source"
# Finally we copy all of our default configuration files, as well as our
# entrypoint, into the final container and update the settings to reflect this.
COPY ./root/ /
ENTRYPOINT [ "/entrypoint.sh" ]
CMD []
# Bind uses both TCP and UDP on port 53. Port 953 is used for rndc communication.
EXPOSE 53 53/udp 953