What is Docker Compose ?

Definition(Docker Compose)

Docker Compose will define multi-container applications via the YAML configuration file.
Docker Compose will simplify the control of your entire application stack and provide a streamlined and efficient development and deployment experience.
Compose work in all environments production and non-production and has commands to manage the whole lifecycle of your application such as –

  1. Start, stop, and rebuild services.
  2. View the status of running services.
  3. Stream the log output of running services.
  4. Run a one-off command on a service.

Why use Docker Compose ?

Docker Compose provide several benefits over docker cli commands that streamline the devlopement, deployement and management of containers.

Simplified Control – Docker Compose allows you to define and manage multi-container applications( Frontend, Backend, Databases, API & Authentication Services) in YAML . Hence Simplifying the the complex task of orchestrating and co-ordinating various services, making it easier to manage and replicate you application enviornment.

Better Collaboration – Docker Compose files can be shared with the application code as a configuration file among devlopers, operations teams, and other stakeholders. This helps in smoother workflows, faster issue resolution, and increassed efficiency.

Rapid Application Devlopement – Compose caches the configuration used to create a container. When you restart a service that hast not changed, compose re-uses the existing containers. Re-using containers means that you can make changes to your enviornment very quickly.

Portability Across enviornments – Compose supports variables in the Compose file. You can use these variables to customize you config for different enviorments or users.

Containerization without Docker Compose

In a real time there are different images are required to build overall an application such as (Backend,Frontend, etc). All these container needs to communicate to each other for retreival, persistance of data and any other functionalties.

Docker Network

For this we create docker network and run different image containers inside that network. This will enable communication between docker containers. Given all containers are running on the same host as network there is no need to expose any ports for communication

Once you run above commands with all the enviorment variables from docker CLI you can see mongo-express is started in you local machine on port 8081.

mongo-express
http://localhost:8081/

As you can above setup we have create a database MongoDB to access that database we have used Mongo-Express in a given network as Mongo-Network.
Now think how cumbersome and complex this process will be with 10 images and to start containers for them and add them under a network. That’s why we will use a cleaner, better and more organized approach of compose.

Docker-Compose.YAML

Compose file configures all docker applications, services, and volumes via compose specification.
Elements in Compose File –

  1. Version – Version property is defined by the compose specification for backward compatibility. It is optional.
  2. Services – Services is a top-level element as a map whose keys are string representations of service names, and whose values are service defnitions.
  3. Image – Image element attribute define the image to be downloaded from the registry.
  4. Ports – Ports element will provide list for ports to be mapped from container to localhost.
  5. Environment – Enviorment element list will provide the different enviorment variables required for starting a image container.
  6. Depends On – Depends on Element will define any dependency on other images. Ex – MongoDB is required for Mongo – Express.
sample docker compose yaml
Docker-Compose.yaml

Docker-Compose Up

Once we have defined the configuration for all of the containers. We can start all the containers at same time with help of Docker-Compose up command.
Firstly it will create a docker network and then start all given containers inside that network

docker-compose-up

Docker-Compose Down

Whenever we want to stop all the containers we will use docker-compose down command. By default it will stop all the containers and remove the network created by default.

docker-compose-down

Docker-Compose Start & Stop

As we know containers are ephemeral in nature whenever we up and down the docker compose no data will pe persisted.


Comments

2 responses to “What is Docker Compose ?”

  1. […] thedevopsguy.co.in in Build Tools, Blog Prev Post […]

Leave a Reply to Google Jib(Compiler for Containers) – The Devops Guy Cancel reply

Your email address will not be published. Required fields are marked *