#Day 18 - Docker Compose and YAML

Shefali MishraShefali Mishra
2 min read

Docker Compose Overview

Docker Compose is a powerful tool designed to simplify the deployment and management of multi-container applications. It enables developers to define the services, networks, and volumes in a single file, making it easy to spin up or tear down complex environments with a single command. In this article, we'll explore the fundamentals of Docker Compose and walk through practical tasks to enhance your understanding.

Understanding YAML

Before delving into Docker Compose, it's crucial to grasp the basics of YAML (Yet Another Markup Language). YAML is a data serialization language commonly used for configuration files. Its human-readable and easy-to-understand syntax makes it a popular choice for defining settings and structures in a variety of applications. YAML files typically use the .yml or .yaml extension.

For more in-depth information on YAML, check out the official documentation here.

Task-1: Understanding docker-compose.yml

Your first task is to familiarize yourself with the docker-compose.yml file. This file is where you define the services, networks, and other configurations for your multi-container application. To get started, create a simple docker-compose.yaml file.

version: '3' 
services: 
  web: 
   image: nginx:latest 
   ports: 
     - "8080:80" 
database: 
  image: postgres:latest 
  environment: 
   POSTGRES_PASSWORD: example

This example defines two services: a web service using the latest Nginx image and a database service using the latest PostgreSQL image.

Task-2: Working with Docker Compose

Now, let's pull a pre-existing Docker image from a public repository and run it on your local machine using Docker Compose. Follow these steps:

  1. Pull and Run the Image:docker-compose up -d

  2. Inspect Container Processes and Ports:docker inspect <container_name>

  3. View Container Logs:docker logs <container_name>

  4. Stop and Start the Container:docker stop <container_name> docker start <container_name>

  5. Remove the Container:docker rm <container_name>

Running Docker Commands Without Sudo

To run Docker commands without sudo, ensure that Docker is installed and your system is updated. Follow these steps:

  1. Add your user to the docker group:sudo usermod -a -G docker $USER

  2. Reboot your machine to apply the changes.

With these tasks, you've taken a significant step toward mastering Docker Compose and further enhanced your Docker skills. Keep exploring and experimenting with different configurations to deepen your understanding of container orchestration. Happy coding! ๐Ÿ˜ƒ

0
Subscribe to my newsletter

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

Written by

Shefali Mishra
Shefali Mishra