Can I pass all of my existing env vars to an image without declaring them indv?

69 Views Asked by At

Currently my run command looks like this...

docker run -e DB_URL=$DB_URL -e DB_PORT=$DB_PORT ... <image name>

This works but not very scalable. Is there a way to just pass all configured env vars to the container without declaring each one?

I am using OSX and these are set in a .bash_profile.

2

There are 2 best solutions below

1
On

env > envFile && docker run --env-file=envFile alpine env

However I would not recommend doing this as this will pass even un-necessary info to docker container. And you should rather use a compose file or maybe even a simple script to only pass in variables that are actually needed. This might even mess with the shell inside of a container for things like prompts and locales etc.

0
On

Ultimately, the best option is to explicitly list the vars you want to pass. There should only be a finite number of them, they shouldnt change often, and this solution is more robust and more secure.

Note that you can simplify your example. If you want to pass in a defined var with its current value, just name the var instead of setting it:

docker run -e DB_URL -e DB_PORT ... <image name>