mhaken
mhaken

Reputation: 1125

Docker Environment Variable Not Overridden

Given I have a dockerfile like:

ARG MAX_MEMORY_PER_NODE="10GB"
ENV P_MAX_MEMORY_PER_NODE="${MAX_MEMORY_PER_NODE}"
ENTRYPOINT ["/var/p/entrypoint.sh"]

And the entrypoint.sh does something like:

echo "Max memory ${P_MAX_MEMORY_PER_NODE}"

If I were to run the container using the defaults, I would expect

Max Memory 10GB

And that works, but if I run

docker run me/mycontainer:latest -e P_MAX_MEMORY_PER_NODE=1GB

The script still uses the default value (does not print 1GB instead). In fact if I ran:

docker run me/mycontainer:latest -e A_TEST=Hello

And the script had

echo "My test: ${A_TEST}"

It would output

My test:

What am I doing wrong here? What can't I override (or even set) the environment variables being used in the entrypoint script from docker run?

Upvotes: 2

Views: 2679

Answers (2)

Mahdy H.
Mahdy H.

Reputation: 191

For docker-compose

Similar to the this answer: https://stackoverflow.com/a/48915478/11406645

when using docker-compose, and you are passing docker-compose.yaml file an environment variable, or overriding one in env_file; you should pass your environment variable like so: DEBUG=1 docker-compose up

Another problem I faced is that docker commands require sudo permissions:

If you are using sudo before the docker-compose command, add the environment variable after the sudo like so: sudo DEBUG=1 docker-compose up.

The wrong way:

DEBUG=1 sudo docker-compose up

The right way:

sudo DEBUG=1 docker-compose up

Upvotes: 0

Lexandro
Lexandro

Reputation: 785

Set the environment variable before the image:

docker run -e "A_TEST=hello" alpine env

Upvotes: 9

Related Questions