📂Day 18 - Docker Compose for DevOps Engineers

📂Day 18 - Docker Compose for DevOps Engineers

💻Docker Compose

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, you define a multi-container application in a single file, then spin up your application with a single command. This file, usually named docker-compose.yml, describes the services, networks, and volumes required by your application.

What is YAML?

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents. YAML's human-readable and expressive nature makes it a popular choice for configuration files, especially in the DevOps and containerization space. YAML files use a .yml or .yaml extension.

💻Task-1: Employing Docker Compose to Establish the Environment

Setting up the environment: Create a docker-compose.yml file in your directory.

Configuring services: Define the services in docker-compose.yml file.

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    environment:
      - NGINX_PORT=80
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
  api:
    image: my-api:latest
    ports:
      - "5000:5000"
    environment:
      - API_KEY=abc123
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=example

You can start this application by running docker-compose up in the same directory as the docker-compose.yml file.

💻Task-2:

◼️Pull the Docker Image:

docker pull nginx

◼️Create a new user and give it permission to run Docker commands:

sudo useradd -m myuser
sudo usermod -aG docker myuser
sudo reboot

◼️Run the container as the new user:

sudo su - myuser #login new user
docker run --name mynginx -d -p 8080:80 nginx #run the container

◼️Inspect the container's running processes and exposed ports:

docker inspect mynginx

◼️View the container's log output:

docker logs mynginx

◼️Stop and start the container:

docker stop mynginx
docker start mynginx

◼️Remove the container:

docker rm mynginx

Note: Make sure to replace myuser with the actual username you want to use, and adjust the Docker image and container names as needed.

How to run Docker commands without sudo?

To run Docker commands without using sudo, you need to add your user to the Docker group. Here are the steps:

◼️Add your user to the docker group:

sudo usermod -aG docker $USER

◼️Log out and log back in:

su - $USER

◼️Verify Docker command without sudo:

docker ps

◼️Reboot the machine (optional): While not strictly necessary, rebooting the machine can ensure that all changes are applied correctly.

Conclusion

To summarize, Docker is a powerful tool for creating, deploying, and managing applications in containers. By using Docker, you can easily package your applications and their dependencies into a standardized unit that can run on any environment. With Docker, you can quickly set up and tear down isolated environments, making it ideal for development, testing, and deployment workflows.

😊