Last active
May 14, 2018 07:12
-
-
Save adriagalin/45f2ac4e3616d565d4fd3d6044adcefd to your computer and use it in GitHub Desktop.
Go Multistage Dockerfile builder
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
# Builder stage | |
FROM golang:1.9 AS builder | |
LABEL MAINTAINER "Team Name <team@email.com>" | |
# Install tools required to build the project | |
RUN go get github.com/golang/dep/cmd/dep | |
# Add ssh key to clone private repos | |
ARG SSH_PRIVATE_KEY | |
COPY ${SSH_PRIVATE_KEY} /root/.ssh/id_rsa | |
RUN echo "[url \"git@gitlab.com:\"]\n\tinsteadOf = https://gitlab.com/" >> /root/.gitconfig \ | |
&& echo "StrictHostKeyChecking no " > /root/.ssh/config \ | |
&& chmod 600 /root/.ssh/id_rsa | |
# Gopkg.toml and Gopkg.lock lists project dependencies | |
# These layers are only re-built when Gopkg files are updated | |
WORKDIR /go/src/gitlab.com/repo | |
COPY Gopkg.toml Gopkg.lock ./ | |
# Install library dependencies | |
RUN dep ensure -v --vendor-only | |
# Copy all project and build it | |
# This layer is rebuilt when ever a file has changed in the project directory | |
COPY . ./ | |
RUN CGO_ENABLED=0 GOOS=linux go install ./... | |
# Production build stage | |
FROM alpine:3.7 | |
LABEL MAINTAINER "Team Name <team@email.com>" | |
RUN apk update && apk add ca-certificates mailcap && rm -rf /var/cache/apk/* | |
EXPOSE 8080 | |
WORKDIR /usr/bin | |
COPY --from=builder /go/bin . | |
ENTRYPOINT [ "./binaryName" ] | |
CMD ["--help"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
docker build --build-arg SSH_PRIVATE_KEY=$(SSH_PRIVATE_KEY) -t $(NAME):$(VERSION) -f $(BUILD_PATH)/$(DOCKERFILE) $(BUILD_PATH)