I would like to build Docker images defined in a Compose file for different platforms conditionally. For example, for building production images and pushing to container repository, use linux/amd64
, but for building development images, use the host's native platform.
For individual Docker image builds, Docker docs recommend using the --platform
CLI parameter. However, for Compose, the only way to specify the build platform seems to be the platform
service-level parameter.
For setting that parameter dynamically, I suppose the --env-file
flag of docker compose
CLI could be used, with the compose file specifying platform: ${BUILD_PLATFORM}
and the environment file including something like BUILD_PLATFORM="linux/amd64"
. However, this doesn't allow a "use default host platform" option.
Is there another approach that would allow to only optionally specify the platform, similarly to omitting the --platform
when using the non-Compose docker build
command?
You should be able to use Compose's default interpolation value to achieve this.
Something like:
...
platform: ${BUILD_PLATFORM:-}
...
This worked for me on Debian 12. I was able to build with BUILD_PLATFORM=linux docker-compose build
, BUILD_PLATFORM=arm docker-compose build
and docker-compose build
but not BUILD_PLATFORM=nope docker-compose build
. The bogus value results in an error and the following message is printed: "nope": unknown operating system or architecture: invalid argument
.
Specifying platform: ''
also appears to result in the "default" behavior you're after.