Docker Compose Tool To Run aMulti Container Applications
Last Updated :
04 Jun, 2024
The article talks about how to run multi-container applications using a single command. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can configure a file (YAML file) to configure your docker containers. Then Once you configured the Yaml file with a single command, you create and start all the services (containers) from your configuration.
Let’s say we have a simple application having two components, a flask app and a Redis database. I will go by running the complete application with and without using the docker-compose tool, which would give your major use of this compose tool.
What Is Docker Compose And How Does It Work?
Docker Compose is a software containerized tool developed to orchestrate the definitions and running of multi-container docker applications using single commands. It reads the definitions of the multiple containers from the single Configuration Yaml file and performs the orchestration with single-line commands making easy usage for developers.
It facilitates the management of complex applications allowing the users to specify the services, networks, and docker volumes required for the application. In the configuration yaml file developers can specify the parameters for each service including the docker images, environmental variables, ports, and dependencies. Docker compose provides security to the application by encapsulating the entire application setup in the Compose file providing consistency across different development environments making it easy to share and reproduce the applicable reliably.
Creating a Working Environment For The Project
Step 1: Create a directory gfg_docker_compose that holds the our project
$ mkdir gfg_docker_compose
Step 2: Move to that directory making as your working directory by running the following command.
$ cd gfg_docker_compose
Step 3: Create the requirements.txt file
$ touch requirements.txt
Step 4: Copy the below provided code in that requirements.txt file.
flask
redis
Step 5: Create the file app.py . It will used to have the code for our flask app
$ touch app.py
Step 6: Now the Copy the below code to app.pyÂ
from flask import Flask, request, json
from redis import Redis
# initializing a new flask app
app = Flask(__name__)
# initializing a new redis database
# Hostname will be same as the redis service name
# in the docker compose configuration
redis = Redis(host ="localhost", db = 0, socket_timeout = 5,
charset ="utf-8", decode_responses = True)
# Our app has a single route allowing two methods POST and GET.
@app.route('/', methods =['POST', 'GET'])
def animals():
if request.method == 'POST':
# Take the name of the animal
name = request.json['name']
# push the name to the end of animals list in the redis db
redis.rpush('animals', {'name': name})
# return a success
return jsonify({'status': 'success'})
if request.method == 'GET':
# return complete list of names from animals
return jsonify(redis.lrange('animals', 0, -1))
Explanation Of The Containerized Application
Here we are simply accepting two methods GET and POST requests for `/` route. When ever a POST request is done with the name, the name is added at the end of the animals list. For GET request we will return the list of names from animals list.
Step 7: Create the Dockerfile named file.
$ touch Dockerfile
Step 8: Now try to copy the below code to that created Dockerfile.Â
# Pulling the base image
FROM python:3.7.0-alpine3.8
# Creating a folder and moving into it
WORKDIR /usr/src/app
# Copying the dependency list
COPY requirements.txt ./
# Installing the python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copying the flask code into the container
COPY . .
ENV FLASK_APP=app.py
EXPOSE 5000
# Starting the server
CMD flask run --host=0.0.0.0
Explanation Of The Dockerfile
Through configured definition of the Dockerfile we will start with the base image python:3.7.0-alpine3.8. We will copy the requirements.txt file and install all our flask app dependencies. Then we would copy the app.py file into the container and finally run the flask app.Â
And Now we ready with the docker application. Firstly let’s see running the docker application without docker-compose and then docker-compose tool so that you can understand the performance of docker compose efficiently.
Deploying Docker Application Without Docker Compose Tool
To deploy and use this application without docker compose tool would be tedious for a multi-container application. As you need to remember the complete configuration and usage whenever you run the application. Let’s see how it is normally deployed and run without the compose tool.Till Now we have created the application file such as requirements.txt , app.py and Dockerfile in the gfg_docker_compose directory.
Step 1: Fristly In this Approach we will run and start our redis server container with docker run command as shown below:
$ docker run --name=redis redis:4.0.11-alpine
Step 2: From the following practical screenshot, you can see the software packages of redis version4.0.11 with alpine image is getting downloaded extracting docker hub and after starting the redis server.
 Step 3: Now our redis has started so you should take it’s container IP address
gfg_docker_compose/ $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis

- This gives you a IP address which you need to put it to the host parameter in the app.py.Â
- Now the line looks like in app.py
redis = Redis(host="IPAddress", db=0, socket_timeout=5,
charset="utf-8", decode_responses=True)
where IPAddress is the IP address you get from the redis container.
Step 4: Build the flask application
$ docker build -t gfg/flask-app .

Step 5: Wait for some time, till the application image has built. Now we will start our flask app container as well.Â
Step 6: Open a new terminal tab and run the below command
$ docker run -p 5000:5000 gfg/flask-app

Step 7: So using that command we will pull the gfg/flask-app which we have built earlier and run our flask app container. Also, -p is used to map the port 5000 from container to the host.
Step 8: Finally when you route to the flask app on a browser you should see something like this.

Deploying Docker Application With Docker Compose Tool
On Using docker-compose tool the setup process for multi-containered docker applications will become so easy. The Simple idea behind this is, we will write the complete container configuration in a YAML file called docker-compose.yml file and then with simple commands we can start and stop these applications. This method will also help us to share our docker applications easily to other developers, by simply sharing the docker-compose file with the project.
Step 1: Firstly create the docker-compose.yml file.
$ touch docker-compose.yml
- Till now we are having the created files such as app.py , requirements.txt, Dockerfile and Docker-compose.yml file in the gfg_docker_compose working directory. The project tree is listed here:
gfg_docker_compose
--->> app.py
--->> requirements.txt
--->> Dockerfile
--->> docker-compose.yml
Step 2: Now copy the below YAML code to docker-compose.yml file.
version: '3'
services:
app:
build: .
image: gfg/flask-app
environment:
- FLASK_ENV=development
ports:
- 5000:5000
redis:
image: redis:4.0.11-alpine
Explanation Of Docker Compose File With Its Terminologies
The following are the keywords specified in the above configured Docker Compose File:
- version: states the version of docker-compose to use, here we are using version 3
- services: holds all our application services (container) configurations.
- app: We have named our flask app as app service, feel free to give it any other name you want.
- build: relative path to the Dockerfile
- image: name of the final docker application image
- environment: list of environment variables
- ports: list of ports to be mapped from the container to the host machine
- redis: name of our redis service
- image: name of the image.
NOTE: Service names app and redis are also the hostname for the services(containers) we run because docker-compose automatically creates a network and adds our containers to that network so every container can recognize other containers by their service name as hostname within that network. So this is the reason we will keep the host parameter in the app.py file to redis itself.
Step 3: Now Start the application with the following docker compose command:
$ docker-compose up --build
- –build is used to explicitly mention to build the images before starting the application.
- You can see the application working as below

Step 4: To stop the complete application run the following docker compose command:
$ docker-compose down

- Using the docker-compose tool, we can make the multi-container docker application setup process much faster and easier than the usual way.
Conclusion
In Conclusion, Docker compose provides an effective way of deployment and management of multi-container applications. It offers an efficient solution to define, configure and run the services with a single command. On utilizing a Yaml file, developers can easily replicate and share their complicated application setups. Whether deploying the flask app with Redis or handling more complicated scenarios, docker compose simplifies the orchestration process facilitating efficient and scalable containerized deployments.
Similar Reads
What is Docker?
Have you ever wondered about the reason for creating Docker Containers in the market? Before Docker, there was a big issue faced by most developers whenever they created any code that code was working on that developer computer, but when they try to run that particular code on the server, that code
12 min read
Docker Installation
Docker - Installation on Windows
In this article, we are going to see how to install Docker on Windows. On windows if you are not using operating system Windows 10 Pro then you will have to install our docker toolbox and here docker will be running inside a virtual machine and then we will interact with docker with a docker client
2 min read
How to Install Docker using Chocolatey on Windows?
Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install and Configure Docker in Ubuntu?
Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Docker on MacOS?
Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?
In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its softwa
2 min read
How to Install Docker-CE in Redhat 8?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package. Installing Docker-CE in Redhat 8: St
2 min read
Docker Images
What is Docker Image?
Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the
10 min read
Working with Docker Images
If you are a Docker developer, you might have noticed that working with multiple Docker Images at the same time might be quite overwhelming sometimes. Managing numerous Docker Images all through a single command line is a very hefty task and consumes a lot of time. In this article, we are going to d
2 min read
Docker - Publishing Images to Docker Hub
Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read
Docker Commit
Docker is an open-source container management service and one of the most popular tools of DevOps which is being popular among the deployment team. Docker is mostly used in Agile-based projects which require continuous delivery of the software. The founder, Chief Technical Officer, and Chief Archite
10 min read
Docker - Using Image Tags
Image tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project's version and lets you keep track of the overall development
7 min read
Next.js Docker Images
Using Next.js Docker images allows your app to deploy to multiple environments, and is more portable, isolated and scalable in dev and prod. Dockerâs containerization makes app management super easy, you can move from one stage to another with performance. Before we get started, letâs cover the basi
14 min read
How to Use Local Docker Images With Minikube?
Minikube is a software that helps in the quick setup of a single-node Kubernetes cluster. It supports a Virtual Machine (VM) that runs over a docker container and creates a Kubernetes environment. Now minikube itself acts as an isolated container environment apart from the local docker environment,
7 min read
Docker Containers
Containerization using Docker
Docker is the containerization platform that is used to package your application and all its dependencies together in the form of containers to make sure that your application works seamlessly in any environment which can be developed or tested or in production. Docker is a tool designed to make it
9 min read
Virtualisation with Docker Containers
In a software-driven world where omnipresence and ease of deployment with minimum overheads are the major requirements, the cloud promptly takes its place in every picture. Containers are creating their mark in this vast expanse of cloud space with the worldâs top technology and IT establishments re
9 min read
Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its depende
12 min read
Docker - Remove All Containers and Images
In Docker, if we have exited a container without stopping it, we need to manually stop it as it has not stopped on exit. Similarly, for images, we need to delete them from top to bottom as some containers or images might be dependent on the base images. We can download the base image at any time. So
10 min read
How to Push a Container Image to a Docker Repository?
In this article we will look into how you can push a container image to a Docker Repo. We're going to use Docker Hub as a container registry, that we're going to push our Docker image to. Follow the below steps to push container Image to Docker repository: Step 1: The first thing you need to do is m
2 min read
Docker - Container Linking
Docker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers.There are times during the development of our application when we need two containers to be able to communicate with each other. It might be p
4 min read
How to Manage Docker Containers?
Before virtualization, the management of web servers and web applications was tedious and much less effective. Thanks to virtualization, this task has been made much easier. This was followed by containerization which took it a notch higher. For network engineers, learning the basics of virtualizati
13 min read
Mounting a Volume Inside Docker Container
When you are working on a micro-service architecture using Docker containers, you create multiple Docker containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the containers
10 min read
Difference between Docker Image and Container
Pre-requisite: Docker Docker builds images and runs containers by using the docker engine on the host machine. Docker containers consist of all the dependencies and software needed to run an application in different environments. What is Docker Image?The concept of Image and Container is like class
5 min read
Difference between Virtual Machines and Containers
Virtual machines and Containers are two ways of deploying multiple, isolated services on a single platform. Virtual Machine:It runs on top of an emulating software called the hypervisor which sits between the hardware and the virtual machine. The hypervisor is the key to enabling virtualization. It
2 min read
How to Install Linux Packages Inside a Docker Container?
Once you understand how to pull base Docker Images from the Docker registry, you can now simply pull OS distributions such as Ubuntu, CentOS, etc directly from the Docker hub. However, the OS Image that you have pulled simply contains a raw file system without any packages installed inside it. When
2 min read
Copying Files to and from Docker Containers
While working on a Docker project, you might require copying files to and from Docker Containers and your Local Machine. Once you have built the Docker Image with a particular Docker build context, building it again and again just to add small files or folders inside the Container might be expensive
9 min read
How to Run MongoDB as a Docker Container?
MongoDB is an open-source document-oriented database designed to store a large scale of data and allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables. In this
4 min read
Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its depende
12 min read
Docker - Container for NGINX
Docker is an open-source platform that enables developers to easily develop, ship, and run applications. It packages an application along with its dependencies in an isolated virtual container which usually runs on a Linux system and is quite light compared to a virtual machine. The reason is that a
11 min read
How to Provide the Static IP to a Docker Container?
Docker is an open-source project that makes it easier to create, deploy and run applications. It provides a lightweight environment to run your applications.It is a tool that makes an isolated environment inside your computer. Think of Docker as your private room in your house. Living with your fami
2 min read
Docker Networking
Docker Networking
Pre-requisite: Docker Docker Networking allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help
5 min read
Docker - Managing Ports
Pre-requisites: Docker Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the c
4 min read
Creating a Network in Docker and Connecting a Container to That Network
Networks are created so that the devices which are inside that network can connect to each other and transfer of files can take place. In docker also we can create a network and can create a container and connect to the respective network and two containers that are connected to the same network can
2 min read
Connecting Two Docker Containers Over the Same Network
Whenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.Docker of
3 min read
How to use Docker Default Bridge Networking?
Docker allows you to create dedicated channels between multiple Docker Containers to create a network of Containers that can share files and other resources. This is called Docker Networking. You can create Docker Networks with various kinds of Network Drivers which include Bridge drivers, McVLAN dr
7 min read
Create your own secure Home Network using Pi-hole and Docker
Pi-hole is a Linux based web application, which is used as a shield from the unwanted advertisement in your network and also block the internet tracking system. This is very simple to use and best for home and small office networks. This is totally free and open-source. It also allows you to manage
3 min read