本文最后更新于 530 天前,其中的信息可能已经有所发展或是发生改变。
最近在研究打包一个 Python 的基础镜像,想要新增支持 GUI,考虑基于 GitHub – jlesage/docker-baseimage-gui 构建,项目是使用 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 ,意思是说,docker 的 buildkit 插件会自动根据环境和其他参数确定 TARGETPLATFORM 和 BUILDPLATFORM 的值,但 api 并未执行这一步,所以出现命令行构建正常,但 api 构建失败的现象。
最后的解决方案是用 Gradle 修改 Dockerfile,在第一行添加一句 ARG BUILDPLATFORM,然后添加 build-arg 解决。
相关 issue:https://github.com/bmuschko/gradle-docker-plugin/issues/1233