Day 18 - Understanding YAML and Docker Compose

Simplifying DevOps: Unraveling YAML and Mastering Docker Compose

ยท

3 min read

Day 18 - Understanding YAML and Docker Compose

Introduction

Welcome back to the 90 Days of DevOps Challenge! Today, we'll dive into two essential topics: YAML and Docker Compose. As always, I'll break down these concepts into easy-to-understand bullet points, so you can grasp them effortlessly.

1. What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format. In DevOps, it's commonly used for configuration files because of its simplicity.

Here's a basic YAML example:

# This is a comment
person:
  name: John Doe
  age: 30
  job: DevOps Engineer
  • YAML uses indentation to represent a hierarchy.

  • It's sensitive to whitespace, so be consistent.

  • You can comment using #.

2. What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It simplifies managing complex container setups.

Writing a docker-compose.yaml file is an essential skill for DevOps enthusiasts like you. This file is used to define and manage multi-container Docker applications.

  1. Start with the Version: Begin your docker-compose.yaml file by specifying the version. This version number indicates which syntax and features you can use. For example:

     version: '3'
    
  2. Define Your Services: Think of services as the containers you want to run. Each service is defined under the services section. Give your service a name and then specify its configuration:

     services:
       webapp:
         image: nginx:latest
    

    In this example, we named the service "webapp" and used the Nginx image.

  3. Ports and Volumes: If your container needs to expose ports or use volumes, you can define them like this:

     services:
       webapp:
         image: nginx:latest
         ports:
           - "80:80"
         volumes:
           - ./html:/usr/share/nginx/html
    

    Here, we're exposing port 80 on the host to port 80 in the container and mounting a local directory into the container.

  4. Environment Variables: You can set environment variables for your services using the environment key:

     services:
       webapp:
         image: nginx:latest
         environment:
           - MY_VARIABLE=my_value
    
  5. Networks: If you want your containers to communicate with each other, you can define a custom network:

     networks:
       mynetwork:
     services:
       webapp:
         image: nginx:latest
         networks:
           - mynetwork
    
  6. Putting It All Together: Your final docker-compose.yaml file may look something like this:

     version: '3'
     services:
       webapp:
         image: nginx:latest
         ports:
           - "80:80"
         volumes:
           - ./html:/usr/share/nginx/html
         environment:
           - MY_VARIABLE=my_value
         networks:
           - mynetwork
     networks:
       mynetwork:
    
  7. Running with Docker Compose: Save this file as docker-compose.yaml in your project directory. To start your services, open a terminal in that directory and run:

     docker-compose up
    

    To stop the services when you're done:

     docker-compose down
    

That's the simplest way to create a docker-compose.yaml file to manage your Docker containers. Feel free to expand and customize it for your specific needs as you continue to learn and grow in your DevOps journey!

4. Working with Docker Containers

i. Pull and Run an Image

docker pull nginx:latest
docker run --name mynginx -d -p 80:80 nginx:latest
  • docker pull fetches an image.

  • docker run starts a container.

ii. Run as a Non-Root User

usermod -aG docker your_username
# Reboot your machine

iii. Inspect Container

docker inspect mynginx

View detailed information about the container.

iv. View Logs

docker logs mynginx
  • Check container log output.

v. Stop and Start Container

docker stop mynginx
docker start mynginx
  • Use docker stop and docker start as needed.

vi. Remove Container

docker rm mynginx
  • Cleanup with docker rm when done.

That's it for today's journey into YAML and Docker Compose. We've explored YAML's simplicity for configuration and Docker Compose's power for orchestrating containers. Keep learning, and you'll become a top-notch DevOps engineer in no time!

Stay tuned for more demystified DevOps concepts in the coming days. Happy DevOps learning! โœจ๐Ÿณ๐Ÿš€

ย