0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】Dockerイメージレイヤーについて

Last updated at Posted at 2025-04-27

Dockerfileと完成したイメージレイヤーを図解する

Dockerfile
FROM ubuntu:20.04

RUN apt update
RUN apt install -y curl

COPY . /app

CMD ["ls", "-la"]

image.png

  • Dockerイメージイメージレイヤーが重なって出来ている
    • イメージレイヤーDockerfileに書いた一つの命令のこと
      • イメージレイヤーは読み取り専用
      • イメージレイヤーは一つ前のイメージレイヤーとの差分を保持する

イメージレイヤーの数とイメージサイズの関係

  • 結論:イメージレイヤーの数が少ないほどイメージサイズも小さい
    • Dockerfileに書いた命令のオーバーヘッドが無いから
FROM ubuntu:20.04

RUN apt update
RUN apt install -y curl
RUN apt install -y vim

CMD ["bash"]
  • ①はイメージサイズが209MBになる
FROM ubuntu:20.04

RUN apt update && apt install -y curl vim

CMD ["bash"]
  • ②はイメージサイズが208MBになる

イメージビルド時のキャッシュ戦略について

結論は以下の通り

  • Dockerfileの内容が変更される可能性が高い開発初期はイメージレイヤーを細かく分ける
  • Dockerfileの内容が固まったらイメージサイズ削減のためにイメージレイヤーの数を減らす

理由は以下の通り

  • docker image buildではイメージレイヤー単位でビルド時にキャッシュされている
  • なので、イメージレイヤーを細かく分けた方が再ビルド時にキャッシュが使われやすい
    • よって、Dockerfileの内容が固まっていない時はイメージレイヤーを細かく分ける
    • 最終的に、Dockerfileの内容が固まったらイメージサイズを削減するためにイメージレイヤーの数を減らす
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?