Everything you Need to Know about Docker Compose

Photo of author

By admin

Docker has made the process of setting up virtual or local development environments a cakewalk. Moreover, the containerization platform offers a lot of extra functionalities and a good level of flexibility for users to create robust containers easily.

However, creating a lot of containers for your application makes it mandatory to build docker files as well. This ends up with complex management and time-consuming process and connecting them with each other becomes a next-to-impossible task. Since this digital era has solutions to most hurdles, this area is no exception.

Allowing users to have a YAML file to operate multi-container applications at once, Docker Compose is the go-to solution that solves this problem. This blog will help you know all the vital information about Docker Compose and how you can get started with it. Therefore, grab your snacks and laptop, we are ready to take off.

What is Docker Compose?

Docker Compose is a simple yet powerful tool that is used to run multiple containers along with each other as a single service. It allows every individual Docker container to run in isolation and interact with each other to complete a task.

Docker Compose files are extremely easy to write in a scripting language called YAML, which is an XML-based language. YAML stands for Yet Another Markup Language. Moreover, what makes Docker Compose even better is that it allows users to activate all the services (containers) using a single command.

Docker Compose EnvironmentLet’s understand this better with an example:

Here, we will be taking the example of Amazon, which is an eCommerce giant that you must already know about. A lot of users visit this site every day and perform different kinds of tasks to make a purchase.

They log in, search products, add them to the cart, make payments, check out, and whatnot. Each of these activities or services are categorized as different products, such as an account database, product database, cart database, and they run in the background and are considered as microservices.

Bigger the size of the website, the bigger the number of containers associated with different tasks. For developers, it becomes important to jump from one Docker container to another for smooth operation and management. Therefore, they use Docker Compose to connect different containers as a single service and ease out the container management.

Main Features of Docker Compose

1. Re-using Existing Containers

Docker Compose re-uses the existing containers which after you restart the service has not changed. Re-using them allows you to make changes without the hassle and in no time.

2. Preserved volume

Docker Compose keeps all your volumes preserved and makes sure that no leak is happening. While running, if it finds any container similar to the previous runs, it copies the volume from the old container to the new one. If you use Docker Compose on Windows, you’ll see environment variables that you can adjust as per the needs.

3. Multiple Isolated Environments

Docker Compose isolates containers and environments from each other yet allows them to function together easily. It uses a project name to achieve this and the default name is the basename of the project directory.

However, you can set a custom project name by using the -p command-line option, else you can use the COMPOSE_PROJECT_NAME environment variable.

4. Customize using variables

Docker Compose supports variables to use in the website. These variables can be used to customize the composition for different environments or users.

You can check out the variable substitution for complete knowledge and how to execute them. It also comes with an ‘extend’ file that you can use to create multiple files.

Basic Commands in Docker Compose

The below-mentioned commands are the basic yet important and commonly used commands that can be executed in the Docker Compose. Have a look at these commands and their uses:

Commands Uses
Docker Compose up Starts all the services.
Docker Compose down Stops all the services.
pip install -U Docker-compose Installs Docker Compose using pip.
Docker-compose-v Checks the version of the Docker Compose.
Docker-compose up -d Runs the Docker Compose files.
Docker ps Lists the entire existing Docker containers.
Docker Compose up -d -scale Scales a service.
Docker Compose.yml Allows you to use YAML files to configure application services.

Installing Docker Compose on Windows, Linux, and macOS

Before proceeding, make sure that you have Docker Engine installed either locally or remotely as Docker Compose uses it to execute the tasks. Also, ensure to have:

  • On desktop systems like Docker Desktop for macOS and Windows, Docker compose would be already included as a part of desktop installs.
  • On the Linux system, first install Docker Engine on your preferred Linux Distro.
  • Run Docker Compose as a non-root user. You can check these post-installation steps for Linux.

macOS

Docker Compose is included in the Docker Desktop along with other Docker apps. Therefore, you don’t need to install Compose separately if you’re installing Docker Desktop. But there are certain prerequisites, summed up as follows:

  • The hardware shouldn’t be lower than the 2010 model with the Intel processor.
  • The macOS version must be 10.14 or above.
  • Minimum 4 GB of RAM.

Docker Desktop is available as a community version of docker which you can install from Docker Hub.

Windows

Just like macOS, Docker Desktop for Windows also includes Compose along with other important Docker applications. Therefore, Windows users need not go the extra mile to install Compose separately. However, ensure to have:

  • 64-bit Windows 10.
  • At least 4 GB of RAM.
  • BIOS-level hardware virtualization.
  • Features for Hyper-V and Containers Windows must be enabled.

For Windows, Docker Desktop is also available as the Community version of Docker. You can download it directly from the Docker Hub.

Linux

To install Docker Compose on Linux, you have to download Docker Binary. Now follow the step-by-step instructions mentioned below:

1. Installing

Run the following command in your terminal in order to download the current stable release of Docker Compose.

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

Note: The 1.28.2 version is available during the time this article is written. Please check the latest version or the version you need and add that to the command.

2. Applying permissions to binary

Now, you need to apply the executable permissions to the binary you’ve downloaded before. Use this command for doing the same:

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

3. Test

To test your installation, you can run the following command:

“$ docker-compose –version”

Conclusion

The blog has covered all the necessary information regarding Docker Compose along with its installation process on different operating systems. We hope that now you’ve understood what it is and how it can be utilized to improve your development and deployment workflow.

If this article has helped you, then please consider recommending and sharing it with other developers who are looking for the same information. Also, if you have any questions or feedback, then comment them down below. Our experts are available 24*7 to hear you.

Leave a Comment