Skip to content

Instantly share code, notes, and snippets.

@aarjan
Last active August 28, 2018 10:47
Show Gist options
  • Save aarjan/d1be363e0fd9fe9ed2c60213f77f5717 to your computer and use it in GitHub Desktop.
Save aarjan/d1be363e0fd9fe9ed2c60213f77f5717 to your computer and use it in GitHub Desktop.
A simple Dockerfile for a Go application
# Build stage
# Install tools required
FROM golang:1.10.2 AS build
RUN go get -u github.com/golang/dep/cmd/dep
# List project dependencies with Gopkg.toml and Gopkg.lock
# These layers are only re-built when Gopkg files are updated
COPY Gopkg.lock Gopkg.toml /go/src/projectpath/
WORKDIR /go/src/projectpath/
# Install library dependencies
RUN dep ensure -vendor-only
# Copy the entire project and build it
# This layer is rebuilt when a file changes in the project directory
# (For each ADD or COPY, docker compares the checksum of all the files with the checksum in the cache of the existing image.
# It would do the cache lookup, if not same, it would build it again)
COPY . /go/src/projectpath/
# Since, you would be running in a scratch, we need a static build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w' -o /go/bin/project
# We forced a rebuild with -a and tagged netgo to make sure we use built-in net package and not the system's one.
# Disabling CGO also removes the need for the cross-compile dependencies.
# This ldflags -w just disables debug letting the file be smaller.
# In scratch, there is nothing, except the project binary
FROM scratch
COPY --from=build /go/bin/project /bin/project
EXPOSE 3000
ENTRYPOINT [ "/bin/project" ]
CMD [ "--produce" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment