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.
To follow along, you’re going to need a clone of the repo. You can directly download or else need Git installed for this to work. The command to clone is git clone https://github.com/LetsCodeKnownSense/Docker-Compose-Demo.git
The above image explain the directory structure, which includes Dockerfile, the app folder with source code, and a requirements file listing dependencies.
Let’s break down the key sections of the provided above compose file and understand in detail.
- Networks Section:
networks:
counter-net:
This section defines a custom network namedcounter-net
. The containers within this Docker Compose setup will communicate over this network, allowing them to interact with each other. - Volumes Section:
volumes:
counter-vol:
This section defines a named volume namedcounter-vol
. Volumes are used for persistent storage, and in this case, it can be mounted into containers to store data persistently. - Services Section: This section defines the services (containers) that make up the application.
web-fe
Service:build: .
: Specifies that the Dockerfile for building theweb-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 theapp.py
Python script).ports
: Maps the container’s port 8080 to the host’s port 5001.networks
: Connects the container to thecounter-net
network.volumes
: Mounts the named volumecounter-vol
into the container at the path/app
.
redis
Service:image: "redis:alpine"
: Specifies the Docker image to be used for theredis
service. In this case, it uses the Redis image with the Alpine Linux base.networks
: Connects the container to thecounter-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.
You can find more commands on official page of docker
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.