DevToolBoxFREE
BlogAdvertise

Validasi YAML Docker Compose: 10 Kesalahan Sintaks Umum dan Cara Memperbaikinya

9 menit bacaoleh DevToolBox

File YAML Docker Compose terlihat sederhana — sampai rusak. Satu spasi salah, indentasi keliru, atau karakter khusus tanpa tanda kutip menimbulkan error kriptik. Berikut 10 kesalahan paling umum dan cara memperbaikinya.

Validasi YAML dengan JSON-YAML Converter kami →

Error 1: Tab bukan Spasi

Pesan error:

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

Masalah: YAML melarang ketat karakter tab untuk indentasi.

Perbaikan: Ganti semua tab dengan spasi (2 spasi per level).

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

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

Tip editor: Aktifkan "Render Whitespace" di editor.

Error 2: Indentasi Tidak Konsisten

Pesan error:

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

Masalah: Campuran level indentasi (2 spasi di sini, 4 di sana).

# 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"

Error 3: Karakter Khusus Tanpa Kutip

Pesan error:

yaml: line 6: did not find expected key

Masalah: YAML menginterpretasi titik dua, hash, dan kurung secara khusus. Harus dikutip.

# 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"

Karakter yang perlu dikutip: : { } [ ] , & * # ? | - < > = ! % @

Error 4: Format Pemetaan Port Salah

Pesan error:

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

Masalah: YAML dapat menginterpretasi 80:80 tanpa kutip sebagai base-60.

# 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"

Error 5: Kunci Version Hilang atau Salah

Pesan error:

(root) Additional property version is not allowed

Masalah: Docker Compose v2 tidak lagi memerlukan kunci version.

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

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

Dengan docker compose (v2): hapus version. Dengan docker-compose (v1): pertahankan.

Error 6: Variabel Lingkungan Mapping vs Daftar

Pesan error:

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

Masalah: Pencampuran sintaks mapping dan daftar untuk variabel lingkungan.

# 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!

Error 7: Nilai Boolean Tanpa Kutip

Pesan error: Tidak ada error terlihat, perilaku tak terduga.

Masalah: YAML menginterpretasi yes, no, true, false, on, off sebagai boolean.

# 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"

Error 8: Kunci Duplikat

Pesan error: Sering tidak ada error — nilai kedua menimpa yang pertama.

# 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

Error 9: Masalah Path Volumes

Pesan error:

services.web.volumes contains an invalid type

Masalah: Path Windows dengan backslash perlu tanda kutip.

# 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

Error 10: Anchor YAML Salah Gunakan

Pesan error:

yaml: unknown anchor 'common'

Masalah: Referensi anchor (*nama) sebelum definisi (&nama).

# 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 Validasi Cepat

Sebelum docker compose up, verifikasi:

  1. Jalankan docker compose config untuk validasi sintaks
  2. Periksa tab: grep -P '\t' docker-compose.yml
  3. Kutip semua pemetaan port
  4. Tidak ada kunci duplikat di level sama
  5. Kutip nilai yang mengandung karakter khusus
  6. Indentasi konsisten (2 spasi)

Validasi YAML dengan JSON-YAML Converter kami →

Format output JSON dari "docker compose config" →

Pertanyaan Umum

Bagaimana memvalidasi file Docker Compose sebelum menjalankannya?

Jalankan "docker compose config" untuk memvalidasi dan menampilkan konfigurasi yang di-resolve.

Mengapa "service must be a mapping"?

Bagian services tidak terstruktur dengan benar sebagai mapping YAML. Penyebab umum: indentasi kurang.

Tab atau spasi di file YAML Docker Compose?

Selalu spasi. YAML tidak mengizinkan tab untuk indentasi. Konfigurasi editor untuk menyisipkan 2 spasi saat Tab.

Apakah ini membantu?

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