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

What you'll need

  • Docker CE

Stack

  • Docker
  • Spring Boot
  • MongoDB
  • 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


The MongoClient bean is auto-configured and provided by Spring Boot

FreeMarker View template


Application Configuration


spring.data.mongodb.host is a common property of Spring Boot auto-configuration for MongoDB

hk-mongodb is the Docker Compose service name of MongoDB as we are going to run this Spring Boot application and MongoDB server inside Docker containers. You can find below the docker-compose.yaml file for more details


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-mongodb-nginx/app