Skip to content

Instantly share code, notes, and snippets.

@spyzhov
Last active July 7, 2024 13:41
Show Gist options
  • Save spyzhov/19c55486698aaf27849d563341cfad24 to your computer and use it in GitHub Desktop.
Save spyzhov/19c55486698aaf27849d563341cfad24 to your computer and use it in GitHub Desktop.
Build Golang application in Dockerfile, with private repositories in dependencies, without experimental features.
# NB! not for production!
# https://docs.docker.com/engine/reference/builder/#arg
# Warning: It is not recommended to use build-time variables for passing secrets like github keys, user credentials etc. Build-time variable values are visible to any user of the image with the docker history command.
FROM golang:1.13 as builder
# This argument should be defined as your personal access tokens, from: https://github.com/settings/tokens
ARG GITHUB_ACCESS_TOKEN
# Any kind of configurations:
ENV GO111MODULE=on
ENV GOFLAGS=" -ldflags '-w'"
ENV GOPROXY=direct
ENV GOSUMDB=off
# Setup your repo
WORKDIR /go/src/repo
# Caching layers for dependencies
COPY ./go.mod ./go.sum ./
RUN git config --global url."https://${GITHUB_ACCESS_TOKEN}:@github.com/".insteadOf "https://github.com/" && \
go mod download && \
git config --global --unset url."https://${GITHUB_ACCESS_TOKEN}:@github.com/".insteadOf
# Build stage
COPY . .
RUN go build -o /go/bin/application .
# Result container:
# Use debian if your binary depends on some shared libraries.
# FROM debian:10-slim
FROM busybox:latest
WORKDIR /root
COPY --from=builder /go/bin/application ./application
CMD ["./application"]
@spyzhov
Copy link
Author

spyzhov commented Feb 25, 2020

NB! Not for production!

Documentation

Warning: It is not recommended to use build-time variables for passing secrets like github keys, user credentials etc. Build-time variable values are visible to any user of the image with the docker history command.

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