The problem
During the development of SpringBoot microservices, it is often the case that the microservices need to be developed using Windows, but the microservices will eventually be deployed on Linux, resulting in a discrepancy between the development environment and the production environment, which will lead to some problems after the launch
The solution
Maven in Docker can detect code changes by mounting the source directory to the Docker and running MVN Spring-boot :run in Doker. Since Docker and Windows share the same directory, Maven in Docker can detect code changes. Automatically restarts the SpringBoot service
- Assume your Spring code works
mvn spring-boot:run
Normal operation. To enable Maven to detect code changes to restart the service, make sure thatpom.xml
Add a dependency to:spring-boot-devtools
- Suppose your code directory is in
C:\Project\springProject
In the. Please replace the directory in the following command with your own project directory - Create a new Docker directory and create a new file in the directory
Dockerfile
FROM maven:3.5.3-jdk-8-alpine
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
VOLUME /app/src
CMD ["mvn"."spring-boot:run"]
Copy the code
- Enter the Docker directory, compile and generate the Docker Image
docker build -t app:dev .
- Running Docker.
docker run -v d:/Project/demo/src:/app/src app:dev
reference
- medium.com/@nieldw/cac…
- hub.docker.com/_/maven