Conversation
There was a problem hiding this comment.
Pull request overview
ビルド時間短縮を目的に、Rustビルド設定とDockerビルド手順を最適化するPRです。
Changes:
.cargo/config.tomlでmuslターゲットのリンカーをclangに固定し、moldを確実に利用- 不要な依存関係の削除と
sea-ormの不要feature削除、ビルドプロファイル最適化(release/dev) - Dockerビルドを依存関係先行ビルド+BuildKitキャッシュで高速化(ただし現状キャッシュを壊す行あり)
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Dockerfile | 依存関係を先にビルドしてDockerキャッシュ活用、BuildKit cache mount導入 |
| Cargo.toml | 不要依存の削除、sea-orm feature整理、profile最適化追加 |
| Cargo.lock | 依存削除に伴うロック更新 |
| .cargo/config.toml | musl向けにclangリンカー指定を追加してmold利用を安定化 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| FROM alpine:latest | ||
| COPY . . | ||
| RUN touch src/main.rs |
There was a problem hiding this comment.
RUN touch src/main.rs updates the file timestamp on every build, which makes this layer (and everything after it) effectively uncacheable and reduces build reproducibility. Removing this line (or replacing it with a deterministic cache-busting strategy only when needed) will preserve Docker layer caching and speed up builds.
| RUN touch src/main.rs |
| RUN rm -rf src | ||
|
|
||
| FROM alpine:latest | ||
| COPY . . |
There was a problem hiding this comment.
This COPY . . will send the entire build context into the image. Since the repo has no .dockerignore, it will also include directories like .git/ (and potentially target/ after local builds), which slows builds and can invalidate cache unexpectedly. Consider adding a .dockerignore (or narrowing what gets copied) to keep the build context small and caching effective.
| COPY . . | |
| COPY src ./src |
| --mount=type=cache,target=/root/.cargo/git \ | ||
| cargo build --release -j $(nproc) | ||
|
|
||
| FROM alpine:latest |
There was a problem hiding this comment.
The runtime stage uses FROM alpine:latest, which pins your base image to a mutable latest tag on Docker Hub; if that tag is ever compromised or updated with a malicious or vulnerable image, every build using this Dockerfile can silently inherit the compromise. To make builds deterministic and reduce supply-chain risk, pin the base image to an immutable reference such as a specific version tag or image digest under your control.
Overview
・リンカーが書かれてはいたけど使われてなかったため修正(.cargo/config.toml)
・不要な依存関係の削除(Cargo.toml、Cargo.lock)
・最適化設定の変更(Cargo.toml)
・Dockerのキャッシュ設定や不要なcargo fixの削除(Dockerfile)
Changes
.cargo/config.toml
linkerをclangに明示的に指定することでmoldが正しく使われるようにした。
Cargo.toml,Cargo.lock
これらのクレートを削除。
Dockerfile
わざと
src/main.rsに空のfn main() {}を作り、先にCargo.tomlだけを使ってビルドするようにした。これによりライブラリだけのビルド結果をDockerキャッシュに保存でき、自分のコードを書き替えてもライブラリの再ビルドはスキップできるようにした。
cargo build -j $(nproc)にすることで明示的にすべてのコアを使って並列ビルドできるようにした。不要な
cargo fixを削ることによりビルド時間を短縮した。Optimization Settings
Link Time Optimizationの設定をthinにすることで最適化をしながらビルド時間を伸ばさないようにした。ltoをthinにするとうまくDockerのキャッシュが効かないのでfalseにした。
codegen-unitsを16にすることで並列コンパイルをするようにした。opt-levelを2にすることで実行速度を十分に確保しつつ、ビルド時間も伸びすぎないようにした。最適化を一切せず、デバッグ情報も生成しないようにした。
これによりバイナリサイズが劇的に小さくなりビルド後の書き込み時間を短縮した。