Day 18 - Docker Compose

Day 18 - Docker Compose

What is Docker Compose and Why we need it?

Let's assume you are assigned on a project which requires at least 10 different services in a running state to run your project.

In such case, you have to pull all those images individually from Docker and start all of them in their containers. At some point, one process may depend on another to run. So, you have to order them.

It would be good if it's a one time process. But, not just once – everyday, every time you start working on your project and you have to start all these services which will be a tedious task.

So, to overcome this, Docker introduced a concept called Docker Compose.

Docker Compose is a tool you can use to define and share multi-container applications. This means you can run a project with multiple containers using a single source.

Features of Docker Compose

The key features of Compose that make it effective are:

  • Have multiple isolated environments on a single host

  • Preserve volume data when containers are created

  • Only recreate containers that have changed

  • Support variables and moving a composition between environments

With Docker Compose, you can create a single image that starts multiple containers as a service and you don't need to start each separately.

To achieve this, we need to write a yaml file i.e., docker-compose.yml file.

YAML(Yet Another Markup Language)

YAML is also known as YAML ain’t markup language is a human-readable data serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.

YAML files use a .yml or .yaml extension.

  1. Whitespace indentation is used for denoting structure; however, tab characters are not allowed as part of that indentation.

  2. Comments begin with the number sign (#), can start anywhere on a line and continue until the end of the line. Comments must be separated from other tokens by whitespace characters. If # characters appear inside of a string, then they are number sign (#) literals.

  3. List members are denoted by a leading hyphen (-) with one member per line.

    • A list can also be specified by enclosing text in square brackets ([...]) with each entry separated by a comma.
  4. An associative array entry is represented using colon space in the form key: value with one entry per line. YAML requires the colon be followed by a space so that url-style strings can be represented without needing to be enclosed in quotes.

    • A question mark can be used in front of a key, in the form "?key: value" to allow the key to contain leading dashes, square brackets, etc., without quotes.

    • An associative array can also be specified by text enclosed in curly braces ({...}), with keys separated from values by colon and the entries separated by commas (spaces are not required to retain compatibility with JSON).

  5. Strings (one type of scalar in YAML) are ordinarily unquoted, but may be enclosed in double-quotes ("), or single-quotes (').

    • Within double-quotes, special characters may be represented with C-style escape sequences starting with a backslash (\). According to the documentation the only octal escape supported is \0.

    • Within single quotes the only supported escape sequence is a doubled single quote ('') denoting the single quote itself as in 'don''t'.

  6. Block scalars are delimited with indentation with optional modifiers to preserve (|) or fold (>) newlines.

  7. Multiple documents within a single stream are separated by three hyphens (---).

    • Three periods (...) optionally end a document within a stream.
  8. Repeated nodes are initially denoted by an ampersand (&) and thereafter referenced with an asterisk (*).

  9. Nodes may be labeled with a type or tag using a double exclamation mark (!!) followed by a string, which can be expanded into a URI.

  10. YAML documents in a stream may be preceded by 'directives' composed of a percent sign (%) followed by a name and space-delimited parameters. Two directives are defined in YAML 1.1:

    • The %YAML directive is used for identifying the version of YAML in a given document.

    • The %TAG directive is used as a shortcut for URI prefixes. These shortcuts may then be used in node type tags.

docker-compose file Example

docker-compose.yml

version : "3.3"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123"

How to Deploy a Multi Container Docker Compose Application

  1. Install docker-compose using the below command after connecting to your instance:

     sudo apt-get install docker-compose
    

  2. You can check docker-compose version using the below command:

  3. Clone the Docker Compose application repository on your instance using the below command:

     git clone https://github.com/docker/awesome-compose.git
    

  4. Once in the project directory, to start the containers using cdocker-ompose.yaml file as annotated above run:

     docker-compose up -d
    
  5. And after Docker pulls the images and creates the network for the containers to communicate with each other, you will see an output like in the image below.

  6. To confirm that the 3 containers are running, run $ docker ps and you should see an output similar to the image below.

  7. Now you can test your application in your browser by following the steps we did in the previous blog i.e., https://sharmasmriti.hashnode.dev/day-17-dockerfile

Conclusion

In Conclusion, Docker Compose is a powerful tool that simplifies the deployment and management of multi-container Docker applications. It allows developers to define and configure multi-container environments in a single YAML file, making it easier to orchestrate complex setups.

With Docker Compose, you can specify services, networks, and volumes, enabling seamless collaboration and consistency across development, testing, and production environments. It promotes the idea of infrastructure as code, facilitating easy replication and sharing of application environments.

Overall, Docker Compose streamlines the containerization process and enhances the efficiency of managing containerized applications.

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌