Docker for DevOps Engineers Part-2

Docker Compose is a tool to manage multi-container Docker applications. It uses a YAML file (docker-compose.yml) to define services, networks, and volumes. You can start, stop, and manage multiple containers with a single command.

Key Commands:

  • Start services: docker-compose up

  • Stop services: docker-compose down

  • Check logs: docker-compose logs

  • List services: docker-compose ps

Example:

version: '3.8'
services:
  app:
    image: my-app:latest
    ports:
      - "5000:5000"
  db:
    image: postgres:13
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

Use Cases:

  • Simplifies development with multiple containers (e.g., app + database).

  • Automates testing and deployments.

  • Ideal for microservices.

What is YAML?

YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format often used for configuration files and data exchange between programming languages. It is simple, easy to read, and focuses on being intuitive for humans while being structured enough for machines to parse.


Key Features of YAML:

  1. Human-Friendly: Easy to read and write due to its plain-text format.

  2. Hierarchy: Represents data in a nested structure using indentation.

  3. Lightweight: Avoids complex syntax, relying on indentation and simple characters.

  4. Widely Used: Commonly used in tools like Docker Compose, Kubernetes, and Ansible.


YAML Syntax Basics:

  1. Key-Value Pairs:

     name: Vanshika
     age: 20
    
  2. Lists:

     skills:
       - Python
       - Java
       - Docker
    
  3. Dictionaries (Nested Data):

     address:
       city: Noida
       state: Uttar Pradesh
    
  4. Combining Lists and Dictionaries:

     students:
       - name: Rahul
         age: 21
       - name: Priya
         age: 22
    
  5. Comments: Use # for comments.

     # This is a comment
     version: '3.8'  # Docker Compose version
    

Key Advantages:

  • Simplicity: Easier to read compared to formats like JSON or XML.

  • Flexible: Works well for structured data, including lists, key-value pairs, and hierarchies.

  • Portable: Used in many tools and programming frameworks.


Use Cases:

  • Configuration Files: Tools like Docker Compose, Kubernetes, and CI/CD pipelines use YAML for defining configurations.

  • Data Serialization: Exchanging structured data between systems.

Task-1

How to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

A sample docker-compose.yml file to help you learn how to set up the environment, configure services, link containers, and use environment variables:

version: '3.8'  # Specify the version of Docker Compose

services:
  app:  # Define the application service
    image: my-app:latest  # Use a pre-built image
    build:  # Optionally build from a Dockerfile
      context: ./app  # Directory containing the Dockerfile
    ports:
      - "5000:5000"  # Map host port 5000 to container port 5000
    environment:  # Pass environment variables
      - APP_ENV=development
      - DEBUG=true
    depends_on:  # Link with other containers
      - db  # The app service depends on the db service

  db:  # Define the database service
    image: postgres:13  # Use the official PostgreSQL image
    ports:
      - "5432:5432"  # Map host port 5432 to container port 5432
    environment:  # Pass environment variables for database configuration
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydatabase
    volumes:
      - db_data:/var/lib/postgresql/data  # Persist database data

volumes:  # Define a volume for persistent data
  db_data:

Breakdown:

  1. Version: Specifies the Docker Compose file version (e.g., 3.8 is commonly used).

  2. Services:

    • Each service (e.g., app, db) represents a container.

    • You can specify the image, ports, dependencies, and environment variables for each service.

  3. Environment Variables:

    • Passed directly under the environment section or from an external file.
  4. Volumes:

    • Used for persistent storage (e.g., to keep database data even after the container stops).
  5. Links/Dependencies:

    • The depends_on key ensures that one container starts only after another is running.

Commands to Run:

  1. Start Services:

     docker-compose up -d
    
  2. Stop Services:

     docker-compose down
    

This file sets up a simple application with a PostgreSQL database, demonstrates the use of environment variables, and links containers together.

Task-2

  • Pull a Docker image from a public repository and run it locally as a non-root user (grant permissions to the user and reboot). Inspect the container's processes and ports with docker inspection. View logs using docker logs. Stop and start the container with docker stop and docker start. Remove the container with docker rm when finished.

1. Pull a Pre-existing Docker Image

  • Use the docker pull command to pull an image from Docker Hub. For example, to pull the Nginx web server image:

      docker pull nginx
    

2. Run the Container as a Non-Root User

  • First, add your user to the docker group to allow running Docker commands without root privileges:

      sudo usermod -aG docker $USER
    
  • Reboot your system for the changes to take effect:

      sudo reboot
    
  • After reboot, verify Docker commands work without sudo:

      docker ps
    
  • Run the container:

      docker run -d --name my-nginx -p 8080:80 nginx
    

    This runs the Nginx container in detached mode and maps port 8080 on your machine to port 80 in the container.


3. Inspect the Container's Processes and Ports

  • Use docker inspect to get detailed information about the container:

      docker inspect my-nginx
    
  • You can filter specific details, such as the exposed ports:

      docker inspect -f '{{json .NetworkSettings.Ports}}' my-nginx
    

4. View Container Logs

  • Use the docker logs command to view the container's log output:

      docker logs my-nginx
    

5. Stop and Start the Container

  • Stop the container:

      docker stop my-nginx
    
  • Start the container again:

      docker start my-nginx
    

6. Remove the Container

  • Stop the container (if it’s running):

      docker stop my-nginx
    
  • Remove the container:

      docker rm my-nginx
    

How to run Docker commands without sudo?

To run Docker commands without needing to use sudo, follow these steps:


1. Add Your User to the Docker Group

The Docker daemon runs as the root user, but by adding your user to the docker group, you can avoid using sudo for Docker commands.

Command:

sudo usermod -aG docker $USER
  • usermod: Modify a user account.

  • -aG: Add the user to a group (in this case, docker).

  • $USER: Refers to the currently logged-in user.


2. Reboot the System

For the changes to take effect, you need to log out and back in or reboot your system.

Command:

sudo reboot

3. Verify the Change

After rebooting, check if you can run Docker commands without sudo.

Command:

docker ps

If it works without showing a permission error, the setup is successful.


4. Troubleshooting (If It Doesn't Work)

  1. Check If the docker Group Exists:

     cat /etc/group | grep docker
    

    If it doesn't exist, create the docker group:

     sudo groupadd docker
    
  2. Re-add Your User to the Group:

     sudo usermod -aG docker $USER
    
  3. Restart the Docker Service:

     sudo systemctl restart docker