DevToolBoxFREE
BlogAdvertise

Validazione YAML Docker Compose: 10 errori di sintassi comuni e come risolverli

9 min di letturadi DevToolBox

I file YAML di Docker Compose sembrano semplici โ€” finchรฉ non si rompono. Uno spazio fuori posto, un'indentazione sbagliata o un carattere speciale non quotato genera errori criptici. Ecco i 10 errori piรน comuni e come correggerli.

Valida YAML con il nostro convertitore JSON-YAML โ†’

Errore 1: Tab invece di spazi

Messaggio di errore:

yaml: line 5: found a tab character where an indentation space is expected

Problema: YAML vieta rigorosamente i tab per l'indentazione.

Soluzione: Sostituire tutti i tab con spazi (2 per livello).

# BAD (tabs - invisible but breaks YAML)
services:
	web:
		image: nginx

# GOOD (2 spaces)
services:
  web:
    image: nginx

Suggerimento editor: Attivare "Render Whitespace" nell'editor.

Errore 2: Indentazione incoerente

Messaggio di errore:

yaml: line 8: mapping values are not allowed in this context

Problema: Mescolati livelli diversi (2 spazi qui, 4 lร ).

# BAD (inconsistent: 2 spaces then 4 spaces)
services:
  web:
      image: nginx     # 4 spaces - inconsistent!
      ports:
        - "80:80"

# GOOD (consistent 2 spaces)
services:
  web:
    image: nginx       # 2 spaces - consistent
    ports:
      - "80:80"

Errore 3: Caratteri speciali non quotati

Messaggio di errore:

yaml: line 6: did not find expected key

Problema: YAML interpreta due punti, hash e parentesi in modo speciale. Vanno quotati.

# BAD
services:
  web:
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/mydb  # colon in value!
      - APP_TITLE=My App: The Best                       # colon in value!

# GOOD (quote values with special characters)
services:
  web:
    environment:
      - "DATABASE_URL=postgres://user:pass@db:5432/mydb"
      - "APP_TITLE=My App: The Best"

Caratteri da quotare: : { } [ ] , & * # ? | - < > = ! % @

Errore 4: Formato mapping porte sbagliato

Messaggio di errore:

services.web.ports contains an invalid type, it should be a number, or an object

Problema: YAML puรฒ interpretare 80:80 come base-60 senza virgolette.

# RISKY (YAML may interpret as base-60)
services:
  web:
    ports:
      - 80:80        # Works but risky
      - 5432:5432    # May cause issues

# SAFE (always quote port mappings)
services:
  web:
    ports:
      - "80:80"
      - "5432:5432"
      - "127.0.0.1:3000:3000"

Errore 5: Chiave version mancante o errata

Messaggio di errore:

(root) Additional property version is not allowed

Problema: Docker Compose v2 non richiede piรน la chiave version.

# OUTDATED (Docker Compose v1 format)
version: "3.8"
services:
  web:
    image: nginx

# CURRENT (Docker Compose v2 - no version needed)
services:
  web:
    image: nginx

Con docker compose (v2): rimuovere version. Con docker-compose (v1): mantenerla.

Errore 6: Variabili ambiente mapping vs lista

Messaggio di errore:

services.web.environment must be a mapping or an array

Problema: Mescolata sintassi mapping e lista per le variabili d'ambiente.

# Format A: List (with dashes)
services:
  web:
    environment:
      - NODE_ENV=production
      - PORT=3000

# Format B: Mapping (key: value)
services:
  web:
    environment:
      NODE_ENV: production
      PORT: 3000

# BAD: Mixing both formats
services:
  web:
    environment:
      NODE_ENV: production
      - PORT=3000          # ERROR: mixing formats!

Errore 7: Valori booleani non quotati

Messaggio di errore: Nessun errore visibile, comportamento inatteso.

Problema: YAML interpreta yes, no, true, false, on, off come booleani.

# BAD (YAML converts to boolean true/false)
services:
  web:
    environment:
      FEATURE_FLAG: yes       # becomes boolean true, not string "yes"
      DEBUG: on               # becomes boolean true
      COUNTRY: NO             # becomes boolean false (Norway code!)

# GOOD (quote string values)
services:
  web:
    environment:
      FEATURE_FLAG: "yes"
      DEBUG: "on"
      COUNTRY: "NO"

Errore 8: Chiavi duplicate

Messaggio di errore: Spesso nessun errore โ€” il secondo valore sovrascrive il primo.

# BAD (duplicate 'ports' key - second one silently wins)
services:
  web:
    image: nginx
    ports:
      - "80:80"
    environment:
      - NODE_ENV=production
    ports:                    # DUPLICATE! This replaces the first ports
      - "443:443"

# GOOD (combine under single key)
services:
  web:
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    environment:
      - NODE_ENV=production

Errore 9: Percorsi volumi

Messaggio di errore:

services.web.volumes contains an invalid type

Problema: Percorsi Windows con backslash richiedono virgolette.

# BAD (Windows paths without quotes)
services:
  web:
    volumes:
      - C:\Users\me\app:/app     # Backslashes cause issues

# GOOD (use forward slashes or quotes)
services:
  web:
    volumes:
      - "./app:/app"
      - "/home/user/data:/data"
      - "C:/Users/me/app:/app"      # Forward slashes on Windows

Errore 10: Ancore YAML usate male

Messaggio di errore:

yaml: unknown anchor 'common'

Problema: Ancora (*nome) referenziata prima della definizione (&nome).

# BAD (reference before definition)
services:
  web:
    <<: *common          # ERROR: 'common' not defined yet

x-common: &common
  restart: always

# GOOD (define anchor first, then reference)
x-common: &common
  restart: always
  logging:
    driver: json-file

services:
  web:
    <<: *common
    image: nginx
  api:
    <<: *common
    image: node:20

Checklist validazione rapida

Prima di docker compose up, verifica:

  1. Esegui docker compose config per validare la sintassi
  2. Controlla tab: grep -P '\t' docker-compose.yml
  3. Quota tutti i mapping delle porte
  4. Nessuna chiave duplicata allo stesso livello
  5. Quota valori con caratteri speciali
  6. Indentazione coerente (2 spazi)

Valida YAML con il nostro convertitore JSON-YAML โ†’

Formatta output JSON di "docker compose config" โ†’

Domande frequenti

Come validare un file Docker Compose prima di eseguirlo?

Esegui "docker compose config" per validare e visualizzare la configurazione risolta.

Perchรฉ "service must be a mapping"?

La sezione services non รจ strutturata correttamente come mapping YAML. Cause comuni: indentazione mancante.

Tab o spazi nei file YAML Docker Compose?

Sempre spazi. YAML non consente tab per l'indentazione. Configura l'editor per inserire 2 spazi con Tab.

รˆ stato utile?

Stay Updated

Get weekly dev tips and new tool announcements.

No spam. Unsubscribe anytime.

Partner Picks

Sponsor this article

Place your product next to this developer topic with tracked clicks.

Ask about article sponsorship

This site uses cookies for analytics and to display ads. By continuing to browse, you agree. Privacy Policy