Docker Compose For Java Applications: Simplifying Deployment (original) (raw)

Last Updated : 6 Aug, 2025

Docker is an open-source platform for developers and sysadmins to build, ship, and run distributed applications. If we want to define and run more than one container then we should go for docker compose. We have to configure multiple container environments in a single file so that it simplifies the deployment process. In Java applications, Docker Compose sequencing the deployment of Java-based services, databases, and other components, ensuring consistency across various environments.

Table of Content

What is Docker in Java?

Docker in java simplifies the deployment of applications by containerizing the Java applications. It facilitates ensuring they run consistently across different environments. It allows the developers to package the application code with all its libraries, runtime, and dependencies into a single container. Through containerizing the application it brings portability, scalability, and efficient resource utilization for the Java applications.

What is Docker Compose?

Docker Compose is a docker-supportive native tool that facilitates defining and running multi-container applications in docker. It allows the users to configure the application services in the YAML file and manages them with simple commands. This docker composes support with simplifying the orchestration and scaling of the complex applications.

Docker Compose YAMLml file named '**docker-compose.yml'. This will be created inside the project root directory. In this, file we need to segregate all the services one after another and make dependency if needed. The following are a few terminologies we need to use in the docker-compose.yml file

environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test_db

Options of Docker Compose

The following table specifies on the options of docker compose with its functionalities:

Option/Command Description
docker-compose up It helps to builds, creates, starts, and attaches to containers for a service
docker-compose down It helps to stops and removes containers, networks, images, and volumes
docker-compose build It helps to builds or rebuilds services
docker-compose start It starts the existing containers for a service
docker-compose stop It stops the running containers without removing them
docker-compose restart It restarts the running containers
docker-compose ps It will lists containers
docker-compose logs It will displays the log output from services
docker-compose exec It executes a command in a running container
docker-compose config It validates and views the Compose file
docker-compose pull It pulls service images
docker-compose push It pushes the service images
docker-compose scale It sets the number of containers for a service
docker-compose rm It removes stopped service containers
docker-compose run It runs a one-time command against a service

Operations of Docker Compose

The following are the operations of docker compose:

1. Docker Compose Up

The docker-compose up command is used to start the services defined in the docker-compose.yml file. It can also be used with the --build option to build images before starting containers.

docker-compose up --build

2. Docker Compose Down

The docker-compose down command is used to stop and remove the containers, networks, and volumes defined in the docker-compose.yml file. It also removes any resources created during the docker-compose up process.

docker-compose down -v

**Syntax

services:
app:
image: openjdk:17-jdk-slim
ports:
- 8000:8000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_DB: test_db
mysql:
image: mysql:8.0
volumes:
- gfg-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test_db
volumes:
gfg-mysql-data:

3. Build Docker Compose

The following command is used to build the docker images specified in the docker compose file. It reads the docker compose file and build the images as per specified in the docker compse file.

docker-compose build

4. Run Docker Compose

The following command is used to start the services specified in the docker compose file. It reads the docker compose file and start the containers based on the configurations of the services.

docker-compose run

Why deploy Java Application on Docker Compose?

The following are the some of the main reasons to deploy the java application on the docker compose:

How to Deploy Java Application with Docker Compose? A Step-By-Step Guide

Creating a simple Java application with Docker Compose. In this example, we'll create a simple Spring Boot application. Its having get API, and it provides some sample message like "Geeks For Geeks Docker Compose Example".

**Step1: Create a Spring Boot Application

Creating a Spring Boot Application

Project Structure

**Step2: Create GFGController.java

package com.geeksforgeeks.docker.controller;

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

@RestController @RequestMapping("api") public class GFGController {

@GetMapping("/get-project-name")
public String getProjectName() {
    return "Geeks For Geeks Docker Compose Example";
}

}

`

**Step 3: Change Port (Optional)

server.port=8081

**Step 4: Build the Spring Boot Application

Right click on project -> Run as -> maven clean

Right click on project -> Run as -> maven install

target folder and jar file

**Step 5: Create a Dockerfile

FROM openjdk:17-jdk-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

**Step 6: Create Docker Compose File

version: '3'
services:
myapp:
build: .
ports:
- "8081:8081"

**Step 7: Build and Run the Docker Container

Build the Docker image

docker-compose build

Run the Docker container

docker-compose up

**Note: If you want to build and run your image with single command then use "docker-compose up --build".

docker compose build

Docker Compose run

**Step 8: Access the Application

output of spring boot docker application

Conclusion

The docker-compose.yml file easy the process of managing and deploying multiple container applications. Instead of using multiple "docker run" commands with various configurations, we can define everything in a single file. This makes it simplify to share and reproduce application stack across different environments.