Skip to content

Instantly share code, notes, and snippets.

@ulexxander
Created May 27, 2024 09:03
Show Gist options
  • Save ulexxander/2c031e119fbb0074aa72c259e97c147d to your computer and use it in GitHub Desktop.
Save ulexxander/2c031e119fbb0074aa72c259e97c147d to your computer and use it in GitHub Desktop.
Cache and secret mounts in Dockerfile for Go projects
#!/bin/sh
git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "https://gitlab.com"
docker build -t the-app --build-arg "VERSION=${VERSION}" --secret id=gitconfig,src=${HOME}/.gitconfig .
FROM golang:1.20-bookworm AS builder
WORKDIR /build
ENV \
GOOS=linux \
GOARCH=amd64 \
CGO_ENABLED=0 \
GOCACHE=/go-build-cache \
GOMODCACHE=/go-mod-cache
COPY . .
ARG VERSION
RUN \
--mount=type=secret,id=gitconfig,target=/root/.gitconfig \
--mount=type=cache,target=/go-mod-cache \
--mount=type=cache,target=/go-build-cache \
go build -ldflags "-X 'main.version=${VERSION}'" ./cmd/service
FROM buildpack-deps:bookworm-curl
WORKDIR /app
COPY --from=builder /build/service .
EXPOSE 80
CMD ["./service"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment