Environment Variables in Docker When Exec Docker Run

Get Environment Variable from Docker Container

The proper way to run echo "$ENV_VAR" inside the container so that the variable substitution happens in the container is:

docker exec <container_id> bash -c 'echo "$ENV_VAR"'

How to use docker container's env variable while running docker exec command

Something like this could work (it assumes, the container has a shell installed)

docker exec -it container_id sh -c 'command_here param_1 $param_2_as_env_variable'

For example the following works:

docker exec -it test sh -c 'echo $HOSTNAME'

to give the host name of the container.

Docker exec quoting variables

Ok, I found a way to do it, all you need to do is evaluate command with bash

docker exec -it <container id> bash -c 'echo something-${CLI}'

returns something-/usr/local/bin/myprogram

If the CLI environment variable is not already set in the container, you can also pass it in such as:

docker exec -it -e CLI=/usr/local/bin/myprogram <container id> bash -c 'echo something-${CLI}'

See the help file:

 docker exec --help

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
-d, --detach Detached mode: run command in the background
-e, --env list Set environment variables
....

How to pass ALL environment variables to container with docker exec

With Bash it seems using process substitution work:

docker run --rm -ti --env-file <(env) alpine sh

Note, this creates a temporary fifo file behind the scenes anyway.

Note, this will not work properly with variables containing newlines, they are cutoff on newlines. You should do something along, I tried to make it short:

readarray -d '' -t args < <(env -0 | sed -z 's/^/--env\x00/')
docker run --rm -ti "${args[@]}" alpine sh

How do I pass environment variables to Docker containers?

You can pass environment variables to your containers with the -e flag.

An example from a startup script:

sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \ 
-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \
-e POSTGRES_ENV_POSTGRES_USER='bar' \
-e POSTGRES_ENV_DB_NAME='mysite_staging' \
-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \
-e SITE_URL='staging.mysite.com' \
-p 80:80 \
--link redis:redis \
--name container_name dockerhub_id/image_name

Or, if you don't want to have the value on the command-line where it will be displayed by ps, etc., -e can pull in the value from the current environment if you just give it without the =:

sudo PASSWORD='foo' docker run  [...] -e PASSWORD [...]

If you have many environment variables and especially if they're meant to be secret, you can use an env-file:

$ docker run --env-file ./env.list ubuntu bash

The --env-file flag takes a filename as an argument and expects each line to be in the VAR=VAL format, mimicking the argument passed to --env. Comment lines need only be prefixed with #

Environment variables empty in Docker container bash

docker exec starts a new shell in the container. It's not a child of the the initial process, so it won't inherit any environment variables from that process.

If you want to set environment variables that will be visible in docker exec, then set them on the container itself, either in your Dockerfile:

FROM docker.io/node:18

ENV testvar=test
CMD node app.js

Or on the docker run command line:

docker run -e testvar=test myimagename

Can't execute command to set environment variables in docker container

Your question involves a lot of stuff, if you can narrow it down people can help better. Here are my suggestions to debug it:

bash -exc "
echo home1=$JAVA_HOME
source ./opt/apache-atlas-2.1.0/conf/atlas-env.sh
echo home2=$JAVA_HOME
chmod 777 /app/import-hive.sh
echo home3=$JAVA_HOME
/opt/apache-atlas-2.1.0/bin/atlas_start.py
"

If JAVA_HOME is never set, there's something wrong with .sh file, either you fix that file or manually set it with

export JAVA_ENV=/aaa/bbb/ccc

Or defining it in your compose yaml file.


Also the way you're checking for env vars is wrong, running Docker exec -it atlas bash won't run in the same bash as bash -c "source ./opt/apache-a..."

Environment variables in docker when exec docker run

echo $HOME is being evaluated on your host because you haven't got the syntax of the switch to bash correct. It's Linux so you need single quotes.

Try replacing your double quotes with single quotes.

eg. This is what I get:

bash-3.2$  docker run ubuntu /bin/bash -c 'echo $HOME'
/root


Related Topics



Leave a reply



Submit