Andy
Andy

Reputation: 2603

Can I add new environment variable to docker container

There are ways to pass the environment variable declared in dockerfile using the docker run command using --env or -e options.

E.g. Lets say you are using MAC or linux, then you can define a shell variable like below:

export MyEnvVar=SomeValue

And then pass this value to an environment variable that was defined inside your dockerfile.

docker run --name "MyContainerName" -e MyEnvVar Container_Image_Name And for this to work you must have a statement in the dockerfile like below:

ENV MyEnvVar=${MyEnvVar}

However, I do not want to define any variable inside dockerfile but add a new environment key/value while creating a container instance using docker run or some other way.

So is there a way to add a new environment variable using docker run or by some other means in to a docker container without declaring that environment variable in dockerfile?

Upvotes: 1

Views: 866

Answers (1)

Matt
Matt

Reputation: 74791

Adding an environment variable at run time doesn't require the ENV to be defined in the Dockerfile

$ docker run -e TEST=OHYEAH busybox sh -c 'echo $TEST'
OHYEAH

As @djmm187 mentioned, --env-file will work the same too.

Upvotes: 4

Related Questions