bake --push throws error when image field is not defined within docker-compose file
Within a docker-compose file, you might want to build services while only pushing some of them to a registry, for example:
services:
service1:
build:
context: ./service1
service2:
image: ${DOCKER_REGISTRY}/myservice:latest
build:
context: ./service2
Running docker compose push with the above configuration would skip service1 and only push service2.
Running docker buildx bake --push throws an error: error: tag is needed when pushing to registry.
The bake command should be able to skip pushing images which do not have a tag defined.
Global flags like --push apply to all targets. If you want to build specific ones then docker buildx bake service2 servicefoo
@tonistiigi I want to build all targets, but push only the targets which have an image tag defined. I believe it's reasonable to expect this from bake considering that this is the behavior with docker compose.
Looking at your compose file:
docker buildx bake --push --print
{
"group": {
"default": {
"targets": [
"service2",
"service1"
]
}
},
"target": {
"service1": {
"context": "service1",
"dockerfile": "Dockerfile",
"output": [
"type=image,push=true"
]
},
"service2": {
"context": "service2",
"dockerfile": "Dockerfile",
"tags": [
"/myservice:latest"
],
"output": [
"type=image,push=true"
]
}
}
}
If you want to disable push for some services you can add output: type=cacheonly field with x-bake so --push will not override this service:
services:
service1:
build:
context: ./service1
x-bake:
output: type=cacheonly
service2:
image: ${DOCKER_REGISTRY}/myservice:latest
build:
context: ./service2
docker buildx bake --push --print
{
"group": {
"default": {
"targets": [
"service2",
"service1"
]
}
},
"target": {
"service1": {
"context": "service1",
"dockerfile": "Dockerfile",
"output": [
"type=cacheonly"
]
},
"service2": {
"context": "service2",
"dockerfile": "Dockerfile",
"tags": [
"/myservice:latest"
],
"output": [
"type=image,push=true"
]
}
}
}