2023-09-03 16:16:50 +00:00
|
|
|
# syntax=docker/dockerfile:1
|
2020-07-08 11:12:21 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# -✂- this stage is used to develop and build the application locally -------------------------------------------------
|
|
|
|
FROM docker.io/library/golang:1.22-bookworm AS develop
|
2023-09-03 16:16:50 +00:00
|
|
|
|
2024-07-04 08:17:34 +00:00
|
|
|
# use the /var/tmp/go as the GOPATH to reuse the modules cache
|
2024-07-03 14:12:13 +00:00
|
|
|
ENV GOPATH="/var/tmp/go"
|
|
|
|
|
|
|
|
RUN set -x \
|
|
|
|
# renovate: source=github-releases name=golangci/golangci-lint
|
|
|
|
&& GOLANGCI_LINT_VERSION="1.59.1" \
|
|
|
|
&& wget -O- -nv "https://cdn.jsdelivr.net/gh/golangci/golangci-lint@v${GOLANGCI_LINT_VERSION}/install.sh" \
|
|
|
|
| sh -s -- -b /bin "v${GOLANGCI_LINT_VERSION}"
|
|
|
|
|
|
|
|
RUN set -x \
|
|
|
|
# customize the shell prompt (for the bash)
|
|
|
|
&& echo "PS1='\[\033[1;36m\][go] \[\033[1;34m\]\w\[\033[0;35m\] \[\033[1;36m\]# \[\033[0m\]'" >> /etc/bash.bashrc
|
2023-09-03 16:16:50 +00:00
|
|
|
|
|
|
|
WORKDIR /src
|
|
|
|
|
|
|
|
# burn the modules cache
|
2024-07-03 14:12:13 +00:00
|
|
|
RUN \
|
|
|
|
--mount=type=bind,source=go.mod,target=/src/go.mod \
|
|
|
|
--mount=type=bind,source=go.sum,target=/src/go.sum \
|
|
|
|
go mod download -x \
|
|
|
|
&& find "${GOPATH}" -type d -exec chmod 0777 {} \; \
|
|
|
|
&& find "${GOPATH}" -type f -exec chmod 0666 {} \;
|
2023-09-03 16:16:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# -✂- this stage is used to compile the application -------------------------------------------------------------------
|
|
|
|
FROM develop AS compile
|
2021-04-13 13:55:03 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# can be passed with any prefix (like `v1.2.3@GITHASH`), e.g.: `docker build --build-arg "APP_VERSION=v1.2.3" .`
|
2021-09-29 15:38:50 +00:00
|
|
|
ARG APP_VERSION="undefined@docker"
|
2020-07-08 11:12:21 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
RUN --mount=type=bind,source=.,target=/src set -x \
|
|
|
|
&& go generate ./... \
|
|
|
|
&& CGO_ENABLED=0 LDFLAGS="-s -w -X gh.tarampamp.am/error-pages/internal/appmeta.version=${APP_VERSION}" \
|
|
|
|
go build -trimpath -ldflags "${LDFLAGS}" -o /tmp/error-pages ./cmd/error-pages/ \
|
|
|
|
&& /tmp/error-pages --version \
|
|
|
|
&& /tmp/error-pages -h
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# -✂- this stage is used to prepare the runtime fs --------------------------------------------------------------------
|
|
|
|
FROM docker.io/library/alpine:3.20 AS rootfs
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2021-04-13 13:55:03 +00:00
|
|
|
WORKDIR /tmp/rootfs
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
# prepare rootfs for runtime
|
2024-07-04 08:17:34 +00:00
|
|
|
RUN set -x \
|
2024-07-03 14:12:13 +00:00
|
|
|
&& mkdir -p ./etc ./bin \
|
2021-09-29 15:38:50 +00:00
|
|
|
&& echo 'appuser:x:10001:10001::/nonexistent:/sbin/nologin' > ./etc/passwd \
|
2024-07-03 14:12:13 +00:00
|
|
|
&& echo 'appuser:x:10001:' > ./etc/group
|
|
|
|
|
|
|
|
# take the binary from the compile stage
|
|
|
|
COPY --from=compile /tmp/error-pages ./bin/error-pages
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
WORKDIR /tmp/rootfs/opt
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# generate static error pages (for use inside other Docker images, for example)
|
2021-09-29 15:38:50 +00:00
|
|
|
RUN set -x \
|
2024-07-03 14:12:13 +00:00
|
|
|
&& mkdir ./html \
|
|
|
|
&& ./../bin/error-pages build --index --target-dir ./html \
|
2021-09-29 15:38:50 +00:00
|
|
|
&& ls -l ./html
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# -✂- and this is the final stage (an empty filesystem is used) -------------------------------------------------------
|
2023-09-03 16:17:07 +00:00
|
|
|
FROM scratch AS runtime
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
ARG APP_VERSION="undefined@docker"
|
2020-07-08 11:12:21 +00:00
|
|
|
|
2021-04-13 13:55:03 +00:00
|
|
|
LABEL \
|
2024-07-03 14:12:13 +00:00
|
|
|
# docs: https://github.com/opencontainers/image-spec/blob/master/annotations.md
|
2021-04-13 13:55:03 +00:00
|
|
|
org.opencontainers.image.title="error-pages" \
|
2024-07-04 08:17:34 +00:00
|
|
|
org.opencontainers.image.description="Pretty server's error pages" \
|
2021-04-13 13:55:03 +00:00
|
|
|
org.opencontainers.image.url="https://github.com/tarampampam/error-pages" \
|
|
|
|
org.opencontainers.image.source="https://github.com/tarampampam/error-pages" \
|
|
|
|
org.opencontainers.image.vendor="tarampampam" \
|
2021-09-29 15:38:50 +00:00
|
|
|
org.opencontainers.version="$APP_VERSION" \
|
2021-04-13 13:55:03 +00:00
|
|
|
org.opencontainers.image.licenses="MIT"
|
2020-07-08 11:12:21 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# import from builder
|
|
|
|
COPY --from=rootfs /tmp/rootfs /
|
2021-04-13 16:51:23 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# use an unprivileged user
|
2022-04-09 15:39:52 +00:00
|
|
|
USER 10001:10001
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
WORKDIR /opt
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# to find out which environment variables and CLI arguments are supported by the application, run the app
|
|
|
|
# with the `--help` flag or refer to the documentation at https://github.com/tarampampam/error-pages#readme
|
|
|
|
|
|
|
|
ENV LOG_LEVEL="warn"
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
# docs: https://docs.docker.com/reference/dockerfile/#healthcheck
|
|
|
|
HEALTHCHECK --interval=10s --start-interval=1s --start-period=5s --timeout=2s CMD [\
|
|
|
|
"/bin/error-pages", "--log-format", "json", "healthcheck" \
|
|
|
|
]
|
2021-09-06 06:47:10 +00:00
|
|
|
|
2021-09-29 15:38:50 +00:00
|
|
|
ENTRYPOINT ["/bin/error-pages"]
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
CMD ["--log-format", "json", "serve"]
|