Skip to content

Instantly share code, notes, and snippets.

@Segmentational
Last active February 18, 2024 23:29
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 Segmentational/de24be895dc4a65a848e5d59005e2367 to your computer and use it in GitHub Desktop.
Save Segmentational/de24be895dc4a65a848e5d59005e2367 to your computer and use it in GitHub Desktop.
Optimized GO HTTP Server Dockerfile - Kubernetes, Private VCS Compatible
# syntax = docker/dockerfile:1.0-experimental
FROM golang:1.22-alpine as BUILD
# ARG NETRCPASSWORD
# ENV NETRCPASSWORD "${NETRCPASSWORD}"
# ENV GOPRIVATE "github.com/example/*"
ENV GOOS "linux"
ENV GOVCS "*:all"
ENV CGO_ENABLED "0"
WORKDIR /
COPY . ./
# RUN echo "machine github.com" > ~/.netrc
# RUN echo "login Segmentational" >> ~/.netrc
# RUN echo "password ${NETRCPASSWORD}" >> ~/.netrc
RUN apk add --no-cache git
RUN go mod download && go build -ldflags "-s -w" -o /http-api
# --> Prevents shell access
RUN adduser -h "/dev/null" -g "" -s "/sbin/nologin" -D -H -u 10001 api-service-user
FROM scratch as RELEASE
WORKDIR /
COPY --from=BUILD /.env /.env
COPY --from=BUILD /etc/passwd /etc/passwd
COPY --from=BUILD /http-api /usr/local/bin/http-api
COPY --from=BUILD /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
USER api-service-user
CMD ["http-api"]
@Segmentational
Copy link
Author

Ideal usage would mount a .netrc as a Docker Secret (for private build dependencies), and the .env as s Kubernetes Secret.

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