Skip to content

Instantly share code, notes, and snippets.

@dantheman213
Last active September 28, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantheman213/9146540bfecf57072fc73cdcfd77599b to your computer and use it in GitHub Desktop.
Save dantheman213/9146540bfecf57072fc73cdcfd77599b to your computer and use it in GitHub Desktop.
Go Dockerfile Example
# Dockerfile for basic Go application example
#
# Assumptions
# - There is a project dir
# - Inside the project dir, there is the path src/main
# - The project's *.go files are located in src/main
# - The project's name is "myapp"
FROM golang:1.11.0-stretch AS packager
RUN mkdir -p /opt/build/bin
WORKDIR /opt/build
ENV GOPATH /opt/build
COPY . .
# Install all dependencies listed in *.go import
RUN go get ./...
# Statically build all libraries and find all *.go files and compile them into a single executable
RUN CGO_ENABLED=0; \
GOOS=linux; \
go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o bin/myapp $(find src/main | grep -i .go)
# --
FROM golang:1.11.0-stretch AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/myapp .
RUN chmod +x /opt/app/myapp
ENTRYPOINT ["/opt/app/myapp"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment