Skip to content

Commit 3640555

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

File tree

1 file changed

+109
-28
lines changed

1 file changed

+109
-28
lines changed

‎Dockerfile.precommit‎

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,118 @@
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
129

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

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}"
11+
# =========================================================================
12+
# Golang Stage
13+
# =========================================================================
14+
FROM golang:1.24-bookworm AS go_builder
15+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
2016

21-
# Markdown
22-
RUN npm install -g markdownlint-cli
17+
ARG GOLANGCI_LINT_VERSION=v2.5.0
18+
ENV PATH=/usr/local/go/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2319

24-
# Install pre-commit and tools
25-
RUN pip install --break-system-packages pre-commit
20+
# ==> Install golangci-lint
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
2625

27-
# Yamllint
28-
RUN pip install --break-system-packages yamllint
2926

30-
# CodeSpell
31-
RUN pip install --break-system-packages codespell
27+
# =========================================================================
28+
# Rust Stage
29+
# =========================================================================
30+
FROM rust:bookworm AS rust_builder
31+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
3232

33-
# Shellcheck
34-
RUN pip install --break-system-packages shellcheck-py
33+
# ==> Install rustfmt
34+
RUN set -eux; \
35+
rustup component add rustfmt 2>/dev/null || true
36+
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+
# =========================================================================
39+
# Python Stage
40+
# =========================================================================
41+
FROM python:3.11-bookworm AS python_builder
42+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
43+
44+
ENV PRE_COMMIT_HOME=/opt/pre-commit
45+
46+
# ==> Install pre-commit and related tools
47+
RUN set -eux; \
48+
python3 -m venv /opt/py-tools; \
49+
/opt/py-tools/bin/pip install --upgrade pip; \
50+
/opt/py-tools/bin/pip install --no-cache-dir pre-commit yamllint codespell; \
51+
/opt/py-tools/bin/pip cache purge >/dev/null 2>&1 || true; \
52+
find /opt/py-tools -type d -name '__pycache__' -prune -exec rm -rf {} +; \
53+
find /opt/py-tools -name '*.pyc' -delete; \
54+
find /opt/py-tools -type d -name 'tests' -exec rm -rf {} +
55+
56+
57+
# =========================================================================
58+
# Final Stage
59+
# =========================================================================
60+
FROM python:3.11-bookworm
61+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
62+
63+
LABEL maintainer="Gemini"
64+
LABEL description="Slimmed-down pre-commit environment"
65+
66+
ARG APT_MIRROR
67+
ENV APT_MIRROR=${APT_MIRROR}
68+
69+
# ==> 1. Set environment variables
70+
ENV LANG=C.UTF-8 \
71+
RUSTUP_HOME=/opt/rustup \
72+
CARGO_HOME=/opt/cargo \
73+
PIP_BREAK_SYSTEM_PACKAGES=1 \
74+
PRE_COMMIT_HOME=/opt/pre-commit \
75+
NODE_PATH=/usr/local/lib/node_modules \
76+
PATH=/usr/local/go/bin:/opt/cargo/bin:/opt/py-tools/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
77+
RUN set -eux; \
78+
python3 -m venv /opt/py-tools; \
79+
/opt/py-tools/bin/pip install --upgrade pip; \
80+
/opt/py-tools/bin/pip install --no-cache-dir pre-commit yamllint codespell; \
81+
/opt/py-tools/bin/pip cache purge >/dev/null 2>&1 || true; \
82+
find /opt/py-tools -type d -name '__pycache__' -prune -exec rm -rf {} +; \
83+
find /opt/py-tools -name '*.pyc' -delete
84+
85+
RUN set -eux; \
86+
if [ -n "${APT_MIRROR:-}" ]; then \
87+
rm -f /etc/apt/sources.list.d/debian.sources; \
88+
SEC_MIRROR="${APT_MIRROR/debian/debian-security}"; \
89+
echo "deb ${APT_MIRROR} bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list; \
90+
echo "deb ${APT_MIRROR} bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list; \
91+
echo "deb ${SEC_MIRROR} bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list; \
92+
fi
93+
94+
# ==> 2. Install only essential runtime packages
95+
RUN set -eux; \
96+
apt-get update; \
97+
apt-get install -y --no-install-recommends --fix-missing \
98+
ca-certificates \
99+
git \
100+
make \
101+
gcc \
102+
pkg-config \
103+
libssl-dev \
104+
shellcheck; \
105+
rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man /usr/share/info /usr/share/locale /usr/share/zoneinfo
106+
107+
# ==> 3. Copy tools and runtimes from the builder stage
108+
COPY --from=go_builder /usr/local/go /usr/local/go
109+
COPY --from=go_builder /usr/local/bin/golangci-lint /usr/local/bin/golangci-lint
110+
COPY --from=rust_builder /usr/local/rustup /opt/rustup
111+
COPY --from=rust_builder /usr/local/cargo /opt/cargo
112+
COPY --from=node_builder /usr/local/bin/node /usr/local/bin/node
113+
RUN ln -sf /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm
114+
RUN ln -sf /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx
115+
COPY --from=node_builder /usr/local/lib/node_modules /usr/local/lib/node_modules
116+
RUN ln -sf /usr/local/lib/node_modules/markdownlint-cli/markdownlint.js /usr/local/bin/markdownlint
117+
118+
WORKDIR /app

0 commit comments

Comments
 (0)