title: Spring add enviornment variable from docker
body: <p>I'm new to Spring and Docker.</p>
  
  <p>I created a project by using the default template provided by Gitlab.
  It comes with a predefined Dockerfile on which I would like to add some extra environment variables to be used inside my <code>application.properties</code>.</p>
  
  <p>What I've done so far is adding the <code>MY_PROP</code> ENV value inside the DockerFile like this:</p>
  
  <pre><code>FROM maven:3-jdk-8-alpine
  
  WORKDIR /usr/src/app
  
  COPY . /usr/src/app
  RUN mvn package
  
  ENV PORT 5000
  ENV MY_PROP testpropertyfromdocker
  EXPOSE $PORT
  CMD [ "sh", "-c", "mvn -Dserver.port=${PORT} spring-boot:run" ]
  
  </code></pre>
  
  <p>Set my <code>application.properties</code> like this: </p>
  
  <pre><code>test.prop=${MY_PROP}
  </code></pre>
  
  <p>And try to use it in a basic <code>RestControntroller</code>:</p>
  
  <pre><code>@SpringBootApplication
  @RestController
  public class DemoApplication {
  
      @Value("${test.prop}")
      private String testProp;
  
      @GetMapping("/")
      String home() {
          return "My prop from docker: " + testProp;
      }
  
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
  }
  </code></pre>
  
  <p>But if I try to build my Docker image with </p>
  
  <pre><code>docker build -t demo .
  </code></pre>
  
  <p>It fails on </p>
  
  <pre><code>java.lang.IllegalArgumentException: Could not resolve placeholder 'MY_PROP' in value "${MY_PROP}"
  </code></pre>
  
  <p>I've also tried to run the project directly in IntelliJ without building the Docker image but got the same error even when adding a <code>.env</code> file.</p>
  
  <p>How do I pass my environment variable to my Spring application ?
  Is it possible to have a <code>.env</code> file or similar to use when debugging inside the IDE or do I have to build the image to be able to run the project ?</p>
  
  <p>PS: I don't think this is a duplicate of <a href="https://stackoverflow.com/q/46057625/3520621">Externalising Spring Boot properties when deploying to Docker</a> or at least the answer should be more complete as I didn't manage to have my configuration working as expected.</p>
  
  <p><strong>EDIT</strong>
  I updated my <code>pom.xml</code> to include this:</p>
  
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.2.2.RELEASE&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;environmentVariables&gt;
                      &lt;MY_PROP&gt;testfromappprops&lt;/MY_PROP&gt;
                  &lt;/environmentVariables&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;  
  </code></pre>
  
  <p>I made a clean and build and resync maven project but it is still failing even when running through the IDE.</p>
  
  <p><strong>EDIT 2</strong>
  I update dmy DockerFile to use java -jar:</p>
  
  <pre><code>FROM openjdk:8-jdk-alpine
  VOLUME /tmp
  ARG JAR_FILE
  COPY ${JAR_FILE} app.jar
  ENV MY_PROP testpropertyfromdocker
  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  </code></pre>
  
  <p>The <code>docker build -t demo .</code> command line is working but the <code>docker run demo</code> is giving the following output:</p>
  
  <pre><code>Error: Invalid or corrupt jarfile /app.jar
  </code></pre>
  
  <p>Also, when running from the IDE (the "run" button), I still have same error about environment variable not found.</p>
  
  <p>Thanks.</p>