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

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.

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.

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>

Related

If docker run has multiple -e VAR=VALUE options for the same VAR, which one is used?

If I have docker run ... -e VAR=x ... -e VAR=y ..., what is the value of VAR in the container? From a quick test it looks like the last one is used, but is this guaranteed anywhere in the documentation?
No, this is not guaranteed as far as I can tell. Here are the relevant locations in the documentation:
The docker run reference
The docker run command line reference
Also the docker daemon API does not specify what happens on duplicate entries in the Env array.
There is also the documentation on Environment variables precedence for docker-compose. But this also does not mention duplicate keys in one of the layers.
You are probably best advised to not rely on the fact that this is implemented as it is.

Pass NEPTUNE_API_TOKEN environment variable via docker run command

Using the docker run command, I'm trying to pass my NEPTUNE_API_TOKEN to my container.
My understanding is that I should use the -e flag as follows: -e ENV_VAR='env_var_value' and that might work.
I wish, however, to use the value existing in the already-running session, as follows:
docker run -e NEPTUNE_API_TOKEN=$(NEPTUNE_API_TOKEN) <my_image>
However, after doing so, NEPTUNE_API_TOKEN is set to empty when checking the value inside the container.
My question is whether I'm doing something wrong or if this is not possible and I must provide an explicit Neptune API token as a string.
$(NEPTUNE_API_TOKEN) is the syntax for running a command and grabbing the output. Use $NEPTUNE_API_TOKEN.
You can set up and pass NEPTUNE_API_TOKEN as a:
Docker run command options environment variable
Example: docker run -e NEPTUNE_API_TOKEN="<YOUR_API_TOKEN>" <image-name>
Dockerfile environment variable
Docker secret
Neptune will work with any of the methods described above.
For your case, I believe using method 2 and 3 will work best as you will set the API token only once and all containers can reuse it. Additionally, they are more secure methods.
You can read this guide on how to use Neptune with Docker I created last year.
Docs: https://docs.neptune.ai/how-to-guides/automation-pipelines/how-to-use-neptune-with-docker

How do I pass in configuration settings to a docker image for local development?

I'm working on a dotnet core docker container (not aspnet), I'd like to specify configuration options for it through appsettings.json. These values will eventually be filled in through environment variables in kubernetes.
However, for local development, how do we easily pass in these settings without storing them in the container?
You can map local volumes to docker -v local_path:container_path.
If you gonna use kubernetes you can use ConfigMap as well.
You can pass env variables while running the container with -e flag of the command docker run.
With this method, you’ll have to pass each variable in the command line. For example, docker run -e VAR1=value1 -e VAR2=value2
If this gets cumbersome, you can write these values to an env file and use this file like so, docker run --env-file=filename
For reference, you can check out the official docs.

Docker environmental variables from a file

I am trying to use one Dockerfile for both my production and development. The only difference between the production and development are the environment variables I set. Therefore I would like someway import the environment variables from a file. Before using Docker I would simply do the following
. ./setvars
./main.py
However if change ./main.py with the Docker equivalent
. ./setvars
docker run .... ./main.py
then the variables will be on the host and not accessible from the Docker instance. Of course a quick and dirty hack would be to make a file with
#!/bin/bash
. ./setvars
./main.py
and run that in the instance. That would however be really annoying, since I got lots of scripts I would like to run (with the same environment variables), and would then have to create a extra script for everyone of those.
Are there any other solution to get my environment variables inside docker without using a different Dockerfile and the method I described above?
Your best options is to use either the -e flag, or the --env-file of the docker run command.
The -e flag allows you to specify key/value pairs of env variable,
for example:
docker run -e ENVIRONMENT=PROD
You can use several time the -e flag to define multiple env
variables. For example, the docker registry itself is configurable
with -e flags, see:
https://docs.docker.com/registry/deploying/#running-a-domain-registry
The --env-file allow you to specify a file. But each line of the file
must be of type VAR=VAL
Full documentation:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables-e-env-env-file

passing parameters to app on docker image

I was looking around and I saw some simple examples of HelloWorld running on a docker container like this one:
http://dotnet.dzone.com/articles/docker-%E2%80%98hello-world-mono
at the end of the Dockerfile, the author calls:
CMD ["mono", "/src/hello.exe"]
What I want to do is have a reusable image as we build our Console App. Put that on a docker image using a Dockerfile. That part makes sense to me. But then I want to be able to pass the ConsoleApp parameters. Is that possible?
for example,
sudo docker run crystaltwix/helloworld -n "crystal twix"
where -n was a parameter I defined in my helloworld app.
You can use ENTRYPOINT foo rather than CMD foo to achieve this. All arguments after the docker run are passed to foo.
#seanmcl's answer is the simplest... if you have to pass secret values like application keys you may have to worry about exposing them is process lists.... So, you could use environment vars that you app looks for during startup:
SECRET_KEY="crystal twix"
docker run -e APP_KEY=$SECRET_KEY crystaltwix/helloworld

Resources