Logo

dev-resources.site

for different kinds of informations.

A Simple Guide to Docker Compose & Multi-Container Applications

Published at
11/2/2024
Categories
docker
dockercompose
beginners
containers
Author
mukhilpadmanabhan
Author
17 person written this
mukhilpadmanabhan
open
A Simple Guide to Docker Compose & Multi-Container Applications

In the previous blog, we discussed about Docker and containerization. By now you already know that Docker helps you in packaging and deploying your applications in isolated containers. But what if an application has multiple components like a backend server, a database or may be a cache? Handling each container of such application could be daunting! Docker Compose helps with this.

Docker Compose allows you to define and manage multi-container with only one configuration file. It helps you orchestrate complex applications which could become perfect scenario when it comes to modern multi-service application.

Today I am going to show you how to setup multi-container applications using Docker Compose; we will cover the basics of Docker Compose and why you should start using it.


Why Docker Compose?

Letโ€™s say you are building a blog application. Itโ€™s not a single service - you need a backend to handle requests, a database to store data, and maybe even a caching service for performance. Managing these services one by one requires too much manual work. To the rescue comes Docker Compose โ€“ it allows you to define and start all your services in one command.

Benefits of Docker Compose

Simplified Configuration: You can define multiple services in a single YAML file which helps to keep track of your dependencies easily.

Scalability: If we want to scale any individual service, Docker Compose has made it amazingly simple.

Portability: In Docker Compose, with other developers we can easily share the complete multi-container setup and environment will be consistent in both places.


Setting Up Docker Compose

For this blog, weโ€™ll create a simple multi-container application consisting of:

  • A Node.js backend (serving as an API)
  • A MongoDB database (to store data)

Step 1: Create a Project Structure
Set up your project directory with the following structure:

Image description

This structure keeps the backend code separate from the Docker Compose file, making it organized and modular.

Step 2: Define the Backend Service
Inside the backend folder, create a simple Node.js application. Start by initializing the project andd installing Express, a Node.js framework:

Image description

Next, add a simple Express server in app.js:

Image description

This code is our simple Express server with one route, but weโ€™ve also got MongoDB connected using the connection string mongodb://mongo:27017/mydb. The mongo part of the connection string is referencing the name of the MongoDB service weโ€™ll define in our Docker Compose.

Step 3: Write a Dockerfile for the Backend
In the backend directory, create a Dockerfile to define how Docker should build the backend service:

Image description

This Dockerfile specifies the base image (Node.js), sets up the working directory, installs dependencies, copies the application files, and finally exposes port 3000.

Step 4: Create the docker-compose.yml File
Now, set up Docker Compose to define and connect our backend and MongoDB services. In the root of your project, create a docker-compose.yml file with the following content:

Image description

Explanation:

backend: This service is built using the Dockerfile in the backend folder. The depends_on option ensures that the mongo service starts before the backend service.

mongo: This service uses the official MongoDB image and maps port 27017 on the host to 27017 inside the container. The mongo-data volume persists MongoDB data, so it doesnโ€™t get lost if the container restarts.


Common Use Cases for Docker Compose

  • Local Development Environment: Docker Compose is being used by many developers to create local environment to test multi-service application. So that each member of the team can bring up same environment in few seconds.
  • Docker Compose is commonly used in CI/CD (Continuous Integration/Continuous Deployment) pipelines to build isolated test environments. With Docker Compose, CI/CD systems can test applications with confidence in an identical environment to the production deployment.
  • Microservices Architecture: In Microservices architecture, each service like authentication, billing and user management can be managed as a separate container in Docker Compose. It help make each service to develop, test and scale independent.

As you have seen, working with multi-container applications using Docker Compose is really easy. You do not have to worry about managing each container individually. What you can do is put your complete application setup in one file, and bring it up with just one command. This approach becomes very powerful when dealing with applications comprising multiple services because everything is of the same instance and easy to scale.

As you grow along with Docker and Docker Compose, you will see that there are tons of advanced features and configurations where it can empower your development flow. Either for simple project or multi-service system, you definitely need to make use of Docker Compose because it just makes our life as a developer easier.

See you again in next Docker topic where we deal with more advanced containerization!

dockercompose Article's
30 articles in total
Favicon
Want to Learn Docker in Advance Way?
Favicon
deploy Jenkins using docker compose with production ready
Favicon
Docker Advance Part 2: Docker Logging
Favicon
A Simple Guide to Docker Compose & Multi-Container Applications
Favicon
Dockerize nestjs app with postgres
Favicon
Tips and Tricks for Docker Compose: Leveraging the override Feature
Favicon
Dockerize nestjs application with Postgres
Favicon
RabbitMQ container with Docker Compose
Favicon
Quick and simple Local WordPress Setup for Lazy Developers
Favicon
Bug: Docker-compose up?
Favicon
How to resolve Docker Compose Warning WARN[0000] Found orphan containers
Favicon
How to resolve Docker Compose Warning WARN[0000] Found orphan containers
Favicon
How To Set Up Docker Seleniumย GRID
Favicon
How to Ensure Docker Compose Uses Environment Variables from the `.env` File
Favicon
Essential Docker Commands for Developers
Favicon
[Docker] How to fix 'host not found in upstream "host.docker.internal"'.
Favicon
Como configurar imagem Docker(PHP e Nginx) para projetos Laravel com PHP 8.3
Favicon
Containerize your multi-services app with docker compose
Favicon
แžŸแž˜แŸ’แžšแžถแž”แŸ‹แž›แžปแž”แžขแŸ’แžœแžธแŸ—แž‘แžถแŸ†แž„ แžขแžŸแŸ‹ แž€แŸ’แž“แžปแž„ Docker compose.
Favicon
Installation of Docker Compose:
Favicon
MongoDB containers with Docker Compose
Favicon
An Overview of Docker Compose and its Features.
Favicon
How to change docker root data directory and why would you want to do that (hint: to optimize space)
Favicon
Containerize a Web Application using docker compose
Favicon
O Docker Compose agora tem uma V2, veja como instalar
Favicon
Vue 2 vite dockerized steps
Favicon
An Advanced Guide (2) to Docker: Managing Multi-Container Applications ๐Ÿณ
Favicon
How to run mongodb using docker-compose in ubuntu
Favicon
How to run postgres database using docker and docker-compose
Favicon
Easy Dockerization with Docker INIT

Featured ones: