Ashley
Ashley

Reputation: 1629

Pass environment variable values inside Dockerfile

I am using environment variables under my ECS task def which pull values. The variable name is say encryptor.password I have this also declared in my Dockerfile as ENV variable with some dummy value but at the same time is called at a later entrypoint section something like below :-

ARG pwd
ENV encryptor.password $pwd
# Run the app.jar using the recommended flags per
# https://spring.io/guides/gs/spring-boot-docker/#_containerize_it
ENTRYPOINT ["java","-Dhttp.proxyHost=***",\
"-Dhttps.proxyHost=***","-Dhttp.proxyPort=***",\
"-Dhttps.proxyPort=***","-Djava.net.useSystemProxies=true",\
"-Dhttp.nonProxyHosts=***|/var/run/docker.sock|***|***|***",\
"-Djava.security.egd=file:/dev/./urandom","-Dencryptor.password=${encryptor.password}","-Dspring.profiles.active=dev",\
"-jar","/app/app.jar"]

My understanding is that -Dencryptor.password=${encryptor.password} should actually be replaced by the value that is coming to this dockerfile for the ENV variable encryptor.password from the taskdef when the container starts, but looks like the entrypoint is not picking that value. Am i missing something. How to get Dockerfile to get that value?

Upvotes: 0

Views: 1439

Answers (3)

Ashley
Ashley

Reputation: 1629

The issue was that you need to firstly use shell form of ENTRYPOINT so either have an entrypoint.sh script where you define your commands and arguments and then execute in shell form from ENTRYPOINT or else pass everything as :-

ENTRYPOINT ["sh", "-c", "java ........."]

At the same time make sure the variables parsed in ENTRYPOINT for shell do not use dots. Dots are not valid shell identifiers and very commonly overlooked.

Upvotes: 0

Adiii
Adiii

Reputation: 59946

I will recommend to store your environment variable in the task definition. There is some advantage over Dockerfile.

  1. More secure than Docker ENV
  2. Ability to override at run time
  3. Zero chances of being missed like it seems env is missed during build time in your case
  4. Available across multiple services

You can define ENV is task definition under the container section.

enter image description here

Upvotes: 1

Mihai
Mihai

Reputation: 10727

The line

ARG pwd

means that you need to supply a value during build time: add --build-arg to docker build.

If you want the value to be supplied to the container when you launch it, then you need to remove the ARG line and the $pwd from the declaration of the ENV. docker run' accepts the option--env` where you can supply your values.

Upvotes: 0

Related Questions