DEV Community

Cover image for Run MySQL on Docker and use in your Java App
Sandro Giacomozzi
Sandro Giacomozzi

Posted on • Updated on

Run MySQL on Docker and use in your Java App

Running MySQL in Docker is very useful to use in development. In this tutorial, we will see how easy it is to start MySQL in Docker and use it in our Java application with Spring Boot.


First, you need a docker runtime on you machine. If you don't have it, please install following this docker documentation:

Start Mysql container

To start the container, type the command below in your terminal:

docker run --name mysql57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql/mysql-server:5.7

The command above, pulled image if not found in local repository and run mysql container in detached mode. Configure local port equals to container port and set root password to 1234.

Check your container running:

docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                   PORTS                               NAMES
380eca8a553c        mysql/mysql-server:5.7    "/ mysq…"   8 minutes ago       Up 8 minutes (healthy)>3306/tcp, 33060/tcp   mysql

As we can see, mysql5.7 is running on localhost:3306
At this point, you have a local mysql server without install it.

Note: You will not have external access to the container with root user.

Create a database


docker exec -it mysql57 bash

This command access the mysql container and allow to execute commands on database. Inside a container, type:

mysql -h localhost -u root -p

Remember, the password is 1234. First, we need to create a user for out-of-container access because root access is not allowed:

CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on *.* to 'demo_java'@'%' identified by '1234';

To create a database, paste DDL bellow:

CREATE DATABASE hello_java CHARACTER SET utf8 COLLATE utf8_general_ci;

Next, type:

show databases;
| Database           |
| hello_java         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
5 rows in set (0.00 sec)

Create a database automatically

Another way to start a database and automatic create a hello_java database and demo_java user:

docker run --name mysql57 -p 3306:3306 \
-e MYSQL_USER=demo_java \
-e MYSQL_DATABASE=hello_java \
-d mysql/mysql-server:5.7

With the above command, you do not have to enter the database and create a database and a user. Cool, is not it?

Create Demo App

Go to and create a demo app with the Web and Jpa dependencies.

Import application in your IDE and follow steps below:

1 - Edit file:


spring.jpa.hibernate.ddl-auto=update creates the person table if it doesn't exist.

Note: It's not recommended to use in production.

2 - Edit pom.xml file and add mysql connector java


3 - Add domain, service and persistence classes:



4 - Change main class to create two new Person:

Run Demo App

Start demo app just running a main class:

Application is running and data is persistent in database


To expose your data as a Rest endpoint, just add this dependency:


Access http://localhost:8080/persons

And Wow! Your data exposed as json.

Source code

Discussion (8)

gauravkohirkar profile image
GauravKohirkar • Edited

Great Tutorial !!
I guess I was stuck on the container access issue.
CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on . to 'demo_java'@'%' identified by '1234';
This solved it. Thanks!!
Can you please also do an Enterprize level Spring Boot Running in docker and orchestrated on Kubernetes tutorial? That'd be great.. :)

sandrogiacom profile image
Sandro Giacomozzi Author

Thanks for your feedback. I wrote one serie about this.

franzisk profile image
Francisco Vieira Souza

Hey friend,

Very nice your tutorial.

On the first command (to install mysql on the container), what happens if I have already a mysql local running on 3306 port?

I also would like to know how to create a container with mysql + java + MyApplication.jar, how can I do that?

sandrogiacom profile image
Sandro Giacomozzi Author

Thanks for your feedback. See my other posts about this subject. About port, you received an error. Just change port of left side.

laurebaugnies profile image

Hi ! Thanks for your post, it has been really helpfull ! I have yet a question. How can we save the state of the container in order to reuse it later on a different computer ? How to prevent data loss ?

robson96 profile image

Pode explicar pfv esse trecho de codigo:
De 'demo_java' pro final do codigo nao entende!
CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on . to 'demo_java'@'%' identified by '1234';

O que faz essas tres linhas?

sandrogiacom profile image
Sandro Giacomozzi Author

Ola. Ele cria um usuário no banco e da permissão necessárias para criar as tabelas

sinha123 profile image

Hi friend,
I tried all the three option of jdbc url, but did not work. Request your help.




Just to tell you My Machine is Windows 10 pro.
Spring boot codebase running on my local eclipse.

And I have Docker Desktop installed in my System where I have created Mysql container.

Following error comes when I Run as Spring Boot App:

2020-05-23 20:28:02.279 INFO 76 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-05-23 20:28:24.354 ERROR 76 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException( ~[mysql-connector-java-8.0.16.jar:8.0.16]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException( ~[mysql-connector-java-8.0.16.jar:8.0.16]