compose icon indicating copy to clipboard operation
compose copied to clipboard

[BUG] `service "test-abc" has neither an image nor a build context specified: invalid compose project`

Open stdedos opened this issue 2 years ago • 1 comments

Description

version: "3.9"

services:
  nginx:
    build:
      context: ../
      dockerfile: .gitlab/nginx/Dockerfile
    ports:
      - "80:80"
    cap_add:
      - NET_ADMIN

  abc:
    image: registry/abc:latest
    ports:
      - "127.0.0.1:1234:1234"

  moar_underscored_services:
    image: registry/moar_underscored_services:latest
    ports:
      - "127.0.0.1:9012:9012"

  def:
    image: registry/def:${DEF_TAG}
    ports:
      - "127.0.0.1:5678:5678"

  moar-dashed-services:
    image: registry/moar-dashed-services:latest
    ports:
      - "127.0.0.1:3456:3456"

  test-abc:
    image: ${ABC_IMAGE}
    depends_on:
      - abc
      - more_underscored_services
    entrypoint: ["npm", "run", "test"]

  test-def:
    image: ${DEF_IMAGE}
    depends_on:
      - more_underscored_services
      - def
      - moar_underscored_services
    entrypoint: ["npm", "run", "test"]

with this file, DEF_TAG=$DOCKER_TAG_NAME DEF_IMAGE=$REGISTRY:$CI_COMMIT_REF_SLUG docker-compose -f path/.to/docker-compose.yml run test-def happily compiles.

However, docker compose V2 (aliased via docker-compose) gives:

time="2023-10-12T12:29:46Z" level=warning msg="The \"ABC_TAG\" variable is not set. Defaulting to a blank ....
service "test-abc" has neither an image nor a build context specified: invalid compose project

In case it is not obvious, test-abc is never called in this invocation.

Is that expected?

Steps To Reproduce

No response

Compose Version

docker-cli-compose	2.17.3-r7	URL	Apache-2.0	v3.18	community	x86_64	Jake Buchholz Göktürk	2023-10-12 14:14:03

In Alpine



### Docker Environment

_No response_

### Anything else?

_No response_

stdedos avatar Oct 13 '23 08:10 stdedos

Right, Docker Compose v1 is more lazy validating your compose file, while compose v2 does a full model validation (handled by compose-go library) before running a command, even the latter doesn't actually require this resource. A possible workaround is to declare a default value for such variables: image: ${ABC_IMAGE:-none}

ndeloof avatar Oct 16 '23 12:10 ndeloof

Bypassing consistency check for unselected resources would have a significant impact on the compose-go parser codebase, with risk to introduce regressions. So I prefer we close this issue as "not planned" considering in most cases fixing the compose model for unused resources to have "reasonable defaults" is an acceptable workaround

ndeloof avatar Apr 19 '24 12:04 ndeloof

So I prefer we close this issue as "not planned"

Then please do so :pray: Don't close it as "completed"

stdedos avatar Apr 19 '24 12:04 stdedos