Dockerize your SpringBoot + MySQL Application

In this article, I will discuss how to dockerize your Spring Boot application and MySQL database in separate containers. The Spring Boot application I have used in this article can be found here. It is a simple CRUD application of employee management system which enables to view, add, update and delete employees.

The first step is to create a MySQL container.

docker pull mysql:[version]

You can simply do that using an official image available at docker hub. You can pull images with different versions. ( [version] = latest / 8.0 / 5.7 / 5.6 etc ).

Then we can create a container using that image with different environment variable values.

docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:[version]

The name given for the MySQL container is “mysql-standalone”. The root password is set to “password”. And the database name is set to “test”. Another user is created with the username of “sa” and the password is “password”. The image we are using to build the container is mysql:[version].

After creating the container we can list the containers using,

docker container ls

Then it will show that “mysql-standalone” container is running. You can see the container ID, image and other data related to the container.

The next step is to dockerize the spring boot application. For that first go to the and add the data source related data.


To name the jar created at the build phase, go to the pom.xml and make relevant changes.


The build the project with skipping tests, execute

mvn clean install -Dmaven.test.skip=true

It will create a jar in the target directory with the name of “demo-application.jar”.

To package this as a docker image, create a Dockerfile in the project’s directory and add the required configuration.

FROM openjdk:8
ADD target/demo-application.jar demo-application.jar
ENTRYPOINT [“java”, “-jar”, “demo-application.jar”]

Since spring boot requires jdk , I have packaged it inside the container. Then ADD the jar using the existing location and rename it to demo-application.jar. Then EXPOSE the port number 8086 of the container to communicate with the host. The last one is the ENTRYPOINT which is the command to run. To build the docker image, go to the projects directory and run,

docker build . -t demo-application

The image will have a tag demo-application. If you run “docker images” in the console, then you will be able to see the image with “demo-application”. To run the application in the docker container,

docker run -p 8086:8086 --name demo-application --link mysql-standalone:mysql -d demo-application

“-p” is used to match the port numbers of the host with the containers. So here the first 8086 is the host port which is mapped with the containers port of 8086. The container is named as demo-application and linked with the mysql-standalone container. The image used to create the container is the “demo-application”. Then running the command of,

docker container ls

will show you both containers.

To view the logs,

docker logs [container-name]

Then go to your browser and type “localhost:8086/” will let you access the application.



Former Software Engineering Intern @ WSO2 | MIT Undergraduate @ University of Kelaniya, Sri Lanka.