Update Spring Boot and Mysql version for react-java-mysql sample

Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
This commit is contained in:
Guillaume Lours 2020-03-17 16:28:17 +01:00
parent 0b40d180de
commit d7d79e436f
11 changed files with 172 additions and 55 deletions

View File

@ -23,17 +23,17 @@ services:
build: backend
...
db:
image: mysql:5.7
image: mysql:8.0.19
...
frontend:
build: frontend
ports:
- 80:9000
- 9000:9000
...
```
The compose file defines an application with three services `frontend`, `backend` and `db`.
When deploying the application, docker-compose maps port 80 of the frontend service container to port 9000 of the host as specified in the file.
Make sure port 80 on the host is not already being in use.
When deploying the application, docker-compose maps port 9000 of the frontend service container to port 9000 of the host as specified in the file.
Make sure port 9000 on the host is not already being in use.
## Deploy with docker-compose
@ -41,7 +41,7 @@ Make sure port 80 on the host is not already being in use.
$ docker-compose up -d
Creating network "react-java-mysql_default" with the default driver
Building backend
Step 1/10 : FROM maven:3.5-jdk-9 AS build
Step 1/17 : FROM maven:3.6.3-jdk-11 AS builder
...
Successfully tagged react-java-mysql_frontend:latest
WARNING: Image for service frontend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
@ -57,11 +57,11 @@ Listing containers must show three containers running and the port mapping as be
$ docker ps
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a63dee74d79e react-java-mysql_backend "java -Djava.securit…" 39 seconds ago Up 37 seconds react-java-mysql_backend_1
6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:80->9000/tcp react-java-mysql_frontend_1
b176b18fbec4 mysql:5.7 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1
6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:9000->9000/tcp react-java-mysql_frontend_1
b176b18fbec4 mysql:8.0.19 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1
```
After the application starts, navigate to `http://localhost:80` in your web browser to get a colorful message.
After the application starts, navigate to `http://localhost:9000` in your web browser to get a colorful message.
```
My New React App
```

View File

@ -1,11 +1,20 @@
FROM maven:3.5-jdk-9 AS build
COPY pom.xml .
RUN mvn --batch-mode dependency:resolve
COPY . .
RUN mvn --batch-mode package
RUN cp target/*jar target/app.jar
FROM maven:3.6.3-jdk-11 AS builder
WORKDIR /workdir/server
COPY pom.xml /workdir/server/pom.xml
RUN mvn dependency:go-offline
FROM openjdk:9-jre
COPY src /workdir/server/src
RUN mvn install
RUN mkdir -p target/depency
WORKDIR /workdir/server/target/dependency
RUN jar -xf ../*.jar
FROM openjdk:11-jre-slim
EXPOSE 8080
VOLUME /tmp
COPY --from=build target/app.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ARG DEPENDENCY=/workdir/server/target/dependency
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"]

View File

@ -14,52 +14,61 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>

View File

@ -1,16 +1,21 @@
package com.company.project.controllers;
import org.springframework.stereotype.Controller;
import com.company.project.entity.Greeting;
import com.company.project.repository.GreetingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Controller
@RestController
public class HomeController {
@Autowired
private GreetingRepository repository;
@GetMapping("/")
public String showHome(String name, Model model) {
return "home";
public Greeting showHome(String name, Model model) {
return repository.findById(1).orElse(new Greeting("Not Found 😕"));
}
}

View File

@ -0,0 +1,61 @@
package com.company.project.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
@Entity
@Table(name = "greetings")
public class Greeting {
@Id
@GeneratedValue
private int id;
@Column(nullable = false)
private String name;
public Greeting() {
}
public Greeting(String name) {
this.name = name;
}
public Greeting(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Greeting greeting = (Greeting) o;
return name.equals(greeting.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}

View File

@ -0,0 +1,9 @@
package com.company.project.repository;
import com.company.project.entity.Greeting;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface GreetingRepository extends CrudRepository<Greeting, Integer> {
}

View File

@ -1 +1,12 @@
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:db}:3306/example
spring.datasource.username=root
spring.datasource.password=${MYSQL_PASSWORD:db-57xsl}
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.schema=classpath:/schema.sql
spring.datasource.continue-on-error=true

View File

@ -0,0 +1 @@
INSERT INTO example.greetings(name) values ('Docker');

View File

@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS example.greetings (
id INTEGER AUTO_INCREMENT,
name varchar(50) NOT NULL,
PRIMARY KEY (id)
);

View File

@ -1,9 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Hello from Docker!</p>
</body>

View File

@ -2,25 +2,41 @@ version: "3.7"
services:
backend:
build: backend
environment:
MYSQL_HOST: db
networks:
- react-spring
- spring-mysql
depends_on:
- db
db:
environment:
MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password
image: mysql:5.7
image: mysql:8.0.19
restart: always
secrets:
- db-password
- db-password
volumes:
- db-data:/var/lib/mysql
- db-data:/var/lib/mysql
networks:
- spring-mysql
frontend:
build: frontend
ports:
- 80:9000
- 9000:9000
volumes:
- ./frontend:/project
- /project/node_modules
- ./frontend:/project
- /project/node_modules
networks:
- react-spring
depends_on:
- backend
volumes:
db-data: {}
secrets:
db-password:
file: db/password.txt
networks:
react-spring: {}
spring-mysql: {}