解决 Docker Compose 构建报错 Service 'app' failed to build: ARG not found
分析 Dockerfile 中 ARG 参数在 compose build 时未注入诱因,详解 args 配置规范。
问题现象
使用 docker compose build 构建自定义镜像时报错中断:
Service 'app' failed to build: invalid reference format: repository name must be lowercase
排查发现是 Dockerfile 中的 ARG BASE_IMAGE 为空。
触发根因
在 docker-compose.yml 中仅声明了 build: .,未显式在 args 块中将环境变量传递给 Dockerfile 的 ARG 指令。
解决方案
在 docker-compose.yml 中完整声明 args:
services:
app:
build:
context: .
args:
BASE_IMAGE: "node:18-alpine"
APP_ENV: "production"
验证方法
再次执行构建,参数正确插值,镜像成功构建完成。


