Day 18 - Exploring Docker Compose and Docker Image Management (Dec 14, 2023)

Day 18 - Exploring Docker Compose and Docker Image Management (Dec 14, 2023)

Understanding Docker Compose

Docker Compose Overview

Docker Compose is a powerful tool designed to define and share multi-container applications. Utilizing a YAML file, developers can configure services, establish links between containers, and seamlessly manage the entire environment with a single command.

What is YAML?

YAML (YAML Ain't Markup Language) is a data serialization language commonly used for configuration files. Its human-readable syntax makes it an excellent choice for defining complex structures. YAML files use a .yml or .yaml extension, and they focus on data representation rather than document markup.

Task-1: Docker Compose Configuration

To set up the environment and configure services, a docker-compose.yml file is created. This YAML file defines the services and links between different containers, allowing for efficient management of the entire application stack.

Sample docker-compose.yaml file:

version: '3'
services:
  web:
    image: nginx:alpine
  database:
    image: postgres:alpine

Now, let's explore using environment variables in the docker-compose.yml file to enhance flexibility and customization.


Task-2: Managing Docker Images and Containers

Running a Pre-existing Docker Image

Pulling a pre-existing Docker image from a public repository like Docker Hub and running it locally is a common DevOps task. Ensure to run the container as a non-root user for security purposes.

# Pulling and running the Docker image as a non-root user
docker run --user <username> <image_name>

Inspecting Container Details

Use the docker inspect command to gather detailed information about the container's running processes and exposed ports.

docker inspect <container_id>

Viewing Container Logs

The docker logs command helps monitor the container's log output.

docker logs <container_id>

Stopping, Starting, and Removing Containers

Manage container lifecycle with these commands:

docker stop <container_id>
docker start <container_id>
docker rm <container_id>

Running Docker Commands Without sudo

To execute Docker commands without sudo, follow these steps:

  1. Make sure Docker is installed and your system is updated.

  2. Add your user to the docker group:

  3.    sudo usermod -aG docker $USER
    

  4. Reboot your machine.