Getting Started with YAML: A Quick Guide

Amitt AshokAmitt Ashok
3 min read

What is YAML?

In today's automation world, understanding YAML is important because its syntax is used in many applications to automate various tasks.

Most databases are stored in the format of key-value pairs like first_name, middle_name, and last_name. Here, first, middle, and last names are keys, and their values are the answers.

Here, we will see how to write a YAML file for Docker Compose. My file name is docker-compose.yaml.

YAML files always start with the version, so I mention the first key as version with its value as '3'. Keep in mind that giving a space after the colon is important; otherwise, it will cause an error.

There are other languages like .XML and .JSON that provide key-value syntax, but YAML is easier to understand and write, and it takes up much less space than others.

Note that YAML files use the extensions .yaml or .yml.

Table of Contents:

  • Key-Value

  • Objects

  • Lists

  • List of Objects

  • Multiline String

  • Comments

Key-Value

Most databases store data in key-value pairs like first_name, middle_name, and last_name. Here, first, middle, and last names are keys, and their values are the corresponding names.

Let's see how to write a YAML file for Docker Compose. We'll name our file docker-compose.yaml.

YAML files always start with the version. So, we will set the first key as version with its value as '3'. Remember to add a space after the colon, or it will cause an error.

version: '3'
version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - mysql
    networks:
      - slam-network

Objects

A pair of key-value is called an object. In the below example, we can see that web is the service having build as object.

Always keep in mind the indentation; incorrect indentation can cause errors in the file.

  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - mysql
    networks:
      - slam-network

Lists

If your key has multiple values, you can add them as a list, as shown in the example below with ports, depends_on, and networks. To create a list, use a dash followed by a space and then the value.
Let's look at the code below to understand lists better. If we need to add a list of objects, after the colon, press Enter, then use a dash followed by the value.

  ports:
      - "5000:5000"
    depends_on:
      - mysql
    networks:
      - slam-network

List of Object

In YAML, when you have a list where each item is a mapping (object), it is often called a "list of objects" or a "sequence of mappings." Here, web and mysql are services, while ports, depends_on, and networks are lists. Additionally, environments, networks, and volumes are objects.

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - mysql
    networks:
      - slam-network

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: aWelcome@123
      MYSQL_DATABASE: user_info
      MYSQL_USER: root
      MYSQL_PASSWORD: aWelcome@123
    ports:
      - "3306:3306"
    networks:
      - slam-network
    volumes:
      - mysql_data:/var/lib/mysql
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql

Multiline string

When we need to add multiple string values, we can include them in a YAML file using the " | " operator. Anything that follows the | operator is treated as a string. This is useful when we want to create a configuration.

CONFIG: |
        # This is a multiline string.
        # You can use this to include configuration data or scripts.
        LINE1=value1
        LINE2=value2
        LINE3=value3

Comments

To add any comments in YAML file we can simply use # followed by comment.

version: '3'

  # This is the Docker Compose file for the Slambook project

To summarize this article, we learned what YAML is, the components of a YAML file, and how to write a YAML file for different applications.

Thank You for the day...

0
Subscribe to my newsletter

Read articles from Amitt Ashok directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amitt Ashok
Amitt Ashok

Hey Hi there, Amit here... I love to explore new things and learn new technology.