YAML Basics

YAML stands for YAML Ain't Markup Language.

It is mainly used for:

  • Configuration files
  • DevOps tools
  • APIs
  • Docker
  • Kubernetes
  • CI/CD pipelines

1. YAML Syntax Basics

Rules

  • Uses indentation (spaces)
  • No tabs
  • Usually .yaml or .yml
  • Key-value format

Common Data Types

TypeExample
Stringname: Akkal
Numberage: 20
BooleanisAdmin: true
Array- JavaScript
Objectuser:
Nullvalue: null

2. Key-Value Pair

first.yaml
name: Akkal
age: 22
city: Kathmandu

Meaning

  • name → key
  • Akkal → value
Equivalent JSON:
{
  "name": "Akkal",
  "age": 22,
  "city": "Kathmandu"
}

3. Strings

string.yml
message: Hello World
language: "TypeScript"
framework: 'Express'

Quotes are optional unless special characters exist.

4. Numbers and Boolean

num_bool.yml
age: 22
price: 99.99
 
isAdmin: true
isLoggedIn: false

5. Arrays (Lists)

array.yml
# method - 1
numbers: [1, 2, 3, 4]
 
# method - 2
languages:
  - JavaScript
  - TypeScript
  - Python

6. Objects

object.yml
user:
  name: Akkal
  age: 22
  role: developer

7. Nested Objects

nested_obj.yml
user:
  name: Akkal
  age: 22
 
  address:
    country: Nepal
    city: Kathmandu
    zip: 44600
Structure:
user
 ├── name
 ├── age
 └── address
      ├── country
      ├── city
      └── zip
nested_nested_obj.yml
company:
  name: XYZ
 
  department:
    name: Engineering
 
    manager:
      name: Akkal
      experience: 5
Structure:
company
 └── name
 └── department
      └── name
      └── manager
           ├── name
           └── experience

8. Array of Objects

arr_of_obj.yml
users:
  - name: Akkal
    role: admin
 
  - name: Ram
    role: user

9. Comments

comment.yml
# This is comment
name: Akkal

10. Null Values

null.yml
middleName: null
nickname:
username: ~

11. Multiline Strings

Pipe | : preserve line breaks

multi_line_str.yml
message: |
  Hello
  This is YAML
  Learning basics
Output:

Hello This is YAML Learning basics


Greater Than > : single line

multi_line_str.yml
message: >
  Hello
  This becomes
  one line
Output:

Hello This becomes one line

12. Environment Example

env.yml
app:
  name: auth-service
  port: 5000
 
database:
  host: localhost
  port: 5432
  username: postgres
  password: secret

13. Anchors and Defaults

YAML supports reusable default blocks using anchors & and aliases *.

default_settings is reused in both environments.

anchor_default.yml
default: &default_settings
  timeout: 30
  retries: 3
 
development:
  <<: *default_settings
  host: localhost
 
production:
  <<: *default_settings
  host: prod.server.com

14. Explicit Data Types

value: !!str 123
 
age: !!int "22"
 
isAdmin: !!bool "true"
 
data: !!null ""

15. Docker Compose

docker-compose.yml
version: "3"
 
services:
  app:
    image: node:20
 
    container_name: my-node-app
 
    ports:
      - "3000:3000"
 
    volumes:
      - .:/app
 
    working_dir: /app
 
    comm and: npm start
docker-compose2.yml
version: "3"
 
services:
  app:
    image: node:20
 
    container_name: backend-app
 
    ports:
      - "5000:5000"
 
    volumes:
      - .:/app
 
    working_dir: /app
 
    command: npm run dev
 
    depends_on:
      - mongo
 
  mongo:
    image: mongo
 
    container_name: mongodb
 
    ports:
      - "27017:27017"
 
    volumes:
      - mongo-data:/data/db
 
volumes:
  mongo-data:
docker-compose3.yml
version: "3"
 
services:
  app:
    image: node:20
 
    ports:
      - "5000:5000"
 
    environment:
      DATABASE_URL: postgres://postgres:password@db:5432/mydb
 
    depends_on:
      - db
 
  db:
    image: postgres
 
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
 
    ports:
      - "5432:5432"