How do I specify an empty list of ports in docker-compose.yml?

Viewed 1656

Is there a way to specify an empty array in docker-compose.yml?

I tried ports: [] to no avail.

A JSON empty array literal should be valid YAML syntax, but when I use this, I get the following error from Docker:

services.myapp_migrations.ports must be a list

For more context:

I have a python application with the following docker-compose.yml:

version: "3.9"
services:

  myapp: &myapp
    build:
      context: .
    ports:
      - 8000:8000
    environment:
      - MYAPP_DB_HOST=postgres
    volumes:
      - ./.env:/app/.env
    working_dir: /app
    depends_on:
      - postgres

  myapp_migrations:
    <<: *myapp
    command: ["migrate"]
    restart: on-failure
    ports: []

  postgres:
    image: postgres:12-alpine
    environment:
      - POSTGRES_PASSWORD=${MYAPP_DB_PASSWORD}
      - POSTGRES_USER=${MYAPP_DB_USER}
      - POSTGRES_DB=${MYAPP_DB_NAME}

As you can see, I'm trying to re-use the myapp definition with a YAML anchor, but I need to override the ports definition in myapp_migrations.

(I know I could use extension fields for this, but I'm trying to avoid that.)

1 Answers

I agree with @flyx this is probably a temporary bug.

You might be able to work around this by using docker-compose in place of docker compose. Because they are separate binaries, they are updated independently.

For example, given your yaml above:

docker compose (not working)
test % docker compose config
services.myapp_migrations.ports must be a list
docker-compose (working)
test % docker-compose config
WARNING: The MYAPP_DB_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The MYAPP_DB_USER variable is not set. Defaulting to a blank string.
WARNING: The MYAPP_DB_NAME variable is not set. Defaulting to a blank string.
services:
  myapp:
    build:
      context: /Users/jvanus/test
    depends_on:
      postgres:
        condition: service_started
    environment:
      MYAPP_DB_HOST: postgres
    ports:
    - published: 8000
      target: 8000
    volumes:
    - /Users/jvanus/test/.env:/app/.env:rw
    working_dir: /app
  myapp_migrations:
    build:
      context: /Users/jvanus/test
    command:
    - migrate
    depends_on:
      postgres:
        condition: service_started
    environment:
      MYAPP_DB_HOST: postgres
    ports: []
    restart: on-failure
    volumes:
    - /Users/jvanus/test/.env:/app/.env:rw
    working_dir: /app
  postgres:
    environment:
      POSTGRES_DB: ''
      POSTGRES_PASSWORD: ''
      POSTGRES_USER: ''
    image: postgres:12-alpine
version: '3.9'
Related