[JAVA] Launch Nginx + Spring Boot application with docker-compose

Overview

Create the following Dockerfile and start it from docker-compose.

  1. Forward the request to port 80 of the host to port 8080 of the Nginx container
  2. Forward the request to Nginx to port 8090 of the Spring Boot container

What to use

The version is also listed. It's not that you can't do it without this version.

Directory structure

Only the items required this time are listed.

--Repository root - docker - docker-nginx - Dockerfile - nginx.conf - docker-springboot - Dockerfile - docker-springboot-local - Dockerfile - docker-compose.yml - gradlew - gradlew.bat

Create various Dockerfiles

Nginx

Start Nginx Dockerfile

docker/docker-nginx/Dockerfile


FROM nginx:1.15.0

ADD nginx.conf /etc/nginx/conf.d/nginx.conf

Nginx config file

Not a Dockerfile, but one used by scripts in the Dockerfile.

docker/docker-nginx/nginx.conf


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

  location / {
    proxy_pass http://app:8090;
    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;
  }
}

I don't know if this is the best. There is no HTTPS, and you need to add what your application needs here.

The app of app: 8090 is the Spring Boot container of docker-compose.yml described later.

Spring Boot

Spring Boot startup Dockerfile (clone from Git and start)

docker/docker-springboot/Dockerfile


FROM openjdk:10.0.1-jdk

RUN git clone https://github.com/xxxx/repo_name.git
WORKDIR ./repo_name

RUN ./gradlew --full-stacktrace -q build

CMD ./gradlew bootRun

Spring Boot startup Dockerfile (start by referring to local source)

docker/docker-springboot-local/Dockerfile


FROM openjdk:10.0.1-jdk

Since the necessary settings are specified from docker-compose.yml, the result is only FROM.

Create docker-compose.yml file

docker-compose.yml


version: '3'

services:

  nginx:
    container_name: container_nginx
    build: docker/docker-nginx
    restart: always
    ports:
      - 80:8080
    hostname: container_nginx_hostname
    links:
      - app

  app:
    container_name: container_springboot
    restart: always
    build: docker/docker-springboot
    expose:
      - 8090
    command: ./gradlew bootRun
    hostname: container_springboot_hostname

#Used when launching quickly locally
#  app:
#    container_name: container_springboot-local
#    restart: always
#    build: docker/docker-springboot-local
#    expose:
#      - 8090
#    command: ./gradlew bootRun
#    hostname: container_springboot-local_hostname
#    working_dir: /container_springboot
#    volumes:
#      - ./:/container_springboot

Run

If you start it with docker-comose, you can execute the API on localhost / application path.

Summary

Since it was written roughly so as not to forget the contents of Dockerfile and docker-compose.yml, there is no detailed explanation, but I was able to accept the HTTP request with Nginx and send it to Spring Boot above.

Recommended Posts

Launch Nginx + Spring Boot application with docker-compose
Inquiry application creation with Spring Boot
Processing at application startup with Spring Boot
Start web application development with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Spring Boot 2.3 Application Availability
Download with Spring Boot
Configure Spring Boot application with maven multi module
Generate barcode with Spring Boot
Hello World with Spring Boot
Try using OpenID Connect with Keycloak (Spring Boot application)
Get started with Spring boot
Hello World with Spring Boot!
[Spring Boot] Web application creation
Run LIFF with Spring Boot
SNS login with Spring Boot
CICS-Run Java application-(4) Spring Boot application
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Nginx + Spring Boot parrot return LineBot made with VPS
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Spring Boot application that specifies DB connection settings with parameters
Use Basic Authentication with Spring Boot
Spring Boot application development in Eclipse
Spring Boot application code review points
gRPC on Spring Boot with grpc-spring-boot-starter
Hot deploy with Spring Boot development
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Implement Spring Boot application in Gradle
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Let's make a book management web application with Spring Boot part1
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
[Beginner] Let's write REST API of Todo application with Spring Boot
Launch (old) Spring Boot project in IntelliJ
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
[Among Us] Launch automuteus v4.0.4 with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
CI / CD Spring application with CircleCI (Heroku)
Implement paging function with Spring Boot + Thymeleaf