Skip to content

Instantly share code, notes, and snippets.

@adriagalin
Last active May 14, 2018 07:12
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 adriagalin/45f2ac4e3616d565d4fd3d6044adcefd to your computer and use it in GitHub Desktop.
Save adriagalin/45f2ac4e3616d565d4fd3d6044adcefd to your computer and use it in GitHub Desktop.
Go Multistage Dockerfile builder
# 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"]
@adriagalin
Copy link
Author

docker build --build-arg SSH_PRIVATE_KEY=$(SSH_PRIVATE_KEY) -t $(NAME):$(VERSION) -f $(BUILD_PATH)/$(DOCKERFILE) $(BUILD_PATH)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment