Skip to content

Commit 99ad1c3

Browse files
committed
Enable multi-architecture docker image builds
Previously, the Dockerfile downloaded 'docker-gen' and 'forego' binaries during build time. This caused a problem as it hard-coded the amd64 architecture for the images. This commit updates both 'Dockerfile' and 'Dockerfile.alpine' to build the `forego` and `docker-gen` executables from scratch instead of downloading binaries directly. This is achieved using multi-stage builds [1]. Two seperate stages first build the binaries, and are then copied over to the final stage. The advantage of this change is two-fold: First, it enables building this image on architectures other than amd64. Secondly it adds trust by not adding external binaries to the docker image. This modified version passes the test both a linux desktop (amd64) as well as a raspberry pi (armv7) with some caveats: - On armv7, a modified version of the `jwilder/docker-gen` image is required. See a seperate PR at [2]. - The 'test_dhparam_is_generated_if_missing' test fails. This also doesn't currently pass on master. [1] https://docs.docker.com/develop/develop-images/multistage-build/ [2] nginx-proxy/docker-gen#327
1 parent c8a6785 commit 99ad1c3

File tree

2 files changed

+109
-18
lines changed

2 files changed

+109
-18
lines changed

Dockerfile

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,50 @@
1-
FROM nginx:1.19.3
1+
# setup build arguments for version of dependencies to use
2+
ARG NGINX_VERSION=1.19.3
3+
ARG GO_VERSION=1.14
4+
5+
ARG DOCKER_GEN_VERSION=0.7.4
6+
ARG FOREGO_VERSION=0.16.1
7+
8+
# Use a specific version of golang to build both binaries
9+
FROM golang:$GO_VERSION as gobuilder
10+
11+
# Build docker-gen from scratch
12+
FROM gobuilder as dockergen
13+
14+
# Download the sources for the given version
15+
ARG DOCKER_GEN_VERSION
16+
ADD https://github.com/jwilder/docker-gen/archive/${DOCKER_GEN_VERSION}.tar.gz sources.tar.gz
17+
18+
# Move the sources into the right directory
19+
RUN tar -xzf sources.tar.gz && \
20+
mkdir -p /go/src/github.com/jwilder/ && \
21+
mv docker-gen-* /go/src/github.com/jwilder/docker-gen
22+
23+
# Install the dependencies and make the docker-gen executable
24+
WORKDIR /go/src/github.com/jwilder/docker-gen
25+
RUN go get -v ./... && \
26+
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.buildVersion=${DOCKER_GEN_VERSION}" ./cmd/docker-gen
27+
28+
# Build forego from scratch
29+
# Because this relies on golang workspaces, we need to use go < 1.8.
30+
FROM gobuilder as forego
31+
32+
# Download the sources for the given version
33+
ARG FOREGO_VERSION
34+
ADD https://github.com/jwilder/forego/archive/v${FOREGO_VERSION}.tar.gz sources.tar.gz
35+
36+
# Move the sources into the right directory
37+
RUN tar -xzf sources.tar.gz && \
38+
mkdir -p /go/src/github.com/ddollar/ && \
39+
mv forego-* /go/src/github.com/ddollar/forego
40+
41+
# Install the dependencies and make the forego executable
42+
WORKDIR /go/src/github.com/ddollar/forego/
43+
RUN go get -v ./... && \
44+
CGO_ENABLED=0 GOOS=linux go build -o forego .
45+
46+
# Build the final image
47+
FROM nginx:$NGINX_VERSION
248
LABEL maintainer="Jason Wilder [email protected]"
349

450
# Install wget and install/updates certificates
@@ -14,15 +60,14 @@ RUN apt-get update \
1460
RUN echo "daemon off;" >> /etc/nginx/nginx.conf \
1561
&& sed -i 's/worker_processes 1/worker_processes auto/' /etc/nginx/nginx.conf
1662

17-
# Install Forego
18-
ADD https://github.com/jwilder/forego/releases/download/v0.16.1/forego /usr/local/bin/forego
19-
RUN chmod u+x /usr/local/bin/forego
20-
21-
ENV DOCKER_GEN_VERSION 0.7.4
63+
# Install Forego + docker-gen
64+
COPY --from=forego /go/src/github.com/ddollar/forego/forego /usr/local/bin/forego
65+
COPY --from=dockergen /go/src/github.com/jwilder/docker-gen/docker-gen /usr/local/bin/docker-gen
2266

23-
RUN wget https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
24-
&& tar -C /usr/local/bin -xvzf docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
25-
&& rm /docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz
67+
# Add DOCKER_GEN_VERSION environment variable
68+
# Because some external projects rely on it
69+
ARG DOCKER_GEN_VERSION
70+
ENV DOCKER_GEN_VERSION=${DOCKER_GEN_VERSION}
2671

2772
COPY network_internal.conf /etc/nginx/
2873

Dockerfile.alpine

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1-
FROM nginx:1.19.3-alpine
1+
# setup build arguments for version of dependencies to use
2+
ARG NGINX_VERSION=1.19.3-alpine
3+
ARG GO_VERSION=1.14-alpine
4+
5+
ARG DOCKER_GEN_VERSION=0.7.4
6+
ARG FOREGO_VERSION=0.16.1
7+
8+
# Use a specific version of golang to build both binaries
9+
FROM golang:$GO_VERSION as gobuilder
10+
RUN apk add --no-cache git
11+
12+
# Build docker-gen from scratch
13+
FROM gobuilder as dockergen
14+
15+
# Download the sources for the given version
16+
ARG DOCKER_GEN_VERSION
17+
ADD https://github.com/jwilder/docker-gen/archive/${DOCKER_GEN_VERSION}.tar.gz sources.tar.gz
18+
19+
# Move the sources into the right directory
20+
RUN tar -xzf sources.tar.gz && \
21+
mkdir -p /go/src/github.com/jwilder/ && \
22+
mv docker-gen-* /go/src/github.com/jwilder/docker-gen
23+
24+
# Install the dependencies and make the docker-gen executable
25+
WORKDIR /go/src/github.com/jwilder/docker-gen
26+
RUN go get -v ./... && \
27+
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.buildVersion=${DOCKER_GEN_VERSION}" ./cmd/docker-gen
28+
29+
# Build forego from scratch
30+
# Because this relies on golang workspaces, we need to use go < 1.8.
31+
FROM gobuilder as forego
32+
33+
# Download the sources for the given version
34+
ARG FOREGO_VERSION
35+
ADD https://github.com/jwilder/forego/archive/v${FOREGO_VERSION}.tar.gz sources.tar.gz
36+
37+
# Move the sources into the right directory
38+
RUN tar -xzf sources.tar.gz && \
39+
mkdir -p /go/src/github.com/ddollar/ && \
40+
mv forego-* /go/src/github.com/ddollar/forego
41+
42+
# Install the dependencies and make the forego executable
43+
WORKDIR /go/src/github.com/ddollar/forego/
44+
RUN go get -v ./... && \
45+
CGO_ENABLED=0 GOOS=linux go build -o forego .
46+
47+
# Build the final image
48+
FROM nginx:$NGINX_VERSION
249
LABEL maintainer="Jason Wilder [email protected]"
350

451
# Install wget and install/updates certificates
@@ -11,15 +58,14 @@ RUN apk add --no-cache --virtual .run-deps \
1158
RUN echo "daemon off;" >> /etc/nginx/nginx.conf \
1259
&& sed -i 's/worker_processes 1/worker_processes auto/' /etc/nginx/nginx.conf
1360

14-
# Install Forego
15-
ADD https://github.com/jwilder/forego/releases/download/v0.16.1/forego /usr/local/bin/forego
16-
RUN chmod u+x /usr/local/bin/forego
17-
18-
ENV DOCKER_GEN_VERSION 0.7.4
61+
# Install Forego + docker-gen
62+
COPY --from=forego /go/src/github.com/ddollar/forego/forego /usr/local/bin/forego
63+
COPY --from=dockergen /go/src/github.com/jwilder/docker-gen/docker-gen /usr/local/bin/docker-gen
1964

20-
RUN wget --quiet https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-alpine-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
21-
&& tar -C /usr/local/bin -xvzf docker-gen-alpine-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
22-
&& rm /docker-gen-alpine-linux-amd64-$DOCKER_GEN_VERSION.tar.gz
65+
# Add DOCKER_GEN_VERSION environment variable
66+
# Because some external projects rely on it
67+
ARG DOCKER_GEN_VERSION
68+
ENV DOCKER_GEN_VERSION=${DOCKER_GEN_VERSION}
2369

2470
COPY network_internal.conf /etc/nginx/
2571

0 commit comments

Comments
 (0)