Efficiently Manage Your Docker Containers with Docker Compose

Rasalingam Ragul
3 min readMar 7, 2023

--

As the complexity of modern applications grows, so does the need for efficient deployment solutions. Containerization is one such solution that has gained popularity in recent years. With containers, you can package your applications along with their dependencies, libraries, and configurations, and deploy them in a consistent, isolated environment. Docker is one of the most popular containerization tools available today, and Docker Compose is a powerful tool that simplifies the management of Docker containers. In this article, we’ll explore Docker Compose and its benefits.

What is Docker Compose?

Docker Compose is a tool that allows you to define and run multi-container Docker applications. With Docker Compose, you can specify the services that make up your application, how they should be configured, and how they should communicate with each other. You can define your application using a YAML file, and then use the Docker Compose command-line tool to start and stop your application, as well as perform other operations such as scaling your services.

Why use Docker Compose?

Docker Compose simplifies the process of managing Docker containers, especially for complex applications that require multiple containers. Instead of manually starting and stopping each container, you can define all your services in a single YAML file, and then use Docker Compose to manage them as a group. Docker Compose also allows you to define the configuration of your services, including environment variables, volumes, and ports. This makes it easy to deploy your application consistently across different environments.

Another benefit of Docker Compose is that it makes it easy to scale your application. You can use the Docker Compose command-line tool to start additional instances of your services, or to scale down your services when they’re no longer needed. Docker Compose also supports the use of external tools for service discovery and load balancing, such as Consul or HAProxy.

Prerequisites Before getting started, you will need to have the following installed on your machine:

  • Docker
  • Docker Compose

How to use Docker Compose?

  1. Create a Dockerfile for your Spring Boot application Before creating a Docker Compose file, you will need to create a Dockerfile for your Spring Boot application. You can follow the steps outlined in the previous article to create a Dockerfile. Ensure that your Dockerfile is in the root directory of your project.
  2. Define your services in a Docker Compose file Create a new file in the root directory of your project called docker-compose.yml. This file will define the services for your application. Here is an example of a docker-compose.yml file for a Spring Boot application:
version: '3.8'
services:
app:
build: .
ports:
- 8090:8090
environment:
- SPRING_PROFILES_ACTIVE=dev
# depends_on:
# - db
# db:
# image: mysql:8.0.28
# environment:
# - MYSQL_ROOT_PASSWORD=password
# - MYSQL_DATABASE=myapp
# - MYSQL_USER=user
# - MYSQL_PASSWORD=password

This file defines two services: app and db. The app service is built using the Dockerfile in the current directory (denoted by .). The ports section maps port 8090 in the container to port 8090 on the host machine. The environment section sets the SPRING_PROFILES_ACTIVE environment variable to dev. Finally, the depends_on section specifies that the app service depends on the db service.

The db service is using an existing MySQL Docker image from Docker Hub. The environment section sets the root password, database name, user, and password but in this example i didn’t use db.

3. Run your application using Docker Compose To start your application using Docker Compose, navigate to the root directory of your project and run the following command:

docker-compose up

This will build the Docker image for your application and start the containers defined in your docker-compose.yml file. You should see logs from each container in the terminal window.

4. Test your application Once your containers are up and running, you can test your application by opening a web browser and navigating to http://localhost:8090. If everything is working correctly, you should see the expected output from your Spring Boot application.

5. Stop your application To stop your application, press Ctrl + C in the terminal window where you ran docker-compose up. This will stop and remove the containers defined in your docker-compose.yml file.

Conclusion

Docker Compose is a powerful tool that simplifies the management of Docker containers, especially for complex applications. With Docker Compose, you can define your application using a single YAML file, and then use the Docker Compose command-line tool to start and stop your application, as well as perform other operations such as scaling your services. Docker Compose also allows you to define the configuration of your services, making it easy to deploy your application consistently across different environments. If you’re using Docker to containerize your applications, Docker Compose is definitely worth checking out.

https://github.com/rragul/docker-demo

--

--