首页·终端命令

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