HelloKoding

Practical coding guides

Spring Boot with Docker Compose and NGINX

This tutorial will walk you through the steps of dockerizing a simple Spring Boot web application with NGINX proxy server

What you’ll need

  • Docker CE

Stack

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

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>com.hellokoding</artifactId>
    <name>docker-springboot</name>
    <description>docker-springboot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Define Controller, View Template and Config

Controller

IndexController.java

package com.hellokoding.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController {
    @GetMapping("/")
    public String index() {
        return "index";
    }
}

FreeMarker View template

index.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Docker and Spring Boot!</title>
</head>
<body>
    <h1>Hello Docker and Spring Boot!</h1>
</body>
</html>

Static files

main.js

(function(){
    console.log("Hello World!");
})();

main.css

.hello-title{
    color: darkgreen;
}

Application Configuration

application.properties

spring.freemarker.template-loader-path: classpath:/templates
spring.freemarker.suffix: .ftl

WebApplication.java

package com.hellokoding.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

Dockerize

Dockerfile of Spring Boot web application

Dockerfile

FROM maven:3.5-jdk-8

NGINX config file

app.conf

server {
    listen 80;
    charset utf-8;
    access_log off;

    location / {
        proxy_pass http://app:8080;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static {
        access_log   off;
        expires      30d;

        alias /app/static;
    }
}

Docker Compose

docker-compose.yaml

version: '3'
services:
  nginx:
   container_name: some-nginx
   image: nginx:1.13
   restart: always
   ports:
   - 80:80
   - 443:443
   volumes:
   - ./nginx/conf.d:/etc/nginx/conf.d
   depends_on:
   - app

  app:
    restart: always
    build: ./app
    working_dir: /app
    volumes:
      - ./app:/app
    expose:
      - "8080"
    command: mvn clean spring-boot:run

Test

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

Source code

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

Follow HelloKoding