Docker-compose a SpringBoot + MySQL application

Dhruv Saksena
2 min readJun 4, 2021

--

The main crux of Docker container is that every process is a container and we have multiple containers connected to each other to give an overall app experience.

Now, managing individual images and containers can be a tedious process, therefore we have Docker-compose to compose an entire application.

In my earlier post, I explained on how to run a stand alone Spring Boot application. Here, we will connect the same Spring Boot application to MySQL having Docker as our infrastructure.

The two containers connect with each other via docker network.

Docker-compose helps in establishing this entire infrastructure via a single docker-compose.yml file-

version: "3.7"services: mysql-service:
image: mysql:5.7
networks:
- spring-boot-mysql-network
restart: always
environment:
- MYSQL_ROOT_PASSWORD=ronnie01
- MYSQL_DATABASE=testapp
web-service:
build:
context: ./
dockerfile: Dockerfile

ports:
- "8080:8080"
networks:
- spring-boot-mysql-network
depends_on:
- mysql-service
networks:
spring-boot-mysql-network:
driver: bridge

Please refer my previous article, on how you can Dockerize a Spring Boot Application.

Here, we first create a MySQL service from a MySQL image and provide it’s user name and password.

Once, we do that we define the Spring Boot Application as web-service and the docker-file from which this container needs to be created in referenced in the file.

Following will be project structure

Execute command “docker compose up”

Following will be the output-

Once the build completes, you will get the below two docker images-

And also two running containers as a part of a single app deployment-

--

--