Day 18 - Docker Compose for DevOps

Day 18 - Docker Compose for DevOps

ยท

6 min read

๐Ÿ”— Docker-Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define all the services, networks, and volumes required for your application in a single, easy-to-read YAML file. With Docker Compose, you can then use a single command to start and manage all the containers defined in the configuration.

Here are some key concepts and commands related to Docker Compose:

  1. Docker Compose YAML File: You define your multi-container application and its configuration in a YAML file named docker-compose.yml (or a custom filename if specified with the -f flag). This file specifies services, networks, volumes, and their configurations.

  2. Services: Services are the core building blocks in a Docker Compose configuration. Each service represents a container and can define its image, environment variables, ports, volumes, and more. For example, you can have services for your application, database, cache, etc.

  3. Networks: Docker Compose can create custom networks for your containers to communicate with each other. You can specify the network mode for each service in the YAML file.

  4. Volumes: Volumes allow you to persist data across container restarts. You can define named volumes or use host-based volumes for storing data.

  5. docker-compose Commands:

    • docker-compose up: This command starts the services defined in your docker-compose.yml file. If the services or containers don't exist, Compose will create them.

    • docker-compose down: This command stops and removes the containers, networks, and volumes defined in your docker-compose.yml file.

    • docker-compose ps: Lists the running containers associated with your Compose project.

    • docker-compose logs: Displays the logs of the services.

    • docker-compose exec: Executes a command within a running container.

    • docker-compose build: Builds or rebuilds the images for services defined in your docker-compose.yml.

    • docker-compose restart: Restarts services.

  6. Environment Variables: You can set environment variables in the docker-compose.yml file for each service, allowing you to configure container behavior.

  7. Override Files: You can use override files (e.g., docker-compose.override.yml) to customize the configuration for different environments (e.g., development, production) without modifying the main docker-compose.yml.

Docker Compose simplifies the process of managing and orchestrating multiple containers in a development or testing environment. It is particularly useful for creating reproducible and portable development environments and simplifying complex deployments.

๐Ÿ”— YAML

YAML (short for "YAML Ain't Markup Language" or sometimes "Yet Another Markup Language") is a human-readable data serialization format. It is often used for configuration files and data exchange between languages with different data structures. YAML files use indentation to represent data hierarchies, making it easy for humans to read and write, while also being machine-readable.

Here are some key features of YAML:

  1. Human-Readable: YAML is designed to be easily readable by humans. It uses indentation and minimal punctuation, such as colons and dashes, to define data structures, which makes it visually clear.

  2. Whitespace Sensitive: YAML uses indentation (typically spaces or tabs) to indicate nesting and hierarchy, similar to how Python uses indentation. Proper indentation is crucial for the correct interpretation of YAML documents.

  3. Data Types: YAML supports various data types, including strings, numbers, booleans, arrays (lists), and dictionaries (maps). It allows you to represent complex data structures and their relationships.

  4. Comments: YAML allows for comments, which start with the # character. Comments are used for adding explanatory notes within the configuration file.

  5. Inclusion of Other Files: YAML supports the inclusion of other YAML files, which can be helpful for modularizing configurations.

Here's a basic example of YAML syntax:

# This is a YAML comment
key1: value1
key2: value2
nested_key:
  - item1
  - item2
nested_dict:
  sub_key1: sub_value1
  sub_key2: sub_value2

In this example:

  • key1 and key2 are key-value pairs.

  • nested_key is a list containing two items.

  • nested_dict is a dictionary containing key-value pairs.

YAML is commonly used for configuration files in software development, such as Docker Compose files, Kubernetes configuration files (YAML manifests), and configuration files for various programming languages and tools. It's also used in data serialization formats like JSON and XML but with a more human-friendly syntax.

Overall, YAML's simplicity and human-readable nature make it a popular choice for configuration files and data exchange formats in many applications.

๐Ÿ”— How to Install Docker and run as a non-root user

  1. To install the Docker

     sudo apt-get update
     sudo apt install docker.io
    
  2. Add the user to the group

     sudo userMod -aG docker $USER
    
  3. Reboot the system

     sudo reboot
    

๐Ÿ”— Task 1

Learn 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.

Step 1: - Create a directory

To start the project of Docker-Compose, first of all need to set up a directory for files. So create a directory Docker-Project and create a file docker-compose.yml

Step 2: - Write a docker-compose file.

version : "3.3"
 services:
   web:
     image: nginx:latest
     ports:
       - "80:80"
   db:
     image: mysql
     ports:
       - "3306:3306"
     environment:
       - "MYSQL_ROOT_PASSWORD=test@123"

The file is explained below for more clarity,

  1. version: "3.3": Specifies the version of the Docker Compose file format being used.

    Services: This section defines the different services (containers) in your application.

    • web: This service is defined with the nginx:latest image, which pulls the latest version of the Nginx image from Docker Hub. It maps port 80 on the host machine to port 80 in the container, allowing you to access the Nginx web server.

    • DB: This service uses the mysql image, which is the official MySQL image from Docker Hub. It maps port 3306 on the host to port 3306 in the container. Additionally, it sets the MYSQL_ROOT_PASSWORD environment variable to "test@123", which will be used as the root password for the MySQL database.

Step 3: - Run the docker-compose file

To run the compose file, first of all need to install docker-compose in the system.

sudo apt install docker-compose

Once the docker-compose is installed. Run the YAML file as below.

docker-compose up

It will pull the latest image which creates a container, which we can check on the web.

Once the container is created, we can check it on the web by accessing its port

This example demonstrates how you can use Docker Compose to define and manage a multi-container application environment.

๐Ÿ”— Task 2

  1. Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user

     docker pull <image-name>
    

    By using this command, we can pull the image from the docker hub, and run it.

     docker run -d <image-name>
    

  2. Inspect the container's running processes and exposed ports using the docker inspect command.

     docker inspect <container-id>
    

  3. Use the docker logs command to view the container's log output.

     docker logs <container-id>
    

  4. Use the docker stop and docker start commands to stop and start the container.

    To Stop container: -

     docker stop <container-id>
    

    To start the container: -

     docker start <container-id>
    

  5. Use the docker rm command to remove the container when you're done.

    To remove the container, first we need to Kill it, if it is running

     docker kill <container-id>
    

    Once it is killed, then it can be removed.

     docker rm <container-id>
    

    Thanks for reading !!

    ๐Ÿ™‚

ย