【发布时间】:2018-10-22 10:46:45
【问题描述】:
我正在尝试为我的 Spring Boot 应用程序创建 Docker 映像。
为了启动应用程序,我需要传递一些参数来在环境之间切换,我在本地使用这个命令:
java -Denv=local -jar my-app-1.0-SNAPSHOT.jar
但使用 Docker,我从未成功传递 env 参数,因此它在容器启动后直接崩溃。
我尝试了ENTRYPOINT ["java", "-Denv=prod","-Djava.security.egd=file:/dev/./urandom","-jar","/flad-rest.jar"] 和[CMD ["-Denv=prod"],但没有任何效果
这是我的 Dockerfile:
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 9091 available to the world outside this container
EXPOSE 9091
# The application's jar file
ARG JAR_FILE=flad-rest/target/flad-rest-1.0-SNAPSHOT-exec.jar
# Add the application's jar to the container
ADD ${JAR_FILE} flad-rest.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/flad-rest.jar"]
【问题讨论】:
-
ENTRYPOINT ["java", "-Denv=prod","-Djava.security.egd=file:/dev/./urandom","-jar","/flad-rest.jar"]必须工作。你确定你试过了吗? -
你说需要在环境之间切换,但你的意思是Spring Profiles?
-
@IvanAracki 是的,我尝试了与您完全相同的方式,但我得到了:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [fr.payet.flad.core.config.CoreConfig]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'env' in value "classpath:datasource-${env}.properties" -
@Boris 在我的情况下我没有使用 Spring Profiles,因为我需要加载某个属性文件。
-
在这种情况下,我建议您考虑使用Multi-profile YAML Documents。
标签: docker spring-boot dockerfile