Skip to content

Commit 87a7411

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 87a7411

File tree

2 files changed

+85
-18
lines changed

2 files changed

+85
-18
lines changed

Dockerfile

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

@@ -14,15 +53,9 @@ RUN apt-get update \
1453
RUN echo "daemon off;" >> /etc/nginx/nginx.conf \
1554
&& sed -i 's/worker_processes 1/worker_processes auto/' /etc/nginx/nginx.conf
1655

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
22-
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
56+
# Install Forego + docker-gen
57+
COPY --from=forego /go/src/github.com/ddollar/forego/forego /usr/local/bin/forego
58+
COPY --from=dockergen /go/src/github.com/jwilder/docker-gen/docker-gen /usr/local/bin/docker-gen
2659

2760
COPY network_internal.conf /etc/nginx/
2861

Dockerfile.alpine

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

@@ -11,15 +51,9 @@ RUN apk add --no-cache --virtual .run-deps \
1151
RUN echo "daemon off;" >> /etc/nginx/nginx.conf \
1252
&& sed -i 's/worker_processes 1/worker_processes auto/' /etc/nginx/nginx.conf
1353

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
19-
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
54+
# Install Forego + docker-gen
55+
COPY --from=forego /go/src/github.com/ddollar/forego/forego /usr/local/bin/forego
56+
COPY --from=dockergen /go/src/github.com/jwilder/docker-gen/docker-gen /usr/local/bin/docker-gen
2357

2458
COPY network_internal.conf /etc/nginx/
2559

0 commit comments

Comments
 (0)