Skip to content

Commit 6b3a340

Browse files
committed
🔧 chore(docker): overhaul precommit Dockerfile to multi-stage build
Signed-off-by: samzong <[email protected]>
1 parent ee6e87e commit 6b3a340

File tree

1 file changed

+114
-28
lines changed

1 file changed

+114
-28
lines changed

‎Dockerfile.precommit‎

Lines changed: 114 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,123 @@
1-
FROM golang:1.24
1+
# =========================================================================
2+
# Nodejs Stage
3+
# =========================================================================
4+
FROM node:20-bookworm-slim AS node_builder
5+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
26

3-
# Install Base env
4-
RUN apt-get update && apt-get install -y \
5-
make \
6-
build-essential \
7-
pkg-config \
8-
python3 \
9-
libssl-dev \
10-
ca-certificates \
11-
python3-pip
7+
RUN set -eux; \
8+
npm install -g markdownlint-cli; \
9+
npm cache clean --force; \
10+
rm -rf /tmp/*
1211

13-
# Install Node.js and npm
14-
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \
15-
apt-get install -y nodejs
1612

17-
# Install Rust
18-
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
19-
ENV PATH="/root/.cargo/bin:${PATH}"
13+
# =========================================================================
14+
# Golang Stage
15+
# =========================================================================
16+
FROM golang:1.24-bookworm AS go_builder
17+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
2018

21-
# Markdown
22-
RUN npm install -g markdownlint-cli
19+
ARG GOLANGCI_LINT_VERSION=v2.5.0
2320

24-
# Install pre-commit and tools
25-
RUN pip install --break-system-packages pre-commit
21+
RUN set -eux; \
22+
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}; \
23+
mv /go/bin/golangci-lint /usr/local/bin/golangci-lint; \
24+
strip --strip-unneeded /usr/local/bin/golangci-lint 2>/dev/null || true; \
25+
rm -rf /go/pkg/mod /go/pkg/sumdb
2626

27-
# Yamllint
28-
RUN pip install --break-system-packages yamllint
27+
# prune non-essential Go docs/tests to save some space while keeping stdlib
28+
RUN set -eux; \
29+
rm -rf /usr/local/go/api /usr/local/go/doc /usr/local/go/misc /usr/local/go/test
2930

30-
# CodeSpell
31-
RUN pip install --break-system-packages codespell
3231

33-
# Shellcheck
34-
RUN pip install --break-system-packages shellcheck-py
32+
# =========================================================================
33+
# Rust Stage
34+
# =========================================================================
35+
FROM rust:bookworm AS rust_builder
36+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
3537

36-
# Golangci-lint
37-
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.5.0
38+
ARG RUSTUP_MIRROR
39+
40+
ENV RUSTUP_HOME=/usr/local/rustup \
41+
CARGO_HOME=/usr/local/cargo
42+
43+
RUN set -eux; \
44+
if [ -n "${RUSTUP_MIRROR:-}" ]; then \
45+
export RUSTUP_DIST_SERVER="$RUSTUP_MIRROR" RUSTUP_UPDATE_ROOT="$RUSTUP_MIRROR/rustup"; \
46+
fi; \
47+
rustup set profile minimal; \
48+
rustup toolchain install stable --component rustfmt; \
49+
rustup default stable; \
50+
rm -rf ${CARGO_HOME}/registry ${CARGO_HOME}/git ${RUSTUP_HOME}/tmp
51+
52+
53+
# =========================================================================
54+
# Python Stage
55+
# =========================================================================
56+
FROM python:3.11-bookworm AS python_builder
57+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
58+
59+
ENV PRE_COMMIT_HOME=/opt/pre-commit
60+
61+
RUN set -eux; \
62+
python -m venv /opt/py-tools; \
63+
/opt/py-tools/bin/pip install --upgrade pip; \
64+
/opt/py-tools/bin/pip install --no-cache-dir pre-commit yamllint codespell; \
65+
/opt/py-tools/bin/pip cache purge >/dev/null 2>&1 || true; \
66+
find /opt/py-tools -type d -name '__pycache__' -prune -exec rm -rf {} +; \
67+
find /opt/py-tools -name '*.pyc' -delete
68+
69+
70+
# =========================================================================
71+
# Final Stage
72+
# =========================================================================
73+
FROM python:3.11-slim-bookworm
74+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
75+
76+
ARG APT_MIRROR
77+
78+
ENV LANG=C.UTF-8 \
79+
RUSTUP_HOME=/usr/local/rustup \
80+
CARGO_HOME=/usr/local/cargo \
81+
PRE_COMMIT_HOME=/opt/pre-commit \
82+
NODE_PATH=/usr/local/lib/node_modules \
83+
PATH=/usr/local/go/bin:/usr/local/cargo/bin:/opt/py-tools/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
84+
85+
86+
RUN set -eux; \
87+
if [ -n "${APT_MIRROR:-}" ]; then \
88+
rm -f /etc/apt/sources.list.d/debian.sources || true; \
89+
SEC_MIRROR="${APT_MIRROR/debian/debian-security}"; \
90+
echo "deb ${APT_MIRROR} bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list; \
91+
echo "deb ${APT_MIRROR} bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list; \
92+
echo "deb ${SEC_MIRROR} bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list; \
93+
fi
94+
95+
RUN set -eux; \
96+
apt-get update; \
97+
apt-get install -y --no-install-recommends \
98+
ca-certificates \
99+
git \
100+
make \
101+
gcc \
102+
g++ \
103+
libc6-dev \
104+
pkg-config \
105+
libssl-dev \
106+
shellcheck; \
107+
rm -rf /var/lib/apt/lists/*
108+
109+
# Copy tools and runtimes from the builder stage
110+
COPY --from=python_builder /opt/py-tools /opt/py-tools
111+
COPY --from=go_builder /usr/local/go /usr/local/go
112+
COPY --from=go_builder /usr/local/bin/golangci-lint /usr/local/bin/golangci-lint
113+
COPY --from=rust_builder /usr/local/rustup /usr/local/rustup
114+
COPY --from=rust_builder /usr/local/cargo /usr/local/cargo
115+
COPY --from=node_builder /usr/local/bin/node /usr/local/bin/node
116+
COPY --from=node_builder /usr/local/lib/node_modules /usr/local/lib/node_modules
117+
118+
RUN set -eux; \
119+
ln -sf /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm; \
120+
ln -sf /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx; \
121+
ln -sf /usr/local/lib/node_modules/markdownlint-cli/markdownlint.js /usr/local/bin/markdownlint
122+
123+
WORKDIR /app

0 commit comments

Comments
 (0)