buildx icon indicating copy to clipboard operation
buildx copied to clipboard

bake --push throws error when image field is not defined within docker-compose file

Open dudicoco opened this issue 4 years ago • 2 comments

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.

dudicoco avatar Aug 24 '21 10:08 dudicoco

Global flags like --push apply to all targets. If you want to build specific ones then docker buildx bake service2 servicefoo

tonistiigi avatar Aug 24 '21 23:08 tonistiigi

@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.

dudicoco avatar Aug 25 '21 07:08 dudicoco

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"
      ]
    }
  }
}

crazy-max avatar Feb 24 '23 10:02 crazy-max