You can use this example to work with private Docker repository in Dockerfile.


GitHub token


Create a GitHub token and set it in your local environment. For instance add export GITHUB_TOKEN=ghp_adsa543HG5678zae39gds435879fhg to your bash profile file.


Dockerfile


FROM golang:1.20.2-alpine3.17 as build
WORKDIR /source
COPY . .

ARG GITHUB_TOKEN
RUN apk add --no-cache git
RUN git config --global url."https://${GITHUB_TOKEN}:@github.com/".insteadOf "https://github.com/"

RUN go mod verify
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o ./bin/app ./cmd/app/...

FROM alpine:3.17
RUN addgroup -S -g 1000 app-group
RUN adduser -S -u 1000 app-user app-group
USER app-user:app-group
COPY --from=build /source/bin/app /bin/app
EXPOSE 8001
ENTRYPOINT ./bin/app

This is another version for a custom user and group.


FROM golang:1.19.3-buster as build
...

FROM debian:buster-20221114-slim
RUN addgroup --system --gid 1000 blog-group
RUN adduser --system --gid 1000 --uid 1000 blog-user
USER blog-user:blog-group
...

docker-compose.yaml


You need to add GITHUB_TOKEN: "${GITHUB_TOKEN}" to args property. After that, you should be able to run docker-compose commands without an issue.