Skip to content

Instantly share code, notes, and snippets.

@vaind
Last active March 19, 2020 02:52
Show Gist options
  • Save vaind/6a3987a0af27f50cf194fe7143f0ae31 to your computer and use it in GitHub Desktop.
Save vaind/6a3987a0af27f50cf194fe7143f0ae31 to your computer and use it in GitHub Desktop.
Go - minimal docker image build
FROM golang:1.13.0-stretch AS builder
ENV GO111MODULE=on \
CGO_ENABLED=1
WORKDIR /build
# Let's cache modules retrieval - those don't change so often
COPY go.mod .
COPY go.sum .
RUN go mod download
# Copy the code necessary to build the application
# You may want to change this to copy only what you actually need.
COPY . .
# Build the application
RUN go build ./cmd/my-awesome-go-program
# Let's create a /dist folder containing just the files necessary for runtime.
# Later, it will be copied as the / (root) of the output image.
WORKDIR /dist
RUN cp /build/my-awesome-go-program ./my-awesome-go-program
# Optional: in case your application uses dynamic linking (often the case with CGO),
# this will collect dependent libraries so they're later copied to the final image
# NOTE: make sure you honor the license terms of the libraries you copy and distribute
RUN ldd my-awesome-go-program | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;'
RUN mkdir -p lib64 && cp /lib64/ld-linux-x86-64.so.2 lib64/
# Copy or create other directories/files your app needs during runtime.
# E.g. this example uses /data as a working directory that would probably
# be bound to a perstistent dir when running the container normally
RUN mkdir /data
# Create the minimal runtime image
FROM scratch
COPY --chown=0:0 --from=builder /dist /
# Set up the app to run as a non-root user inside the /data folder
# User ID 65534 is usually user 'nobody'.
# The executor of this image should still specify a user during setup.
COPY --chown=65534:0 --from=builder /data /data
USER 65534
WORKDIR /data
ENTRYPOINT ["/my-awesome-go-program"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment