Docker Compose

Last Updated on March 2, 2024 by KnownSense

We all know about images, containers, networks, and volumes. We also know a bit about microservices apps. Well, it’s time to bring it all together with the wonderful and amazing Docker Compose. We’ll start out with a quick overview. Then, we’ll get hands on deploying and managing a multicontainer app with Compose

Docker Compose: Big Picture

In the really early days of Docker, there was a company called Orchard with a product called Fig. It was pretty much a two‑person company out of London, and Fig was a Python tool that sat on top of Docker and let you deploy and manage multicontainer apps using a really simple config file. Fig was so cool that Docker bought it and renamed it as Docker Compose. For the longest time, it remained as separate Python tool that you had to manually install. But these days, it is fully integrated, and it’s now got its own docker compose subcommand.

Above is a simple example of config file and we call these Compose files. It’s defining a multicontainer app, which is just another word for a microservices app. It has sections for networks, volumes, and services. Under services, there are two parts: a web service and a data store. The config shows how these parts connect using the network and volume defined earlier.

This method of defining things in files like this is called Infrastructure as Code. In real situations, these files can get a lot more complicated with more networks, volumes, and services. They can also handle secrets and configurations. But this simple example gives us a good idea of how it all works, and now, let’s see it in action!

Deploying and Managing Apps with Docker Compose

Now, we’ll walk through the process of deploying and managing applications using Docker Compose. Whether you’re working on a Docker host, Docker Desktop, a Multipass VM, or any Docker-supported environment, this article will provide you with the necessary commands and insights to build, deploy, and maintain your applications seamlessly.

Docker Compose

The above image explain the directory structure, which includes Dockerfile, the app folder with source code, and a requirements file listing dependencies.

Docker Compose file

Let’s break down the key sections of the provided above compose file and understand in detail.

  1. Networks Section:
    networks:
    counter-net:

    This section defines a custom network named counter-net. The containers within this Docker Compose setup will communicate over this network, allowing them to interact with each other.
  2. Volumes Section:
    volumes:
    counter-vol:
    This section defines a named volume named counter-vol. Volumes are used for persistent storage, and in this case, it can be mounted into containers to store data persistently.
  3. Services Section: This section defines the services (containers) that make up the application.
    • web-fe Service:
      • build: .: Specifies that the Dockerfile for building the web-fe image is in the current directory (.).
      • command: python app.py: Overrides the default command in the Dockerfile and specifies the command to run when the container starts (in this case, running the app.py Python script).
      • ports: Maps the container’s port 8080 to the host’s port 5001.
      • networks: Connects the container to the counter-net network.
      • volumes: Mounts the named volume counter-vol into the container at the path /app.
    • redis Service:
      • image: "redis:alpine": Specifies the Docker image to be used for the redis service. In this case, it uses the Redis image with the Alpine Linux base.
      • networks: Connects the container to the counter-net network.

These configurations collectively define a Docker Compose setup with a custom network (counter-net), a named volume (counter-vol), and two services (web-fe and redis) that communicate over the specified network. The web-fe service is built from the current directory, runs a Python script, exposes ports, connects to the network, and mounts a volume. The redis service uses a pre-built Redis image and also connects to the shared network.

To bring up the services defined in the compose.yaml file run the below command

docker-compose up

If you want to run it in the background, you can use -d

docker-compose up -d

Above command will build the necessary images, create networks and volumes, and start the containers.To check if the containers are running, use the following command:

docker ps

If everything is set up correctly, you can access the application by opening a web browser and navigating to http://localhost:5001. Since you’ve mapped the container’s port 8080 to the host’s port 5001, this is where the application will be available.

Conclusion

Docker Compose is this epic tool for defining, deploying, and managing multi‑container microservices apps. It is a really friendly but a really powerful way of deploying and managing multi‑container microservices apps. We learnt how to define everything in a simple YAML file, and then use the docker compose command to deploy and manage everything in it.

Leave a Reply

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

Scroll to Top