Introduction to Docker compose


Docker Compose is used to run multiple containers as a single service. It allows developers to write a YAML or yml configuration file for the application service which then can be started using a single command.

Docker can be used in staging , production , testing as well as CI workflow.
Compose is basically a three step-process:

1. Define your app's environment with a Dockerfile so it can be reproduced anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
3. Run docker-compose up and Compose starts runs your entire app.

Compose file can run in any machine that has docker with docker-compose installed in it.
Compose has commands for managing the whole lifecycle of your application:
==> Start, stop, and rebuild services
==> View the status of running services
==> Stream the log output of running services

Working Of Docker Compose in Linux

Prerequisite for the installation of docker-compose in ubuntu:

  • A non-root user with sudo privileges and if you don't want to use sudo command repeatedly then add yourself to docker group

⇒ sudo usermod -aG docker ${USER}
or
⇒ su - ${USER}

  • Docker should be installed and in running state

Step1: download the latest version of docker compose from the github

⇒ curl -L "ht​tps://github.com/docker/compose/releases/download/1.10.0-rc2/dockercompose -$(uname -s) -$(uname -m)" -o /home/demo/docker-compose

Check the latest release from here https://github.com/docker/compose/releases

Step2: Set the permission to make it executable

⇒ chmod +x /home/agatho/Desktop/docker-compose

Step3: verify the installation was successful or not by checking the version

⇒ docker-compose --version

Step4: Create a yml configuration file which contains rules declared by us to run multiple containers at once

⇒ nano docker-compose.yml

Code :

   version: 
   services:
      Testing:
          image: hello-world
       Web:
          image: nginx

In above yml file tags used are:

  • Version: tells us about the version of the compose file . If version is not mentioned then it is by default considered as "Version:1&quot
    https://docs.docker.com/compose/compose-file/compose-versioning/
  • Services: will contain all the containers which are included in the compose file and considered as parent tag
  • Testing: container name
  • Image: it specifies which image to use to create the container , when we will run the command docker-compose up it will look up for a local image by the name we specified example in the file we have used hello-world , nginx
  • web: container name

Step5: Check if there is any container in running state

⇒ docker ps

Step6: Now we will start the container

⇒ docker-compose up

Optional: Command used to check only the running containers:

⇒ docker ps

Step7: Now we will stop and remove the containers using the command

⇒ docker-compose down

Working of Docker-Compose in windows

Prerequisite for the installation of docker-compose in windows:

  • Docker Desktop should be installed and in running state

  • Docker-compose is inbuilt in Docker Desktop

Step1: Open Powershell or Command Prompt in "Run as Administrator" mode

Step2: verify the installation was successful or not by checking the version

⇒ docker --version

To check whether Docker desktop is working or not

⇒ docker-compose --version

To check whether docker-compose is working or not

Step3: Create a yml configuration file which contains rules declared by us to run multiple containers at once
Notepad++ can be used to create and edit Yaml files by selecting YAML from the list of languages present in language option of notepad++.

Step4: Check if there is any container in running state

⇒ docker ps

Step5: Now we will start the container

⇒ docker-compose up

Optional: Command used to check only the running containers:

⇒ docker ps

Step6: Now we will stop and remove the containers using the command

⇒ docker-compose down

With this article at OpenGenus, you must have a basic knowledge of Docker Compose. Enjoy.