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 s…


This content originally appeared on DEV Community and was authored by Mukhil Padmanabhan

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!


This content originally appeared on DEV Community and was authored by Mukhil Padmanabhan


Print Share Comment Cite Upload Translate Updates
APA

Mukhil Padmanabhan | Sciencx (2024-11-02T15:37:50+00:00) A Simple Guide to Docker Compose & Multi-Container Applications. Retrieved from https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/

MLA
" » A Simple Guide to Docker Compose & Multi-Container Applications." Mukhil Padmanabhan | Sciencx - Saturday November 2, 2024, https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/
HARVARD
Mukhil Padmanabhan | Sciencx Saturday November 2, 2024 » A Simple Guide to Docker Compose & Multi-Container Applications., viewed ,<https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/>
VANCOUVER
Mukhil Padmanabhan | Sciencx - » A Simple Guide to Docker Compose & Multi-Container Applications. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/
CHICAGO
" » A Simple Guide to Docker Compose & Multi-Container Applications." Mukhil Padmanabhan | Sciencx - Accessed . https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/
IEEE
" » A Simple Guide to Docker Compose & Multi-Container Applications." Mukhil Padmanabhan | Sciencx [Online]. Available: https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/. [Accessed: ]
rf:citation
» A Simple Guide to Docker Compose & Multi-Container Applications | Mukhil Padmanabhan | Sciencx | https://www.scien.cx/2024/11/02/a-simple-guide-to-docker-compose-multi-container-applications/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.