I am trying to read some environment variables(which I set after creation of the container) from a container(Ubuntu 16.04). I set those variables using script as mentioned here.
But the problem is these environment variables are not available when they are used in some other script. But the strange thing is when I run ‘set’ command I can see all my desired environment variables. When I run “echo $VAR1”, I can see the desired value. But if I put the same command( “echo $VAR1”) in a script, they are not available anymore. Can anyone please tell me how to make the environment variables available in script that are set by sourcing ‘/etc/bash.bashrc’ / ‘/etc/profile’ / ‘/root/.bashrc’ file inside a container.
The first answer to that SO question is correct, and the second (accepted) one is misleading. You can’t change the environment variables of the primary process in a running container (or, for that matter, any other running process). You should destroy and recreate your container with adjusted -e options, or update your Dockerfile with updated ENV statements.
(Destroying and recreating your container to change things like port mappings or environment variables is totally normal, and your overall Docker workflow shouldn’t depend on “precious” containers that can’t be destroyed.)
In most cases, starting a container doesn’t go via a shell and those files are meaningless. (Even if it does start a shell, it doesn’t start a login shell or an interactive shell; see bash(1) for details of what this means and which files get read.)
Thank you for the quick reply. So that means currently there is no way to update or set environment variable in a container that will be available in a script.
However its really confusing that when I echo the variable in the bash shell(inside container) it gives the value of the environment variable that I set before but its not available inside the script that is ruuning inside container.
Actually I was trying to automate a system where some config parameters(only available after creating a container example-IP, connected networks) will be set in a container and later supply this parameter as environment variable to other container to complete the system connectivity. Can you please give me an idea how it is possible?
You can set the environment variables at launch time, with the docker run -e option, or in something like an entrypoint script. You can’t change the environment variables after the container starts.
This is a general Unix limitation. Consider the following shell script:
#!/bin/sh
while true; do
echo "$V"
sleep 5
fi
It will always print the same string, whatever was in $V when it launched. There’s no way to change that variable later on.
You probably shouldn’t try to deal in raw IP addresses. Docker provides an internal DNS service that allows containers to find each other by name; or you can use a service-discovery tool like Consul, which again provides a DNS service where your containers can find each other by name. (Two corollaries to this: the container private IP addresses aren’t typically useful, since they’re on private networks that aren’t visible off-host; and so docker inspect is a debugging tool you rarely need to use.)
If you do need to pass containers’ IP addresses in environment variables, then you need an orchestration system that starts one container, then finds the relevant IP address, then starts another container passing it in. This is more orchestration than for instance Docker Compose is capable of.