Dockerize spring boot +MySQL application using docker-compose -part 2


In the previous tutorial part 1, we used Dockerfile to set up the environment and run the spring boot application by running containers separately and then building a link between them. Please checkout our previous article to know more about dockerize spring-boot application using Dockerfile

But in this session, we use docker-compose to dockerize the same spring boot application with MySQL.We will learn how to manage multiple containers and define the dependent services using a docker-compose file.

Docker-compose environment

If we want to run services with the docker-compose tool we have to follow these steps that are also defined in Docker documentation.

  1. We need to define the application environment with a Dockerfile, so it can be reproduced anywhere.
  2. We need to define the services that make up the application in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose commands to run/stop the container or deploy the application.

We need a docker-compose.yml file to write the services. In a Dockerfile, we defined the environment of the application, and in docker-compose file we write down the other properties of services, like which service will run on which port, which service will be dependent on other services, which port will be forwarded to other port for public access, define network, etc.

To know about Docker compose check Getting started with docker compose

Check docker-compose installation

we can check that docker-compose is installed by running the command  

docker-compose -v

In my case it returns: 

This image has an empty alt attribute; its file name is sp12.png

Download the code and Run the application

The code is the same as we cloned in the part 1 session and we build the application using Gradle.You may clone the repo here.

Here we can see the Dockerfile same we discussed in part 1 tutorial

Dokcerfile :

FROM java:8
VOLUME /home/docker/spring_boot
EXPOSE 10222
COPY /build/libs/book-manager-1.0-SNAPSHOT.jar book-manager-1.0-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","book-manager-1.0-SNAPSHOT.jar"]

And the docker-compose.yml file looks like this:

We defined two services named by docker-mysql and book-manager-app. Service book-manager-app is dependent on docker-mysql. We are using docker-compose version 3.

MySQL will run on the 3306 port at a docker container but we can access it publicly from 3308. book-manager-app will run on port 10222.  We have an initial DML and DDL file at the SQL directory which will run during startup time of Docker and MySQL setup.

Run Application With docker-compose

We will go to the project root directory(Already downloaded and created jar file).Here is my location looks like

This image has an empty alt attribute; its file name is sp14.png

To run the application we will use following commands:

  • docker-compose up — This will execute Dockerfile commands and will run services defined in the docker-compose file.
  •  docker-compose down — This will stop and remove all containers that were running by docker-compose file.
  •  docker-compose up --build — If we do an update on the Dockerfile, the war/jar file, or the docker-compose file, then we have to execute this command to get updated data on the Docker machine.

Note: Stop the previously created containers docker-mysql & book_manager_app if it is still running

This image has an empty alt attribute; its file name is sp13.png

Now run the application stack using command:

docker-compose up

you will get out put as below

This image has an empty alt attribute; its file name is sp-15-1024x442.png
This image has an empty alt attribute; its file name is sp16-1024x405.png

To check whether our application running or not, we can check in the browser http://localhost:10222/book. We can see the list of books there as same as previously.

Now stop the containers by the command :

docker-compose down

This image has an empty alt attribute; its file name is sp17.png

As you can see now our containers are stopped & removed

Next we will re-run the containers in background ,so use -d flag

docker-compose up -d

we can see our containers are running in background and status shows up

This image has an empty alt attribute; its file name is sp18-1024x145.png


In this session, we learned how to run a Spring Boot Application with MySQL using docker-compose. We discussed how to define multiple services and manage multiple containers using a simple docker-compose file only. Please refer the official docker-compose documentation to learn more and experiment with sample examples

Leave a Reply