Improve the Docker image

This commit is contained in:
James Mills 2022-03-20 08:28:39 +10:00
parent f926981e02
commit 89b245d0e9
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
3 changed files with 71 additions and 22 deletions

7
.dockerfiles/entrypoint.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
[ -n "${PUID}" ] && usermod -u "${PUID}" msgbus
[ -n "${PGID}" ] && groupmod -g "${PGID}" msgbus
printf "Switching UID=%s and GID=%s\n" "${PUID}" "${PGID}"
exec su-exec msgbus:msgbus "$@"

View File

@ -1,2 +1,14 @@
Dockerfile
*~
*.bak
.DS_Store
*.idea
/dist
/msgbus
/msgbusd
/cmd/msgbus/msgbus
/cmd/msgbusd/msgbusd

View File

@ -1,35 +1,65 @@
# Build
FROM golang:alpine AS build
ARG TAG
ARG BUILD
RUN apk add --no-cache -U build-base git make
ENV LIBRARY msgbus
ENV SERVER msgbusd
ENV CLIENT msgbus
ENV REPO prologic/$LIBRARY
RUN mkdir -p /src
RUN apk add --update git make build-base && \
rm -rf /var/cache/apk/*
WORKDIR /src
WORKDIR /go/src/github.com/$REPO
COPY . /go/src/github.com/$REPO
RUN make TAG=$TAG BUILD=$BUILD build
# Copy Makefile
COPY Makefile ./
# Install deps
RUN make deps
# Copy go.mod and go.sum and install and cache dependencies
COPY go.mod .
COPY go.sum .
# Copy sources
COPY *.go ./
COPY ./client/*.go ./client/
COPY ./cmd/msgbusd/*.go ./cmd/msgbusd/
COPY ./cmd/msgbus/*.go ./cmd/msgbus/
# Version/Commit (there there is no .git in Docker build context)
# NOTE: This is fairly low down in the Dockerfile instructions so
# we don't break the Docker build cache just be changing
# unrelated files that actually haven't changed but caused the
# COMMIT value to change.
ARG VERSION="0.0.0"
ARG COMMIT="HEAD"
# Build client binary
RUN make cli VERSION=$VERSION COMMIT=$COMMIT
# Build server binary
RUN make server VERSION=$VERSION COMMIT=$COMMIT
# Runtime
FROM scratch
FROM alpine:latest
ENV LIBRARY msgbus
ENV SERVER msgbusd
ENV CLIENT msgbus
ENV REPO prologic/$LIBRARY
RUN apk --no-cache -U add su-exec shadow ca-certificates tzdata
LABEL msgbud.app main
ENV PUID=1000
ENV PGID=1000
COPY --from=build /go/src/github.com/${REPO}/cmd/${SERVER}/${SERVER} /${SERVER}
COPY --from=build /go/src/github.com/${REPO}/cmd/${CLIENT}/${CLIENT} /${CLIENT}
RUN addgroup -g "${PGID}" msgbus && \
adduser -D -H -G msgbus -h /var/empty -u "${PUID}" msgbus && \
mkdir -p /data && chown -R msgbus:msgbus /data
EXPOSE 8000/tcp
VOLUME /data
ENTRYPOINT ["/msgbusd"]
CMD []
WORKDIR /
# force cgo resolver
ENV GODEBUG=netdns=cgo
COPY --from=build /src/msgbusd /usr/local/bin/msgbusd
COPY --from=build /src/msgbus /usr/local/bin/msgbus
COPY .dockerfiles/entrypoint.sh /init
ENTRYPOINT ["/init"]
CMD ["msgbusd"]