docker-compose builds Springboot project
Springboot project
Configuration information
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 main function
Count the number of client visits
@RestController
public class VisitorController {
@Autowired
private VisitorRepository repository;
@RequestMapping("/")
public String index(HttpServletRequest request) {
String ip=request.getRemoteAddr();
Visitor visitor=repository.findByIp(ip);
if(visitor==null){
visitor=new Visitor();
visitor.setIp(ip);
visitor.setTimes(1);
}else {
visitor.setTimes(visitor.getTimes()+1);
}
repository.save(visitor);
return "我的IP "+visitor.getIp()+" 已经被访问了"+visitor.getTimes()+" 次了.";
}
}
Dockerfile
FROM maven:3.5-jdk-8
COPY ./settings.xml /usr/share/maven/ref/
Springboot project using maven build, build process because the default is downloaded from the external network jar packets, according to the official website of explanation , custom setting.xml domestic source image file in the directory where Dockerfile and Dockerfile content plus COPY ./settings .xml /usr/share/maven/ref/ is fine.
Settings.xml configuration reference Alibaba open source mirror station
Mysql
Use mysql/mysql-server: 5.7
Web container (Nginx)
Use nginx: 1.13
Configuration information
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;
}
}
The service communication in a group of compose needs to use the name of services for access
docker-compose.yml
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"
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
- ports: Use host ports to map container ports
- expose: expose port
- restart: always means that if the service starts unsuccessfully, it will always try (important)
- volumes: Load the configuration file in the local directory to the container target address
Start/stop service and test
Start service
docker-compose up -d
test
Open the webpage http://ip_addr to
access the test
Close service
docker-compose down
reference
https://github.com/ityouknow/spring-boot-examples
http://www.ityouknow.com/springboot/2018/03/28/dockercompose-springboot-mysql-nginx.html