docker build

パッケージと実行環境

書き方

docker build -t <name>:<tag> [-f <Dockerfile>] <context>

Dockerfileからイメージを作ります。末尾の点はデーモンへ送るビルドコンテキストなので、フォルダーが大きいとビルドが遅くなり(.dockerignoreで削ります)、COPYはそのコンテキスト内のファイルしか見えません。

よく使うオプション

オプション意味
-t <name>:<tag>name the image; without it you only get an id
-f <file>use a Dockerfile that is not ./Dockerfile
--no-cacherebuild every layer
--build-arg KEY=valuepass an ARG into the build
--target <stage>stop at one stage of a multi-stage file
--platform linux/amd64build for another architecture
--progress=plainfull log output instead of the collapsed view

docker build -t myapp:1.0 .

builds from ./Dockerfile with the current folder as context

docker build -f docker/Dockerfile.dev -t myapp:dev .

another Dockerfile, same context

docker build --platform linux/amd64 -t myapp:1.0 .

an x86 image built on an Apple Silicon Mac

何をどこに入れるかが問題です。このプロジェクトの中か、マシン全体かを先に決めてください。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. docker build は何をしますか。

Dockerfileからイメージを作ります。末尾の点はデーモンへ送るビルドコンテキストなので、フォルダーが大きいとビルドが遅くなり(.dockerignoreで削ります)、COPYはそのコンテキスト内のファイルしか見えません。

Q. どう打ちますか。

docker build -t <name>:<tag> [-f <Dockerfile>] <context> — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは7個です。全部は man docker にあります。このコマンドはパッケージと実行環境の仲間です。

関連するコマンド

man ページ: man docker-build