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

A brief discussion on order reconstruction: MySQL sharding

Table of contents 1. Objectives 2. Environmental ...

Comparative Analysis of IN and Exists in MySQL Statements

Background Recently, when writing SQL statements,...

mysql 8.0.15 winx64 decompression version graphic installation tutorial

Every time after installing the system, I have to...

Detailed explanation of vue-router 4 usage examples

Table of contents 1. Install and create an instan...

How to use html2canvas to convert HTML code into images

Convert code to image using html2canvas is a very...

MySQL 8.0.24 version installation and configuration method graphic tutorial

This article records the installation and configu...

Vue+element ui realizes anchor positioning

This article example shares the specific code of ...

Learn MySQL in a simple way

Preface The database has always been my weak poin...

MySQL Index Detailed Explanation

Table of contents 1. Index Basics 1.1 Introductio...

Analysis of the ideas of implementing vertical tables in two ways in Vue project

Problem Description In our projects, horizontal t...

Detailed explanation of Nginx static file service configuration and optimization

Root directory and index file The root directive ...

CSS3 achieves conic-gradient effect

grammar: background-image: conic-gradient(from an...

How to allow remote access to open ports in Linux

1. Modify the firewall configuration file # vi /e...

Pure CSS to achieve candle melting (water droplets) sample code

Achieve results Implementation ideas The melting ...