Docking Your Workflow: A Hands-On Guide to Docker Compose Installation and Examples

Docking Your Workflow: A Hands-On Guide to Docker Compose Installation and Examples

What is docker-compose?

It is a tool which is used to create and start Docker application by using a single command.

It simplifies the deployment of complex applications by defining their architecture in a human-readable format.

This configuration file, typically written in YAML, outlines the services, networks, and volumes needed to run your application.

Docker Compose not only simplifies the management of containers but also encourages collaboration among developers and teams

Users can activate all the services (containers) using a single command.

Docker compose features

  1. Purpose:Orchesrating multi-container application
  2. Configuration:YAML base configuration
  3. Scale:Multiple containers
  4. Networking:Built-in networking and service discovery
  5. Dependancy:Images and compose configuration
  6. Ecosystem:Docker Hub and private registries

How to Install Docker Compose

Prerequisites:

Before installing Docker Compose, ensure that you have Docker installed on your system.


Installation Steps:

Check the Current Release:

Visit the Docker Compose GitHub release page to find the latest stable release. Identify the version number you want to install.


Download the Docker Compose Binary:

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose        
Article content

Apply Executable Permissions:

Make the downloaded binary executable:

sudo chmod +x /usr/local/bin/docker-compose        
Article content

Verify Installation:

Confirm the successful installation by checking the version:

docker-compose –version        
Article content

Docker Compose Commands:

  1. docker-compose build =Look for all services containing the build
  2. docker-compose run=Run a one-time command against a service
  3. docker-compose up=Command used to start all the services of the container.
  4. docker Compose down=This syntax is used to stop all the services that were started.
  5. docker-compose -f =Specify the location of a docker-compose configuration file by adding the -f flag
  6. docker-compose start=Start existing containers for a service
  7. docker_compose_v=To check the version of docker-compose, we use this command.
  8. docker ps=This command is used to bring out the whole process of Docker.
  9. docker Compose.yml=This command is used to configure application services using YAML files.
  10. docker-compose up -d=Used to run a Docker-Compose file.
  11. docker Compose up -d -scale=Used to scale the service of the container.
  12. pip install -U Docker-compose= Command used to install Docker Compose using pip.


Compose step process

 1 Create a Directory

  1. Creating the Dockerfile for the app environment
  2. defining the services in docker-compose.yml
  3. starting the application


Running Application using Docker Compose

Example:

Follow the following example


1) Create a Directory

mkdir docker-compose-praman        
Article content

2)Go to the directory

cd docker-composer-praman        
Article content

3) Define docker-compose.yml file

sudo vi docker-compose.yml        
Article content

let’s dismantle the above code and understand it by piece:


  • version: It refers to the docker-compose version (Latest 3)
  • services: It defines the services that we need to run
  • app: It is a custom name for one of your containers
  • image: The image which we have to pull. Here we are using node:latest and Mongo
  • container_name:  It is the name for each container
  • restart: starts/restarts a service container
  • port: It defines the custom port to run the container
  • working_dir: It is the current working directory for the service container
  • environment: It defines the environment variables, such as DB credentials, and so on
  • command: It is the command to run the service


4)How to run the multi-container:

We need to build our multi-container using docker build.

docker-compose build          

(Command to build the docker-compose.yml)

Article content
docker-compose up          

(Command to run multiple containers using docker-compose)

docker-compose up -d         

(Command to run multiple containers using docker-compose in detached mode)

Article content
docker-compose ps          

(Command to list the running container services)

Article content

Sample output for running MongoDB service using docker:

Article content

Docker Compose Disadvantages:

Below are the drawbacks of Docker-Compose.

1.You have to spend more time and effort, installing Docker on your server.

2.Docker-Compose requires manual updates and fails when it comes to rebooting.

3.You have to spend more time and effort, installing Docker on your server.

4. However, docker-compose is already there in your system if you have installed Docker.


Docker Compose Use Cases:

Automated testing environments-

Compose supports automated testing, which is an essential part of CI/CD as it can easily create and destroy the required testing environment

Single host deployments-

In Docker Compose, containers are designed to run on a single host as they have traditionally been focused on development and testing workflows.

Development Environments-

Compose is a fast and simple way of starting projects as it can quickly spin up new isolated development environments.


High productivity -

Docker-Compose increases productivity and reduces the time taken for each task


Security -

All the containers are isolated from each other, reducing the threat landscape


Configuration-

Docker-Compose files are written in YAML scripts hence, provide easy and quick configuration.



Credits - Sachin Auti (DevOps)

 

MetricsViews Pvt. Ltd.

MetricsViews specializes in building a solid DevOps strategy with cloud-native including AWS, GCP, Azure, Salesforce, and many more.  We excel in microservice adoption, CI/CD, Orchestration, and Provisioning of Infrastructure - with Smart DevOps tools like Terraform, and CloudFormation on the cloud.

www.metricsviews.com

To view or add a comment, sign in

More articles by MetricsViews Private Limited

Others also viewed

Explore content categories