Docker deploys Spring boot + Redis +Mysql

Create a bridged network

# Create a bridge mode network, and specify the subnet range and gateway ip
docker network create --driver bridge --subnet 172.20.21.0/24 --gateway 172.20.21.1 school-exam

install redis

#Run redis, and mount the files in the G:\docker-config-files\SchoolExam\redis.conf directory to /etc/redis.conf
#Execute the network segment and ip used
docker run -itd --name school-redis -v G:\docker-config-files\SchoolExam\redis.conf:/etc/redis.conf --net school-exam -p 6480:6379 --ip 172.20.21.2 redis:3.2.10 /etc/redis.conf
# If you need other docker containers to connect to redis, you need to modify redis.conf
1. Put bind 127.0.0.1 -> bind 172.20.21.2 #172.20.21.2 as the ip address of the redis container
2. Enable requirepass foobared #Can be changed or not
3. Put protected-mode yes -> protected-mode no #Close safe mode

install mysql

#Run mysql, you need to specify MYSQL_ROOT_PASSWORD, otherwise docker cannot start normally
docker run -itd --name school-mysql -e  MYSQL_ROOT_PASSWORD=root --net school-exam -p 6481:3306 --ip 172.20.21.3 mysql:5.7
# There is a pit in this place of mysql. I am using version 5.7. This version has ONLY_FULL_GROUP_BY set by default, because there are some group by in the project
# The statement does not write the fields of the whole group, so an error will be reported. Need to change the my.cnf file
# Copy the my.cnf file on the school-mysql container to the local
docker cp school-mysql:/etc/my.cnf G:\docker-config-files\SchoolExam
# Add the following text under [mysqld]:
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#Re-upload the my.cnf file to the container, and restart the mysql container
docker cp G:\docker-config-files\SchoolExam\my.cnf school-mysql:/etc

Execute mvn package on Idea to package the spring boot project into a jar package and create a Dockerfile

Note: If there are jsp files in the spring boot project, the version of spring-boot-maven-plugin needs to be reduced to 1.4.2.RELEASE, otherwise 404 will be reported. And package the jsp file to a specific path when packaging, as follows:

 

The content of the Dockerfile file is as follows:

# The base image that this image needs to depend on
FROM java:8
# Copy the jar package and configuration file in the current directory to the /conf directory of the docker container. This path depends on your own situation
COPY exam-web-2.0.jar /exam-web-2.0.jar
COPY application.yml /conf/application.yml
# Declare that the service runs on port 8031
EXPOSE 8031
# Specify the jar package to run when the docker container starts, and specify the configuration file
ENTRYPOINT ["java", "-jar","/exam-web-2.0.jar","--spring.config.location=/conf/application.yml"]

Create project mirror

# To create an image file, you need to switch to the directory where the Dockerfile is located and execute it, and there must be an application.yml file in the directory
docker build -t school-exam:2.0 .
# start mirroring
# -v file mount, if you need to modify the configuration file, you only need to modify the G:\Lyy_Study\conf directory
docker run -p 8031:8031 -v  G:\Lyy_Study\conf:/conf  --net school-exam --ip 172.20.21.4 --name school-web school-exam:2.0

Note: Modify the redis and mysql connection in the application.yml file, and change it to the ip of the container where redis is located and the ip of the container where mysql is located.

Guess you like

Origin blog.csdn.net/liyayou/article/details/126163625