DEV Community

On-cloud7
On-cloud7

Posted on

Day-18:Docker Compose for DevOps Engineers

Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for the application, making it easy to spin up and manage complex container-based environments.

For example:

If you have an application that requires an NGINX server and a Redis database, you can create a Docker Compose file that can run both containers as a service without the need to start each one separately.

Working of Docker Compose
Using Docker-Compose is essentially a three-step process:

Define your app’s environment with a Dockerfile so it can be reproduced anywhere.

Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using Compose standalone(docker-compose binary).

Key features of Docker Compose
Have multiple isolated environments on a single host
Compose uses a project name to isolate environments from each other. We can make use of this project name in several different contexts.

The default project name is the basename of the project directory. You can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.

The default project directory is the base directory of the Compose file. A custom value for it can be defined with the --project-directory command line option.

Preserves volume data when containers are created
Compose preserves all volumes used by the services. When docker compose up runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost.

Only recreate containers that have changed
Compose caches the configuration used to create a container. When you restart a service that has not changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to your environment very quickly.

Supports variables and moving a composition between environments
Docker Compose supports the use of environment variables to configure your containers at runtime. You can specify environment variables directly in the docker-compose.yml file or use an external .env file to manage them.

This makes it easy to configure your containers for different environments without modifying the underlying configuration file. Additionally, Docker Compose provides support for managing sensitive data, such as passwords or API keys, using Docker secrets.

Common use cases of Docker Compose
Docker Compose is widely used for various use cases, especially in scenarios where you need to manage multi-container applications and their dependencies. Here are some common use cases where Docker Compose can be beneficial:

Development environments
When you’re developing software, the ability to run an application in an isolated environment and interact with it is crucial. The Compose command line tool can be used to create the environment and interact with it.

The Compose file provides a way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command (docker compose up).

Automated testing environments
An important part of any Continuous Deployment or Continuous Integration process is the automated test suite. Automated end-to-end testing requires an environment in which to run tests.

Compose provides a convenient way to create and destroy isolated testing environments for your test suite.

By defining the full environment in a Compose file, you can create and destroy these environments in just a few commands.

Prototyping and Proof of Concepts
Docker Compose enables rapid prototyping and proof of concepts by allowing one to define and manage the required services in a single configuration file.

It helps in quickly spinning up complex environments with multiple containers, allowing developers and teams to experiment, validate ideas, and iterate faster.

Basic Commands in Docker Compose

Command Explanation

Docker Compose up Start all services

Docker Compose down Stop all services

pip install -U Docker-compose Install Docker Compose using pip

Docker-compose-v Check the version of Docker Compose

Docker-compose up -d Run Docker Compose file

Docker ps List the entire process

Docker Compose up -d -scale Scale a service

Docker Compose.yml Use YAML files to configure application service

YAML

YAML stands for YAML Ain't Markup Language

YAML is a human-readable data serialization format used to represent structured data in a simple and easily understandable way. It can be understood as a way to write down information in a format that both humans and computers can read and understand.

YAML is often used for configuration files, data exchange between systems, and defining complex structures. It's commonly used in various programming languages and tools, including Docker Compose.

YAML files use a .yml or .yaml extension.

The syntax of the YAML file is:

keyword: argument
Enter fullscreen mode Exit fullscreen mode

Example of a YAML file:

name: Priyanka
age: 21
email: priyanka@gmail.com
Enter fullscreen mode Exit fullscreen mode

Task-1:- Running Multiple Containers using Docker Compose

Step 1:- First we need to clone the docker image from the docker hub by using the below command

git clone https:https://github.com/MattsManoj/react_django_demo_app.git
Enter fullscreen mode Exit fullscreen mode

Step 2:- Now we need to use docker-compose in the Linux machine, By the below command

sudo apt-get install docker-compose -y
Enter fullscreen mode Exit fullscreen mode

Step 3:- Using the Vim editor we need to create a docker-compose file

version: '3.9'

services:
  web:
    image: mattsmanoj/react_django_app:latest
    ports:
      - "8001:8001"
Enter fullscreen mode Exit fullscreen mode

Step 4:- Now we need to start the container using the below command

sudo docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Step 5:- Now we need to search in the web browser along with the ipv4 address and port number

Step 6:- If we need to down or close the application then need to use the below command

sudo docker-compose down
Enter fullscreen mode Exit fullscreen mode

Top comments (0)