This post demonstrates a Spring Boot Mysql Docker Compose Example. You will learn how to use Docker-compose to dockerize a Spring Boot Restful API application. Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you use a YAML file to configure your all application’s services. Let’s dig deeper.
Spring Boot Mysql Docker Compose Example
Install Docker-Compose
first, install the Docker for your OS as described on the Get Docker page.
To install docker-compose on Linux Mint (Ubuntu) please follow instructions: https://docs.docker.com/compose/install/#install-compose
Post-installation do check the docker-compose version
1 |
$ docker-compose --version |
Working with Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you use a YAML file to configure your all application’s services. Then, with a single command, you create and start all the services from your configuration. That YAML file is called docker-compose.yml
. For example, to run a spring boot application, you could use the docker-compose.yml
file with the configuration like this:
1 2 3 4 5 6 7 8 |
springboot-mysql-app: restart: always container_name: springboot-mysql-app build: ./springboot-mysql-docker-compose working_dir: /sspringboot-mysql-docker-compose ports: - "8080:8080" command: mvn clean spring-boot:run |
The above configuration creates a specific container and it exposes a specific port. To run that container, you just hit the command docker-compose up
then access the application through http://localhost:8080.
In reality, our application usually connects to third-parties, e.g: MySQL, ActiveMQ,…Now we create a container for MySQL and link it with springboot-mysql-app container above in the same docker-compose.yml
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
version: '3' services: mysql-db: container_name: mysql-db image: "mysql/mysql-server:5.7" ports: - "3306:3306" environment: - MYSQL_ONETIME_PASSWORD=true - MYSQL_USER=demo - MYSQL_PASSWORD=demo - MYSQL_ROOT_PASSWORD=root - MYSQL_DB=dockerdemo springboot-mysql-app: container_name: springboot-mysql-app image: springboot-mysql-app:latest build: context: ./springboot-mysql-docker-compose dockerfile: Dockerfile working_dir: /springboot-mysql-docker-compose restart: on-failure ports: - "8088:8080" depends_on: - mysql-db environment: SPRING_DATASOURCE_USERNAME: demo SPRING_DATASOURCE_PASSWORD: demo SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db/dockerdemo?useSSL=false&allowPublicKeyRetrieval=true command: mvn clean spring-boot:run |
In the above configuration, we create a MySQL container like a database. You see the depends_on
property, this property express dependency between services and makes sure that Docker will start MySQL container before springboot-mysql-app container. Moreover, you can access the container via its name, e.g jdbc url look like : jdbc:mysql://mysql-db/dockerdemo
.
Store the sensitive data in a separated file
Docker Compose supports declaring default environment variables in an environment file named .env
placed in the folder where the docker-compose
command is executed (current working directory). We will create a .env
file that stores database account like this
1 2 3 4 |
MYSQL_USER=demo MYSQL_PASSWORD=demo MYSQL_ROOT_PASSWORD=root MYSQL_DB=dockerdemo |
Now, we update the configuration file using .env
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
version: '3' services: mysql-db: container_name: mysql-db env_file: - .env image: "mysql/mysql-server:5.7" ports: - "3306:3306" environment: - MYSQL_ONETIME_PASSWORD=true springboot-mysql-app: container_name: springboot-mysql-app image: springboot-mysql-app:latest build: context: ./springboot-mysql-docker-compose dockerfile: Dockerfile working_dir: /springboot-mysql-docker-compose restart: on-failure ports: - "8088:8080" depends_on: - mysql-db environment: SPRING_DATASOURCE_USERNAME: ${MYSQL_USER} SPRING_DATASOURCE_PASSWORD: ${MYSQL_PASSWORD} SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db/${MYSQL_DB}?useSSL=false&allowPublicKeyRetrieval=true command: mvn clean spring-boot:run |
Manage data in Docker
When you run the docker-compose up
command, Docker will destroy your all containers and recreate it, so your current data will be lost. Don’t worry, you can fix this issue by mapping a specific volume in your docker-compose.yml
file. Here is a sample with MySQL container
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
version: '3' services: mysql-db: container_name: mysql-db env_file: - .env image: "mysql/mysql-server:5.7" ports: - "3307:3306" networks: springboot-mysql-network: aliases: - mysql-db environment: - MYSQL_ONETIME_PASSWORD=true volumes: - ./mysql-data:/var/lib/mysql57:rw - ./mysql-init:/docker-entrypoint-initdb.d/:ro |
In this case, your database will be stored in the folder /mysql-data
in your host machine.
Spring Boot Mysql Docker Compose Example
Here, we will dockerize a Spring Boot Restful API application from the project source code Spring Boot JPA MySQL CRUD Example, the project structure like below
Docker Compose working folder structure
We create a working folder name docker-compose, it contains the docker-compose.yml
file, .env
file, other folders:
mysql-data
(store MySQL database files) folder.
mysql-init
folder stores our SQL script for initializing database, this script:
1 2 3 4 |
DROP DATABASE IF EXISTS dockerdemo; CREATE DATABASE dockerdemo; GRANT ALL PRIVILEGES ON dockerdemo.* TO 'demo'@'%' IDENTIFIED BY 'demo'; GRANT ALL PRIVILEGES ON dockerdemo.* TO 'demo'@'localhost' IDENTIFIED BY 'demo'; |
springboot-mysql-docker-compose
is project folder of our Spring Boot Restful API application
Dockerize application uses Docker compose
Open the console and go into the folder docker-compose
then run the command like below
This docker-compose build
command builds our containers that are defined in the docker-compose.yml
file.
Now, we run our application by running the docker-compose up command, you will see the output like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
springboot-mysql-app | [INFO] Attaching agents: [] springboot-mysql-app | springboot-mysql-app | . ____ _ __ _ _ springboot-mysql-app | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ springboot-mysql-app | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ springboot-mysql-app | \\/ ___)| |_)| | | | | || (_| | ) ) ) ) springboot-mysql-app | ' |____| .__|_| |_|_| |_\__, | / / / / springboot-mysql-app | =========|_|==============|___/=/_/_/_/ springboot-mysql-app | :: Spring Boot :: (v2.2.2.RELEASE) springboot-mysql-app | springboot-mysql-app ...... springboot-mysql-app | 2020-02-17 08:48:57.163 INFO 69 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... springboot-mysql-app | 2020-02-17 08:48:57.165 WARN 69 --- [ restartedMain] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation. springboot-mysql-app | 2020-02-17 08:48:57.533 INFO 69 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. springboot-mysql-app | 2020-02-17 08:48:57.574 INFO 69 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect springboot-mysql-app | 2020-02-17 08:49:02.415 INFO 69 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] springboot-mysql-app | 2020-02-17 08:49:02.443 INFO 69 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' springboot-mysql-app | 2020-02-17 08:49:02.471 INFO 69 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 springboot-mysql-app | 2020-02-17 08:49:03.596 WARN 69 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning springboot-mysql-app | 2020-02-17 08:49:03.909 INFO 69 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' springboot-mysql-app | 2020-02-17 08:49:05.026 INFO 69 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' springboot-mysql-app | 2020-02-17 08:49:05.033 INFO 69 --- [ restartedMain] com.javabycode.Application : Started Application in 18.97 seconds (JVM running for 19.978) |
So far, our application is deployed successfully on the Docker container. You can verify this Spring Boot Restful application with the Postman tool, like this:
The above image shows that we just created an employee using Restful api of our application.
Next, we query the employee above, like this:
You can verify the employee record above by access directly in the console, please see the below screenshot:
That’s all about Spring Boot Mysql Docker Compose Example.
If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.
References
Docker Compose
Spring Boot Docker using Maven Plugin