首頁·終端機命令

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