简体   繁体   English

docker中的SpringBoot环境为null

[英]SpringBoot environment is null in docker

I have a springboot java application with gradle and some environment variables in application.yml : 我有一个带有gradle的springboot java应用程序和application.yml中的一些环境变量:

spring:
  profiles: development
server:
  port: 3100
database:
  connectionString: mongodb://localhost:27017

---

spring:
  profiles: production
server:
  port: 80
database:
  connectionString: prodCS

My dockerfile : 我的dockerfile:

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

I am trying to run the docker locally but my environment returns null properties 我正在尝试在本地运行docker,但是我的环境返回null属性

@Configuration
public class MongoUtilities {
  @Resource
  public Environment env;
  private static String mongoUri;

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }

  @Bean
  public boolean ReadConnectionString() {
    mongoUri = env.getProperty("database.connectionString");
    System.out.println("Loaded connection Uri : " + mongoUri);
    return true;
  }
}

I've had trouble before with the environment variable being null but managed to fix it. 我在环境变量为null之前遇到过麻烦,但是设法解决了这个问题。 It now runs perfectly in intellij with flag --spring.profiles.active=development 现在,它在带有标志--spring.profiles.active=development intellij中可以完美运行

I am building using gradlew build docker and starting docker using docker run springio/my-app 我正在使用gradlew build docker docker run springio/my-app进行gradlew build docker并使用docker run springio/my-app

It's all pretty standard, I'm just a beginner to this area. 这都是很标准的,我只是这个领域的初学者。

Any ideas? 有任何想法吗?

Alright, I've manager to fix it by adding my application.yml to the docker in build.gradle: 好的,我已经通过将我的application.yml添加到build.gradle中的docker来修复它:

docker {
  dependsOn build
  name "${project.group}/${bootJar.baseName}"
  files bootJar.archivePath, 'application.yml'
  buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}

Then I had to copy application.yml to the image using the dockerfile 然后我必须使用dockerfile将application.yml复制到映像

COPY application.yml application.yml

Then I set spring's configuration to my application.yml 然后我将spring的配置设置为我的application.yml

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-Dspring.config.location=application.yml", "-jar","/app.jar"]

There are a couple of ways of doing it namely 有两种方法可以做到这一点

  • Passing Spring Profile in Dockerfile 在Dockerfile中传递Spring配置文件

     ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=dev","-jar","/rest-api.jar"] 
  • Passing Spring Profile in Docker run command 在Docker运行命令中传递Spring Profile

      docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name rest-api dockerImage:latest 
  • Passing Spring Profile in DockerCompose 在DockerCompose中传递Spring配置文件

      rest-api: image: rest-api:0.0.1 ports: - "8080:8080" environment: - "SPRING_PROFILES_ACTIVE=dev" 

I have written in detail over here , how you can achieve the above 3 approaches. 我已经在这里详细介绍了如何实现以上三种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM