Modify the Application.properties file for the Springboot application, and change the MySQL host to the MySQL Docker container name, which is mysql1 consistent with the Docker-compose.yaml file. )
Spring.datasource.url=jdbc:mysql://mysql1:3306/sppanblog?characterencoding=utf8&zerodatetimebehavior= Converttonull
spring.datasource.username=root
spring.datasource.password=123456
Packaged using MVN package under the Springboot application root directory.
Create a D:/docker directory in d:/, which creates blogs and MySQL directories separately.
Create the My.cnf file in the D:/docker/mysql directory as follows:
[MySQL]
Default-character-set=utf8
[mysqld]
Character-set-server=utf8
Create a D:/docker/blog/dockerfile file that reads as follows:
#基于哪个镜像 from
java:8
# to mount the local folder to the current container
volume/tmp
# Copy files to the container, or write directly to the add Microservice-discovery-eureka-0.0.1-snapshot.jar/app.jar
ADD Sppanblog4springboot-0.0.1-snapshot.jar/ App.jar
#RUN bash-c ' Touch/app.jar '
# Open 8080 port
expose 8080
# Configure the command to execute after the container starts
entrypoint ["Java", "- Djava.security.egd=file:/dev/./urandom ","-jar ","/app.jar "]
Copy the Application jar package (here Sppanblog4springboot-0.0.1-snapshot.jar) to the D:/docker/blog directory.
Create a D:/docker/docker-compose.yaml file that reads as follows:
MYSQL1:
image:mysql:5.7
volumes:
-d:/docker/mysql/my.cnf:/etc/mysql/my.cnf MySQL
-d:/data/ Mysql:/var/lib/mysql
Environment:
-mysql_root_password=123456
ports:
-"3306:3306"
Expose:
-"3306"
Blog: Build
:./blog
Ports:
-"8080:8080" #端口映射
expose:
-8080 #暴露容器端口
Links:
-MYSQL1
Go to cmd and execute the following command
D:
cd D:/docker
docker-compose up
Then wait for the image to be automatically built, the MySQL should start after startup, but the application should not start, because the corresponding database was not found.
Connect localhost MySQL with navicat (or other database Tools), account root, Password: 123456, and then create the Sppanblog database.
Perform Docker PS, then execute Docker stop and close the container for MySQL.
Execute docker-compose up again and should be able to perform the application.
Then connect MySQL via navicat, import initial data (Import.sql).
All the code has been uploaded to the code cloud. Https://gitee.com/39627020/sppanblog4springboot