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 worksmvn spring-boot:runNormal operation. To enable Maven to detect code changes to restart the service, make sure thatpom.xmlAdd a dependency to:spring-boot-devtools
  • Suppose your code directory is inC:\Project\springProjectIn 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 directoryDockerfile
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 Imagedocker build -t app:dev .
  • Running Docker.docker run -v d:/Project/demo/src:/app/src app:dev

reference

  1. medium.com/@nieldw/cac…
  2. hub.docker.com/_/maven