Use dockercompose to build springboot-mysql-nginx application

Use dockercompose to build springboot-mysql-nginx application

In the previous article, we used Docker to build a spring-boot application, which built the compiled jar package into the image.

This article runs spring-boot together with the database as a set of docker services.

Here I just record my own operations. For the complete running code, please see the content in reference 1 in "Reference".
(I modified the mysql mapping directory and obtained the remote ip method)

Main steps:

  • Build a simple springboot application
  • Add Docker support to the application
  • Write Docker Compose configuration file
  • Practice operation

Build a simple springboot application

Make a web application to count the number of IP addresses that visit the site.

And store it in mysql database, here we use jpa to access the database.

rely

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.0.RELEASE</version>
</parent>

Dependencies of web, jpa, mysql, and tset libraries

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

Configuration Files

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

Core code

@RestController
public class VisitorController {
 @Autowired
 private VisitorRepository repository;
 @RequestMapping("/")
 public String index(HttpServletRequest request)
 {
 String ip = request.getHeader("X-Real-IP");
 if(ip== null || "".equals(ip))
 {
  ip = request.getRemoteAddr();
 }
 Visitor visitor = repository.findByIp(ip);
 if(visitor == null)
 {
  visitor = new Visitor();
  visitor.setIp(ip);
  visitor.setTimes(1L);
 }
 else
 {
  visitor.setTimes(visitor.getTimes()+1);
 }
 repository.save(visitor);
 return "ip:"+visitor.getIp()+" "+visitor.getTimes()+" times.";
 }
}

Entity Class

@Entity
public class Visitor {
 @Id
 @GeneratedValue
 private Long id;
 @Column(nullable=false)
 private Long times;
 @Column(nullable=false)
 private String ip;
 // get, set methods omitted}

Repository layer code refers to jpa related content.

The local database is opened, and the password is configured above. After running with mvn spring-boot:run, you can see the number of IPs, which increases automatically after each statistics.

Docker Compose Configuration File

Create a new docker-compose.yaml file as follows:

version: '3'
services:
 nginx:
  container_name: v-nginx
  image: nginx:1.13
  restart: always
  ports:
  - 80:80
  -443:443
  volumes:
  - ./nginx/conf.d:/etc/nginx/conf.d
 mysql:
  container_name: v-mysql
  image:mysql/mysql-server:5.7
  environment:
  MYSQL_DATABASE: test
  MYSQL_ROOT_PASSWORD: root
  MYSQL_ROOT_HOST: '%'
  ports:
  - "3306:3306"
  volumes:
  - ./mysqldata:/var/lib/mysql
  restart: always
  
 app:
  restart: always
  build: ./app
  working_dir: /app
  volumes:
   - ./app:/app
   - ~/.m2:/root/.m2
  expose:
   - "8080"
  depends_on:
   - nginx
   -mysql
  command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker

Mainly explain this configuration file and add related configuration in the file system.

There are three services under services: nginx, mysql, and app.
images specifies to use images. Nginx and mysql are directly taken from the docker repository.
The image is not specified in the app, but the directory where the Dockerfile is located is specified with build.
Volumes specifies the mapping between files in local directories and container target addresses.
environment configures the environment variables required by the container
ports configures the local and container mapping ports, with the local port in front and the container port in the back

The purpose of the volumes configuration under nginx is to directly overwrite the nginx configuration file we wrote to the default nginx configuration file in the container.

The purpose of the volumes configuration under MySQL is to map the MySQL data files to the local mysqldata directory. When the container is deleted, the data remains.

The function of the volumes configuration under app: The first line maps the code file to the container. The second line maps the Maven repository file to the local one. After deleting the container, you can build it again without re-downloading dependent packages.

command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker

The command is to compile and run the project in the container, using Docker profiles.

So we have to add the file

  • Dockerfile: Create a new file and add a line FROM maven:3.5-jdk-8
  • Docker profiles: Copy application.properties to application-docker.properties, and change the database connection address in application-docker.properties to jdbc:mysql://mysql:3306/test.
  • nginx configuration file
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;
  }
}

Deployment Validation

Copy the entire file to the server and run it using docker-compose up .

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker compose deploys SpringBoot project to connect to MySQL and the pitfalls encountered
  • Implementation of docker-compose deployment project based on MySQL8
  • How to run MySQL using docker-compose
  • A brief analysis of the problem of mysql being inaccessible when deployed with docker-compose
  • How to build an elk system using docker compose
  • Detailed process of building mongodb and mysql with docker-compose

<<:  Briefly describe the difference between Redis and MySQL

>>:  js implements the pop-up login box by clicking the pop-up window

Recommend

How to add indexes to MySQL

Here is a brief introduction to indexes: The purp...

JavaScript Sandbox Exploration

Table of contents 1. Scenario 2. Basic functions ...

A few things you need to know about responsive layout

1. Introduction Responsive Web design allows a we...

Detailed explanation of deploying MySQL using Docker (data persistence)

This article briefly describes how to use Docker ...

XHTML Web Page Tutorial

<br />This article is mainly to let beginner...

Json advantages and disadvantages and usage introduction

Table of contents 1. What is JSON 1.1 Array liter...

Four data type judgment methods in JS

Table of contents 1. typeof 2. instanceof 3. Cons...

Building an image server with FastDFS under Linux

Table of contents Server Planning 1. Install syst...

html page!--[if IE]...![endif]--Detailed introduction to usage

Copy code The code is as follows: <!--[if IE]&...

Detailed explanation of how to configure openGauss database in docker

For Windows User Using openGauss in Docker Pull t...

Things about installing Homebrew on Mac

Recently, Xiao Ming just bought a new Mac and wan...

Summary of Vue's cross-domain problem handling and solutions

When you send a network request, the following sa...

Eight examples of how Vue implements component communication

Table of contents 1. Props parent component ---&g...