Accessing Environment Variables in a linked Docker - docker

How do I access the environment variables of a source container from another container which is linking to it with the --link argument?
The docker manual states:
Environment Variables
Docker creates several environment variables when you link containers.
Docker automatically creates environment variables in the target
container based on the --link parameters. It will also expose all
environment variables originating from Docker from the source
container. These include variables from:
the ENV commands in the source container's Dockerfile
the -e, --env and --env-file options on the docker run command when the source container is started
http://docs.docker.com/userguide/dockerlinks/
But I can't access an environment variable which I set with
ENV MY_VARIABLE = "example"
in the linking container with
#!/bin/sh
echo $MY_VARIABLE
it will contain no value.

Environment variables from the source container are prefixed with the alias set with the --link <source container>:<alias> in the target container:
$<alias>_ENV_<env variable>
The environment variable will be accessible as follows if <alias> is e.g. set to source:
#!/bin/sh
echo $SOURCE_ENV_MY_VARIABLE

Related

docker env variable only local

I have a Dockerfile that sets environment variables that are common to all environments, whether dev, test, or production ones, but I have to set another environment variable that is only applicable to my development environment, so I can't set it in the Dockerfile because such file is managed by the version control, so the change would be deployed to all environments.
How can add an environment variable to a docker container only in my local development environment?
In case that the env variable can be specified when the image is being used, then just supplying the variable then makes more sense. For instance, if you are locally testing the image, by using the docker cli, you can set the variable with:
docker run -e KEY=VALUE $image
If you are using other tools to test the image, there are always other methods to set env keys.
If it's required for you to have set the variable at build time, you can specify built args inside the Dockerfile.
An example for that would be:
FROM someimage:v1
ARG DEV_ONLY_VAR
ENV KEY=$DEV_ONLY_VAR
Using this, you can specify the build arg DEV_ONLY_VAR in the build command by writing:
docker build --build-arg DEV_ONLY_VAR=VALUE .
Note, even without the ENV KEY=$DEV_ONLY_VAR line the build arg will be available like a env variable during build time, on other run steps.
More on build args here

How do environment variables work when a Docker container builds itself?

I'm trying to update an environment variable for a Docker image
The image builds like this in Jenkins:
insideNode { // executed inside the container docker-compose is building
withEnv(['COMPOSE_OPTIONS=-e http_proxy -e https_proxy -e no_proxy']) {
sh """
export http_proxy=${env.http_proxy}
export https_proxy=${env.https_proxy}
export no_proxy=${env.no_proxy}
VERSION=${project.version} docker-compose --file docker-compose.yml node-container
"""
}
}
The insideNode closure is executed inside the Docker container I'm building - so it uses itself to build a new version of itself. I'm trying to update those proxy variables, which you can see are drawn from the environment (the Jenkins container originally, but now its own container).
If I update the environment variables in the overall Jenkins environment, do they flow down to the container build here? I would assume that the closure would be using the container's own environment, which means you can't really update them since they need to be updated, to use the updated values, so it becomes a cyclical dependency. So in this scenario, how would overwrite those env vars?

Find out value of environment variables being passed to container by docker-compose

I'm troubleshooting a service that's failing to start because of an environment variable issue. I'd like to check out what the environment variables look like from the inside of the container. Is there a command I can pass in my docker-compose.yaml so that instead of starting the service it prints the relevant environment variable to standard output and exits?
Try this:
docker-compose run rabbitmq env
This will run env inside the rabbitmq service. env will print all environment variables (from the shell).
If the service is already running, you can do this instead, which will run env in a new shell in the existing container (which is faster since it does not need to spin up a new instance):
docker-compose exec rabbitmq env
Get the container ID with docker ps.
Then execute a shell for the running rabbitmq container by running docker exec command with the container id for your rabbitmq container.
Once you are on the rabbitmq container, you can echo out the value of any environment variable like you would on any other linux system. e.g. if you declared ENV DEBUG=true at image build time, then is should be able to retrieve that value with echo $DEBUG in the container. Furthermore, once you are in the container, you can poke around the log files for more investigation.
As others have said, first get the container ID with docker ps. When you have done that, view all the properties with docker inspect <id> and you will see something like:
[
{
...
"Config": {
...
"Env": [
"ASPNETCORE_URLS=http://+:80",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"DOTNET_RUNNING_IN_CONTAINER=true",
"DOTNET_VERSION=6.0.1",
"ASPNET_VERSION=6.0.1",
"Logging__Console__FormatterName=Json"
],
...
}
}
]

Define environment variable in Dockerfile or docker-compose?

After reading the config point of the 12 factor app I decided to override my config file containing default value with environment variable.
I have 3 Dockerfiles, one for an API, one for a front-end and one for a worker. I have one docker-compose.yml to run those 3 services plus a database.
Now I'm wondering if I should define the environment variables in Dockerfiles or docker-compose.yml ? What's the difference between using one rather than another ?
See this:
You can set environment variables in a service’s containers with the 'environment' key, just like with docker run -e VARIABLE=VALUE ...
Also, you can use ENV in dockerfile to define a environment variable.
The difference is:
Environment variable define in Dockerfile will not only used in docker build, it will also persist into container. This means if you did not set -e when docker run, it will still have environment variable same as defined in Dockerfile.
While environment variable define in docker-compose.yaml just used for docker run.
Maybe next example could make you understand more clear:
Dockerfile:
FROM alpine
ENV http_proxy http://123
docker-compose.yaml:
app:
environment:
- http_proxy=http://123
If you define environment variable in Dockerfile, all containers used this image will also has the http_proxy as http://123. But the real situation maybe when you build the image, you need this proxy. But, the container maybe run by other people maybe not need this proxy or just have another http_proxy, so they had to remove the http_proxy in entrypoint or just change to another value in docker-compose.yaml.
If you define environment variable in docker-compose.yaml, then user could just choose his own http_proxy when do docker-compose up, http_proxy will not be set if user did not configure it docker-compose.yaml.

Environment variable and docker-compose

When running a number of linked services with docker-compose up, how can I add values for environment variables to be passed to specific containers for services? Say I have a service "webapp" which uses the TIMEOUT environment variable, how do I set TIMEOUT=10000 with docker-compose for the container that "webapp" lives in? I have tried the notation -e TIMEOUT=10000 from the docker command, but this does not seem to work.
with docker compose your have to specify environment variables in docker-compose.yml with env_file or environment configuration commands
https://docs.docker.com/compose/compose-file/#env-file
https://docs.docker.com/compose/compose-file/#environment

Resources