dockerdocker-composegitlab-cibuildx

docker buildx bake "failed to find target" error only in CI


I have a docker-compose.yml which contains a owmf-web-prod service configured for docker buildx bake:

version: '3.8'

...

services:

...

  owmf-web-prod:
    build:
      context: .
      dockerfile: Dockerfile
      target: prod
      x-bake:
        # https://docs.docker.com/build/customize/bake/compose-file/
        # https://docs.docker.com/engine/reference/commandline/buildx_bake/
        platforms:
          - "linux/amd64"
          - "linux/arm64"
        tags:
          - registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:${framework_image_tag}
          - registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:latest
        cache-from:
          - type=registry,ref=registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:buildx_bake_cache
        cache-to:
          - type=registry,ref=registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:buildx_bake_cache,mode=max
        pull: true
    image: registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:${framework_image_tag:-latest}
    env_file:
      - .env
    ports:
      - "${web_http_port:-80}:80"
      - "${web_https_port:-443}:443"
    networks:
      - owmf-internal
    logging:
      driver: json-file
      options:
        tag: "{{.ImageName}}|{{.Name}}|{{.ImageFullID}}|{{.FullID}}"
    healthcheck:
      test: ["CMD", "curl", "--fail", "http://localhost:80/health.php"]
      interval: 20s
      timeout: 1s
      retries: 5
    restart: unless-stopped
    volumes:
      - apache-sites-available:/etc/apache2/sites-available/
      - apache-sites-enabled:/etc/apache2/sites-enabled/
      - letsencrypt-certs:/etc/letsencrypt/
    profiles:
      - prod

On my machine

On my local machine if I run docker buildx bake owmf-web-prod --print it correctly outputs the JSON for my service:

{
  "group": {
    "default": {
      "targets": [
        "owmf-web-prod"
      ]
    }
  },
  "target": {
    "owmf-web-prod": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": [
        "registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:latest"
      ],
      "cache-from": [
        "type=registry,ref=registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:buildx_bake_cache"
      ],
      "cache-to": [
        "type=registry,ref=registry.gitlab.com/openetymologymap/osm-wikidata-map-framework:buildx_bake_cache,mode=max"
      ],
      "target": "prod",
      "platforms": [
        "linux/amd64",
        "linux/arm64"
      ],
      "pull": true
    }
  }
}

In CI

I have configured Gitlab CI to run:

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes # https://forum.gitlab.com/t/build-multi-arch-docker-image/23569/4
docker buildx create --use
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
docker buildx bake owmf-web-prod --print
docker buildx bake owmf-web-prod --pull --push

When the script reaches docker buildx bake owmf-web-prod --print it exits with an error:

ERROR: failed to find target owmf-web-prod

What I tried

I have recently renamed that service from oem-web-prod to owmf-web-prod (old docker-compose.yml, old gitlab-ci.yml). Before this change, it worked correctly (example output). I have double checked whether I did forget some old reference or made some typo but I didn't find anything wrong.

I verified that the command is correctly executed in the directory where the docker-compose.yml is located.

I tried to explicitly specify to use docker-compose.yml as source file but it didn't change anything.

What could be causing this problem? How can I fix it?


Solution

  • Turns out I was doing nothing wrong, a bug had been introduced in buildx v0.11.0 which pervented services with a profile from being discovered. The issue is tracked here. Meanwhile, I fixed the error by adding COMPOSE_PROFILES=* to the env vars, as suggested in the issue