Skip to content

Commit

Permalink
Enable multi-architecture docker image builds
Browse files Browse the repository at this point in the history
Previously, the Dockerfile downloaded a 'docker-gen' binary during build
time. This caused a problem as it hard-coded the amd64 architecture for
the image.

This commit updates the Dockerfile to build the docker-gen executable
from source instead of downloading a binary. This updated Dockerfile
uses a multi-stage build [1]. The first stage downloads a source tarball
from github and produces a binary out of it. The second stage
corresponds to the old Dockerfile, with the sole change being that is
fetches the binary from the first stage instead of GitHub.

This has the advantage that the build process no longer assumes a
certain architecture and enables building the image for any architecture
that both the golang and alpine base images support.

[1] https://docs.docker.com/develop/develop-images/multistage-build/
  • Loading branch information
tkw1536 committed Jul 13, 2020
1 parent 4edc190 commit 16dbe96
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
# Build docker-gen from scratch
FROM golang:1.14-alpine as dockergen
RUN apk add --no-cache git

# Download the sources for the given version
ENV VERSION 0.7.3
ADD https://github.com/jwilder/docker-gen/archive/${VERSION}.tar.gz sources.tar.gz

# Move the sources into the right directory
RUN tar -xzf sources.tar.gz && \
mkdir -p /go/src/github.com/jwilder/ && \
mv docker-gen-* /go/src/github.com/jwilder/docker-gen

# Install the dependencies and make the docker-gen executable
WORKDIR /go/src/github.com/jwilder/docker-gen
RUN go get -v ./... && \
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.buildVersion=${VERSION}" ./cmd/docker-gen

FROM alpine:latest
LABEL maintainer="Jason Wilder <mail@jasonwilder.com>"

RUN apk -U add openssl

ENV VERSION 0.7.3
ENV DOWNLOAD_URL https://github.com/jwilder/docker-gen/releases/download/$VERSION/docker-gen-alpine-linux-amd64-$VERSION.tar.gz
COPY --from=dockergen /go/src/github.com/jwilder/docker-gen/docker-gen /usr/local/bin/docker-gen
ENV DOCKER_HOST unix:///tmp/docker.sock

RUN wget -qO- $DOWNLOAD_URL | tar xvz -C /usr/local/bin

ENTRYPOINT ["/usr/local/bin/docker-gen"]

0 comments on commit 16dbe96

Please sign in to comment.