The project address

Github.com/117503445/s…

The target

After debugging locally, Commit the code to the repository, and docker images are built automatically and then deployed automatically on the production server. At the same time, pay attention to the security of configuration files.

process

Submit code -> The Docker Hub Automated service detects the submission on Github and builds an image from the Dockerfile -> WatchTower on the production server detects an update to the Docker Hub and automatically updates the local image.

Configuration file transfer roadmap

Because both Github repositories and Docker Hub images are public, you cannot store configuration files there. Therefore, if the configuration is passed through the Docker run, security can be ensured.

Method of use

Production server

Run the image using the following code

docker rm spring_boot_docker -f
docker rmi 117503445/spring_boot_docker
docker run --name spring_boot_docker -d -e var1="var 1" -e var2="var 2" -p 80:80 --restart=always 117503445/spring_boot_docker:latest
Copy the code

Configure WatchTower to enable automatic updating (the following code automatically updates all Docker images)

docker run -d \
    --name watchtower \
    -v /var/run/docker.sock:/var/run/docker.sock \
    containrrr/watchtower
Copy the code

Local debugging

Configure it in the application.properties file and run it as usual

Dockerfile parsing

FROM maven:3.6.3-openjdk-14 AS MAVEN_BUILD
COPY pom.xml /build/
COPY src /build/src/
WORKDIR /build/
RUN mvn package -Dmaven.test.skip=true
FROM openjdk:14-alpine
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/spring_boot_docker.jar /app/
ENV var1="" var2=""
EXPOSE 80
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/urandom"."-jar"."spring_boot_docker.jar"."--env.var1=${var1}"."--env.var2=${var2}"]
Copy the code

A layered build is used, spring_boot_docker.jar is constructed by MVN package in MAVEN_BUILD layer, and then run in OpenJDK layer. If you need to modify the configuration file structure later, you also need to modify the Dockerfile.

Pom. XML parsing

Through a wave of operations, realizing Spring Boot to read the version number and compile time stamp function, and setting finalName to redefine the generated JAR file name, without the version number, convenient Dockerfile implementation.