Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile Overriding ENV variable

I have a Dockerfile and I'd like to make the API configurable with a default value.

FROM socialengine/nginx-spa

ENV API_URL localhost:6007

So when I run this image I'd to be able to override the localhost:6007 with something like below:

docker run -e API_URL=production.com:6007 ui

This doesn't work and I can't find a clear explanation of how to do this.

Any advice?

like image 535
Proximo Avatar asked Oct 30 '16 23:10

Proximo


People also ask

How do I pass an environment variable in Dockerfile?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

Can I use ENV variable in Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do I override an .ENV file?

Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.

What is the difference between ENV and Arg in Dockerfile?

ENV is for future running containers. ARG for building your Docker image. ¶ ENV is mainly meant to provide default values for your future environment variables.


1 Answers

What you have described should work just fine. Given:

$ cat Dockerfile FROM socialengine/nginx-spa ENV API_URL localhost:6007 $ docker build -t ui . [...] 

Consider this:

$ docker run -it --rm ui env | grep API_URL API_URL=localhost:6007 

Compared to:

$ docker run -it --rm -e API_URL='production:6007' ui env | grep API_URL API_URL=production:6007 

Passing a -e VARNAME=varvalue on the docker run command line will override a default set in your Dockerfile.

If you are seeing different behavior, please update your question to show exactly the command you are running and the associated output.

like image 192
larsks Avatar answered Sep 23 '22 21:09

larsks