Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Dockerfileチョットカケルになろう/dockerfile-next-steps
Search
aono
October 12, 2024
Programming
85
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dockerfileチョットカケルになろう/dockerfile-next-steps
aono
October 12, 2024
More Decks by aono
See All by aono
RDB(ぽすぐれ)チューニング入門/rdb-tuning-introduction-for-postgresql
awonosuke
0
220
Other Decks in Programming
See All in Programming
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.4k
R8 の設定見直しでアプリサイズ削減
andpad
0
240
Railsバージョンを4倍にした話
optim
0
840
ActionController meets Protocol Buffers
sorah
1
750
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
300
sbt 2.0: takeoff
eed3si9n
0
180
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
4
1.4k
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
250
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
240
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
490
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
230
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
240
Featured
See All Featured
The Cult of Friendly URLs
andyhume
79
7k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
New Earth Scene 8
popppiees
4
2.7k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
31k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
470
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
310
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Paper Plane (Part 1)
katiecoart
PRO
2
12k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Transcript
Dockerfileチョットカケルになろう
目次 1. Docker 2. Dockerfileのおまじない 3. マルチステージビルド 4. RUN --mount=...
5. Dockerのキャッシュ 6. hadolint 7. まとめ 8. 補足
Docker • コンテナ ◦ 軽量性、可搬性が推しらしい ▪ OSや環境に依存せずどんな場所でも同じように動くアプリケーションを作れる ▪ 作ったり壊したりしやすいので破壊的な検証も行いやすい •
ランタイムのアップグレード(Node.js 18.x→20.x)等
Docker • コンテナ化は当たり前 ◦ Docker Compose ◦ Amazon ECS ◦
Cloud Run ◦ k8s(EKS,GKE...)
Docker • Dockerfileはコンテナイメージのレシピ ◦ レシピが良いと誰がどこで作っても良いコンテナが出来上がる ▪ だからDockerfileはこだわっておきたい
Dockerfileのおまじない • # syntax=docker/dockerfile:1 ◦ https://docs.docker.com/reference/dockerfile/#syntax • 新しいお作法を使いまっせ、と宣言 ◦ Docker
BuildKitが有効になってる場合のみ利用できる ▪ Docker BuildKit:Docker 18.09~利用可能
マルチステージビルド • 1つのDockerfileの中に複数のFROM句が存在 ◦ COPY --from=...もマルチステージビルド • 最終的なイメージに必要なリソースだけを詰める ◦ アプリケーションが実行できればOK
▪ コンパイル言語の場合、ビルド済みのバイナリのみ! ◦ 別に最終イメージ用に限らず自由にステージ定義できる ▪ 例)本番、開発環境で同じ依存関係用baseステージ作っておいてCOPY --from=base
RUN --mount=... • 種類(参考) ◦ --mount=type=bind ◦ --mount=type=cache ◦ --mount=type=secret
• --mount=type=bind ◦ ビルドコンテキスト内のリソースをレイヤーに書き込まず利用できる ▪ 例)package*.json、go.{mod,sum}、composer.*等をCOPYしなくて良い • --mount=type=cache ◦ 差分更新だけになるのでコマンド実行が早くなる ▪ RUN --mount=type=cache,target=/root/.npm \ NODE_ENV=production \ npm i ◦ 同じディレクトリであればレイヤーをまたいだキャッシュが使える • --mount=type=secret(参考) ◦ RUNで実行するコマンドで秘匿情報が必要な場合にイメージに書き込まずに注入できる ▪ 例)ビルド時にシークレットキーが必要な場合 ◦ ARG→ENVのパターンを秘匿情報で使わない!
Dockerのキャッシュ • Docker BuildKitのキャッシュ ◦ さっきのRUN --mount=type=cache • Dockerのレイヤーキャッシュ ◦
COPY、RUN等の記述順を変える ◦ マルチステージビルド ◦ ビルドコンテキストに不要なリソースが入らないようにしとく ▪ .dockerignore • .gitignoreとは挙動が違うので注意 • CI/CD上のキャッシュ ◦ GitHub Actionsの場合はgha使っておけばとりあえずOK
ベストプラクティス多くて網羅できない問題...
ベストプラクティス多くて網羅できない問題... ↓ hadolint!
hadolint • Haskell Dockerfile Linter • 使い方 ◦ $ hadolint
path/to/Dockerfile ▪ CI/CDでも導入が簡単 • 良しなにルールを削ったり設定変えれる ◦ チーム開発ではプロジェクトルートに.hadolint.yamlを置いとく ◦ ルールはWikiにまとまってる
まとめ • コンテナ化は当たり前 • Dockerfileの先頭におまじないを入れよう ◦ # syntax=docker/dockerfile:1 • マルチステージビルドを使おう
◦ 1つのDockerfile内にFROMが複数 or COPY --from=... • RUN --mount=...を使おう ◦ --mount=type=bind ◦ --mount=type=cache ◦ --mount=type=secret • Dockerのキャッシュを使おう • hadolintを使おう
参考 • Docker 公式ドキュメント • 2024年版のDockerfileの考え方&書き方 • 2021〜22年頃のDockerfile事情 • Docker
のキャッシュを全力で使いこなそう • Docker BuildKitを理解する
補足 • ADD句の使い道 ◦ リモートからファイルを持ってきたい時 ▪ 例)GitHub等からバイナリを持ってきたい ◦ 持ってくるリソースが圧縮されてたら自動で解凍してくれる
補足 • ADD句の使い道 ◦
補足 • docker-composeじゃなくてdocker compose使おう • COPY --chown使おう