How to solve Docker compose error ".labels array items must be unique"?

Emmanuel Gautier / January 21, 2024

1 min read

If you have the validating docker-compose.dev.yml: services.xxx.labels array items must be unique error showing up when you are trying to run your docker compose, you are at the right place.

Let's consider a scenario where you have separate Docker Compose files for production and development environments. The production compose file (docker-compose.prod.yml) might look like this:

docker-compose.prod.yml
version: '3'
services:
  web:
    image: production-image:latest
    ports:
      - "80:80"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefiknet"
      - "traefik.http.routers.yourservice.rule=Host(`service.example.com`)"
      - "traefik.http.routers.yourservice.service=yourservice"
      - "traefik.http.routers.yourservice.entrypoints=http"
      - "traefik.http.services.yourservice.loadbalancer.server.port=4455"
      - "traefik.http.services.yourservice.loadbalancer.passHostHeader=true"

And the development compose file (docker-compose.dev.yml) may look similar, with adjustments for the development environment:

docker-compose.dev.yml
version: '3'
services:
  web:
    image: development-image:latest
    ports:
      - "8080:80"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefiknet"
      - "traefik.http.routers.yourservice.rule=Host(`service.example.local`)"
      - "traefik.http.routers.yourservice.service=yourservice"
      - "traefik.http.routers.yourservice.entrypoints=http"
      - "traefik.http.services.yourservice.loadbalancer.server.port=4455"
      - "traefik.http.services.yourservice.loadbalancer.passHostHeader=true"

Unique Labels Requirement

The error "Docker Compose .labels array items must be unique" arises when Docker compose labels are duplicated between Docker Compose files. In our example, if both production and development files share the same labels, this error will occur.

Solution

Remove from development docker compose file any labels that are already present in the production Docker Compose file (docker-compose.prod.yml). Example:

docker-compose.dev.yml
version: '3'
services:
  web:
    image: development-image:latest
    ports:
      - "8080:80"
    labels:
      - "traefik.http.routers.yourservice.rule=Host(`service.example.local`)"

Consulting

If you're seeking solutions to a problem or need expert advice, I'm here to help! Don't hesitate to book a call with me for a consulting session. Let's discuss your situation and find the best solution together.

Share this post
Follow the RSS feed