π YAML Ultimate Guide


A human-readable, whitespace-sensitive format for configuration & data serialization.
A serialization language is a format used to convert complex data structures (objects, arrays, etc.) into a flat format (text or binary).
π§ Core Rules
Rule | Example |
β Use spaces only (no tabs) | 2 or 4 spaces (consistent) |
β Case-sensitive keys | Name β name |
β Indentation defines structure | (NOT braces or brackets like JSON) |
β Strings can be unquoted | Use quotes if special chars exist |
β Never start number with 0 | Use 9 , not 09 (or quote it: "09" ) |
π Multiple documents β use --- separator | In same .yml file |
π§± Data Types
πΉ Key-Value Pair
app: Eventra
port: 3000
version: "1.0"
πΉ Nested Object
database:
host: localhost
port: 5432
credentials:
username: admin
password: "pa$$word"
πΉ List (Array)
Hyphen method
services:
- web
- api
- auth
Inline method
services: [web, api, auth]
π€ Scalars (Simple Data Types)
string: Hello World
number: 42
float: 99.99
boolean_true: true # also yes, on
boolean_false: false # also no, off
null_value: null # or just leave blank
π§Ύ Multiline Strings
Literal block (|
) β preserves line breaks
description: |
This is line 1.
This is line 2.
Folded block (>
) β folds into one line
note: >
This is line 1
and line 2 will join.
π Anchors & Aliases (Reusability)
default: &default-env
DEBUG: false
PORT: 8080
dev:
<<: *default-env
DEBUG: true
βοΈ Environment Variables
env: $NODE_ENV # shell style
config: "{{ value }}" # templating (e.g. Ansible/Helm)
ποΈ Comments
# This is a comment
π Multiple Documents in One File
---
app: one
port: 3000
---
app: two
port: 4000
π YAML vs JSON vs XML (Mini-Comparison)
Feature | YAML | JSON | XML |
Human-readable | β Best | Medium | β Verbose |
Syntax | Indentation | Braces | Tags |
Comments | β Yes | β No | β Yes |
Used in | Docker, K8s | APIs | Legacy systems |
Multidoc | β
Yes (--- ) | β No | β No |
π³ Docker Compose YAML (Example)
version: "3.8"
services:
app:
image: node:18
ports:
- "3000:3000"
environment:
- NODE_ENV=production
volumes:
- ./app:/usr/src/app
βΈοΈ Kubernetes Pod YAML (Example)
apiVersion: v1
kind: Pod
metadata:
name: eventra-app
spec:
containers:
- name: backend
image: node:18
ports:
- containerPort: 3000
π€ GitHub Actions YAML (CI/CD Example)
name: Build & Deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install deps
run: npm install
- name: Build
run: npm run build
π₯ Pro Tips
Use
.yml
or.yaml
(both valid)Keep consistent 2-space or 4-space indentation
Use double quotes when using special characters
Use
---
to split multiple componentsAnchor (
&
) and alias (*
) for DRY configs
β When to Use YAML?
Tool | Uses YAML? | Purpose |
Docker Compose | β Yes | Define services, volumes, env, ports |
Kubernetes | β Yes | Pods, Services, Deployments |
Ansible | β Yes | Automation scripts and configs |
GitHub Actions | β Yes | CI/CD pipelines |
Helm Charts | β Yes | Templated Kubernetes configurations |
Subscribe to my newsletter
Read articles from Abheeshta P directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abheeshta P
Abheeshta P
I am a Full-stack dev turning ideas into sleek, functional experiences π. I am passionate about AI, intuitive UI/UX, and crafting user-friendly platforms . I am always curious β from building websites to diving into machine learning and under the hood workings β¨. Next.js, Node.js, MongoDB, and Tailwind are my daily tools. I am here to share dev experiments, lessons learned, and the occasional late-night code breakthroughs. Always evolving, always building.