Day 18 : Docker for DevOps Engineers

Day 18 : Docker for DevOps Engineers

Using Docker Compose

Β·

5 min read

Introduction to Docker Compose

Docker Compose is a powerful tool designed to simplify the management of multi-container applications. It enables users to define application services using a simple YAML configuration file, allowing for easy setup and teardown of complex environments with just a single command.

Understanding YAML

YAML (YAML Ain't Markup Language) serves as the language of choice for defining Docker Compose configurations. Renowned for its readability and simplicity, YAML files utilize a ".yml" or ".yaml" extension and are structured hierarchically, making them easy to understand and maintain.

Sample Docker Compose Configuration

Consider the following example of a Docker Compose YAML file:

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  database:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example

In this configuration:

  • Two services are defined: "web" and "database."

  • The "web" service utilizes the latest Nginx image and maps port 80 of the host to port 80 of the container.

  • The "database" service utilizes the latest MySQL image and sets the MySQL root password to "example" using environment variables.

Task - 1

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.

To effectively use the docker-compose.yml file, follow these steps to set up your environment, configure services, establish links between containers, and utilize environment variables:

  1. Environment Setup:

    • Start by creating a new directory for your project.

    • Inside the project directory, create a file named docker-compose.yml.

  2. Service Configuration:

    • Define the services required for your application within the docker-compose.yml file using the YAML syntax.

    • Each service should specify the Docker image to use, ports to expose, volumes to mount, environment variables, and any other necessary configurations.

    • For example, to set up a web server and a database service, define them as separate services within the file:

    version: '3.7'

    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - ./web:/usr/share/nginx/html
      database:
        image: mysql:latest
        environment:
          MYSQL_ROOT_PASSWORD: example
  1. Links Between Containers:

    • To establish communication between containers, specify dependencies and links between services.

    • For instance, if your web server relies on the database service, ensure that the web service depends on the database service and specify the necessary connection details:

    version: '3.7'

    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - ./web:/usr/share/nginx/html
        depends_on:
          - database
      database:
        image: mysql:latest
        environment:
          MYSQL_ROOT_PASSWORD: example
  1. Using Environment Variables:

    • Leverage environment variables to customize service configurations dynamically.

    • Define environment variables within the environment section of each service and reference them in your application as needed.

    version: '3.7'

    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
        environment:
          - ENVIRONMENT=production
      database:
        image: mysql:latest
        environment:
          MYSQL_ROOT_PASSWORD: example

By following these guidelines, you can effectively utilize the docker-compose.yml file to orchestrate your application's environment, configure services, establish links between containers, and manage environment variables dynamically. Experiment with different configurations for greater understanding.


Task - 2

In this task, we'll walk through the process of pulling a Docker image from a public repository, running it on your local machine, and managing the container.

Step 1: Pulling a Docker Image

Before we can run a Docker container, we need to pull the required Docker image from a public repository. We'll use the docker pull command for this purpose. For example, let's pull the official Nginx image:

docker pull nginx

Step 2: Running the Docker Container

Once the image is pulled, we can start a container using the docker run command. It's important to run the container as a non-root user to enhance security. Here's how you can grant permission to a user and reboot the instance:

sudo usermod -aG docker <username>
sudo reboot

Step 3: Inspecting Container Processes and Exposed Ports

After starting the container, we can inspect its running processes and exposed ports using the docker inspect command. This provides detailed information about the container. For example:

docker inspect <container_id>

Step 4: Viewing Container Log Output

To monitor the container's activity and view its log output, we can use the docker logs command. This helps in troubleshooting any issues with the container. For example:

docker logs <container_id>

Step 5: Stopping and Starting the Container

If needed, we can stop and start the container using the docker stop and docker start commands respectively. This allows us to control the execution of the container. For example:

docker stop <container_id>
docker start <container_id>

Step 6: Removing the Container

Once we're done with the container, we can remove it using the docker rm command. This cleans up the container and frees up system resources. For example:

docker rm <container_id>

Running Docker Commands Without sudo

To run Docker commands without using sudo, ensure that the user is added to the Docker group. This grants the necessary permissions to execute Docker commands without requiring superuser privileges. Don't forget to reboot the instance after making changes to the user's permissions for them to take effect.


βœ‰Endcard:

πŸŽ‰ Thank you for joining me on this insightful journey into the world of DevOps!

❀ If you found this blog helpful and informative, don't forget to give it a like!

πŸ”„ Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!

πŸ‘‰ Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!

Follow me on LinkedIn -->abdallah-qamarπŸ‘”

Stay tuned for Day 19...πŸ‘‹

Β