1. Add maven configuration
<properties> <docker.image.prefix>ms4t</docker.image.prefix> </properties> <build> <finalName>eureka</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </build>
2. Create Dockerfile
FROM openjdk:8-jdk-alpine VOLUME /tmp ARG JAR_FILE COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Parameter explanation:
FROM
Subsequent builds will be based on this image. If multiple images are created in the same Dockerfile, multiple FROM instructions can be used
VOLUME configuration A directory with persistence function. A temporary file is created in the host /var/lib/docker directory and linked to the container’s /tmp. The modification step is optional, but it is necessary if it involves file system applications
. The /tmp directory is used to persist to the Docker data folder, because the embedded Tomcat container used by Spring Boot uses /tmp as the working directory by default
ARG sets the parameters added when compiling the image. ENV is to set the environment variable of the container
COPY: only supports copying local files to the container. There is also ADD which is more powerful but more complicated
ENTRYPOINT Command executed when the container starts
EXPOSE 8080 Exposed image port
##3. Build
mvn install dockerfile:build
4. Start
docker run -d --name 名称 -p 对外端口:容器端口 镜像ID
The above is the detailed content of Analysis of the principles of docker in springboot. For more information, please follow other related articles on the PHP Chinese website!