> ## Content Index
> Fetch the complete content index at: https://sgpublic.xyz/llms.txt
> Use this file to discover other available public pages before exploring further.

# Docker Engine Api 和 docker-buildkit 的坑：BUILDPLATFORM
- URL: https://sgpublic.xyz/p/2025/03/6aac9cb1810a48000153360a/
- Published: 2025-03-05T13:20:57.000Z
- Updated: 2025-03-05T13:20:57.000Z
- Author: Haven Madray
- Tags: 基础设施, Docker, #wp-post

最近在研究打包一个 Python 的基础镜像，想要新增支持 GUI，考虑基于 [GitHub - jlesage/docker-baseimage-gui](https://github.com/jlesage/docker-baseimage-gui?ref=sgpublic.xyz) 构建，项目是使用 Gradle 创建的，使用的插件是基于 Docker Engine Api 完成的 Docker 操作。

在构建镜像的时候遇到以下报错：

```
Using Dockerfile '/vol1/1000/Document/Docker/poetry-docker/src/main/base/baseimage-gui/Dockerfile'
Using images 'mhmzx/docker-python-baseimage:3.10-bookworm-base-gui', 'mhmzx/docker-python-baseimage:3.10.16-bookworm-base-gui-v3.6.5'.
Step 1/176 : ARG BASEIMAGE=unknown
Step 2/176 : ARG ALPINE_PKGS="    openssl     netcat-openbsd "
Step 3/176 : ARG DEBIAN_PKGS="    netcat-openbsd     net-tools     openssl "
Step 4/176 : FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx
Execution failed for task ':buildGui310BookwormImage'.
> Could not build image: failed to parse platform : "" is an invalid component of "": platform specifier component must match "^[A-Za-z0-9_-]+$": invalid argument
```

猜测是变量 `BUILDPLATFORM` 没有赋值，docker 无法识别，但在控制台中命令行构建是正常的。

由于要优先考虑在不修改 Dockerfile 的情况下构建，于是考虑添加 build-arg（`--build-arg BUILDPLATFORM=linux/amd64`），但还是同样的错误。

经过网上冲浪发现：[https://github.com/oauth2-proxy/oauth2-proxy/issues/1729#issuecomment-1211952861](https://github.com/oauth2-proxy/oauth2-proxy/issues/1729?ref=sgpublic.xyz#issuecomment-1211952861) ，意思是说，docker 的 buildkit 插件会自动根据环境和其他参数确定 `TARGETPLATFORM` 和 `BUILDPLATFORM` 的值，但 api 并未执行这一步，所以出现命令行构建正常，但 api 构建失败的现象。

最后的解决方案是用 Gradle 修改 Dockerfile，在第一行添加一句 `ARG BUILDPLATFORM`，然后添加 build-arg 解决。

相关 issue：[https://github.com/bmuschko/gradle-docker-plugin/issues/1233](https://github.com/bmuschko/gradle-docker-plugin/issues/1233?ref=sgpublic.xyz)