Use dockercompose to build springboot mysql nginx applications

  • 2021-01-18 06:46:01
  • OfStack

The previous article used docker to build the spring-boot application by building the compiled jar package into an image.

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

This is just a note of what you did, and the full running code is shown in Reference 1 in the "Reference" section.
I modified the mysql mapping directory and got the remote ip method.

Main steps:

Build a simple springboot application Applications added under docker support Write the dockercompose configuration file Practice running

Build a simple springboot application

Make an web application and count the number of ip visits to the site.

It is stored in an mysql database, which is accessed as jpa.

Rely on


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

web, jpa mysql, tset library


<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>

The configuration file


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

The 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 a little 
}

Repository layer code refer to jpa related content.

Open the local database, the password is, of the above configuration using mvn spring - boot: run to run after, can see the number of ip, after each statistics is the increase.

dockercompose 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

This configuration file is mainly explained and related configuration is added to the file system.

There are 3 services nginx,mysql and app under services.
images specifies the use of mirroring. nginx and mysql are directly taken from the docker warehouse.
The image is not specified in app, but the directory in which Dockerfile resides is specified using build.
volumes specifies the mapping of files in the local directory to the container's destination address.
environment configures the environment variables required by the container
ports configures ports that map locally to the container, with the local port first and the container port second

What the volumes configuration does is to overwrite the nginx configuration file we wrote directly to the default nginx configuration file in the container.

volumes: Map mysql data files to the local mysqldata directory. When the container is deleted, the data is still there.

The role of the volumes configuration under app: Line 1 maps the code file to the container. Line 2 maps the maven repository file to the local. After the container is removed, build again without re-downloading the 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 profiles of docker.

So we're going to add the file

Dockerfile: Create a new file, add 1 line FROM: maven:3.5-jdk-8 docker profiles: reproduction application. properties for application - docker. properties, and application - docker. Address of the database connection in the properties jdbc instead: 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 of validation

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


Related articles: