Last active
September 28, 2018 17:55
-
-
Save dantheman213/9146540bfecf57072fc73cdcfd77599b to your computer and use it in GitHub Desktop.
Go Dockerfile Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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