Open In App

Installation of Docker Compose

Last Updated : 04 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker Compose is a powerful tool that helps in simplifying the orchestration of multi-container docker applications. In this we have to configure the application services inside the Yaml file and docker-compose will read the configuration data from that Yaml script.

Before Moving into the installation process, make sure that you have Docker already installed on your system. On activation of the docker service only, we can work on the docker-compose tool because docker-compose works seamlessly alongside docker. It provides an enhancement in the better functionality for managing multi-container setups.

The step-by-step guideline for Installation of the Docker-compose

Step 1: Download Docker Compose

To get started, download the Docker Compose binary from the official GitHub repository. You can use the following `curl` command to retrieve the latest version:

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

Try copying the link and run it, in your docker platform as shown in the below figure:

Docker-compose Installation

Step 2: Apply Executable Permissions

Make the downloaded binary executable using the following command:

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

Provide the execution permissions on running the above command as shown in the figure and for confirmation you can view the permissions of the file through running the command `ls -ld /usr/local/bin/docker-compose`.

Docker-compose permissions

Step 3 – Verify Installation

Ensure that Docker Compose is installed correctly by checking the docker-compose version:

docker-compose --version

Now try on running the above command, if it displays the version of docker-compose then installation has done successfully or else their is a issue in the installation. In the below figure you can see the successful installation result on running the above command. For the help of docker-compose command try on using `docker-compose –help` command through it you can know the commands with its usage.

docker-compose --help

Docker-compose-version

Step 4: Create a Docker Compose File

Docker-compose uses a YAML file to define and configure multi-container applications. Create a docker-compose.yml file in your working directory and define the services, networks, and volumes needed for your application.

Here I provided an example for the yaml file for multi-containers application with some optional adding functionalities , for practice you can try this , While writing the yaml code ensure that identation is properly aligned as it is necessary guideline. It will generate errors while docker-compose reading the lines if it is not properly identated.

version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./app:/usr/share/nginx/html
networks:
- mynetwork
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: examplepassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: myuser
MYSQL_PASSWORD: myuserpassword
networks:
- mynetwork
networks:
mynetwork:

As specified in the above yaml code , I have created a yaml file with name docker-compose.yml through vim and copied the above specified code in it. You can see that in the below figure with highlighted text with green color.

docker-compose_yml-code

Step 5: Run Your Docker Compose Application

Navigate to the directory containing your `docker-compose.yml` file and try on running the following command to start your application:

docker-compose up

On running the created docker-compose yaml code file with the above displayed command , it will launch the multi-docker containers automatically as per the specifications specified with the docker-compose.

Docker-compose-up

To run your application in the background, add the `-d` flag:

docker-compose up -d

Step 6: Stop and Remove Containers

To stop and remove the containers created by Docker Compose, use the following command:

docker-compose down

This will stop and remove the containers, networks, and volumes defined in your `docker-compose.yml` file.

In the below figure you can see , once the `docker-compose down` command is entered it immediately removing the running containers to the stop mode. You can check it status through the `docker-compose ps` command. I hope through this practical example you understand the workflow of docker-compose , Try on doing it practical to get hands on experience on docker-compose software. Know more docker commands.

Docker-compsoe-down

People also Read

Article Name

Article link

Docker Installation on Linux Distributions

Read

Docker Installation on Windows

Read

Docker Installation on MacOS

Read

Conclusion

Installing and working with Docker-compose opens up a wide of possibilities for efficiently managing complex multi-container applications. By following above steps you can easily integrate docker compose with your docker workflow making well management of deployment applications.

FAQs on Docker Compose

1. How To Start Docker Compose Services?

The following command is useful to launch the services in your docker-compose.yml file:

docker-compose up

2. How To Run a Multiple Containers Of Service With Docker Compose ?

The following command helps to run a multiple containers of a service with docker-compose.

docker-compose up --scale service_name=N

3. What Are The Command Stops And Removes Docker Compose Containers?

The following command will stops and removes docker compose containers

docker-compose down

4. How To Change The Docker Compose Configuration Values ?

On using environment variables or a .env file we can override the configuration settings in docker-compose.

5. What Is Docker Compose And What Is Its Use ?

Docker Compose is a tool for defining and running multi-container Docker apps, simplifying deployment and ensuring consistency.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads