This tutorial walks you through the steps of creating a simple Spring Boot web application with NGINX and MySQL running inside Docker containers

What you'll need

  • Docker CE

Stack

  • Docker
  • Spring Boot
  • MySQL
  • NGINX
  • Maven

Init project structure and dependencies

Project structure

├── app
│   ├── src
│   │   └── main
│   │       ├── java
│   │       │   └── com
│   │       │       └── hellokoding
│   │       │           └── springboot
│   │       │               ├── IndexController.java
│   │       │               └── WebApplication.java
│   │       └── resources
│   │           ├── static
│   │           │   ├── css
│   │           │   │   └── main.css
│   │           │   └── js
│   │           │       └── main.js
│   │           ├── templates
│   │           │   └── index.ftl
│   │           └── application.properties
│   ├── Dockerfile
│   └── pom.xml
├── nginx
│   └── conf.d
│       └── app.conf
└── docker-compose.yaml

Application dependencies


Define Controller, View Template and Config

Controller


FreeMarker View template


Application Configurations


spring.datasource.* are common properties of Spring Boot Data Source auto-configuration for MySQL

In the datasource url property, hk-mysql is the Docker Compose service name of MySQL as we're going to run this Spring Boot application and MySQL server in the Docker containers. You can find it in the docker-compose.yaml file below


Dockerize

Dockerfile of Spring Boot web application


NGINX config file


Docker Compose


Test

  • Run command docker-compose up
  • Access to http://localhost

Source code

https://github.com/hellokoding/hellokoding-courses/blob/master/docker-examples/dockercompose-springboot-mysql-nginx