spring boot 构建docker镜像,运行指定spring profile

章彬郁
2023-12-01

#本文基于maven构建的spring boot项目,打包docker镜像需要安装docker

1、pom.xml添加docker打包插件

 <plugin>
	 <groupId>com.spotify</groupId>
	 <artifactId>dockerfile-maven-plugin</artifactId>
	 <version>1.3.6</version>
	 <configuration>
		 <repository>${project.artifactId}</repository>
		 <buildArgs>
	 		<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
		 </buildArgs>
	 </configuration>
 </plugin>

2、在src同级目录下创建Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

3、构建步骤

3.1 编译打包成jar包 mvn package -Dmaven.test.skip=true
3.2 将jar包打包成docker镜像:mvn dockerfile:build

4、运行命令:

docker run -d \
    -p 8001:8001 \
    -e "SPRING_PROFILES_ACTIVE=dev" \
    --name auth \
    -v /tmp/cloud-ac-service/auth/:/var/log/cloud-ac-service/auth \
    auth:latest
4.1 命令说明
使用 -e "SPRING_PROFILES_ACTIVE=dev"  指定spring profile
使用 -v /tmp/cloud-ac-service/auth/:/var/log/cloud-ac-service/auth  挂载日志目录

转载于:https://my.oschina.net/u/3971841/blog/2056302

 类似资料: