Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ENV variable when doing Docker Inspect

Tags:

docker

I wonder how I get an Environment variable from docker inspect.

when i run

docker inspect -f "{{.Config.Env.PATH}} " 1e2b8689cf06 

i get the following

FATA[0000] template: :1:9: executing "" at <.Config.Env.PATH>: can't evaluate field PATH in type interface {} 
like image 534
Trind Avatar asked May 20 '15 07:05

Trind


People also ask

How can I see environment variables in container?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers.

Can docker access environment variables?

Overriding ENV ValuesContainers started from it, have access to ENV variables defined in the Dockerfile. However, those values can be overridden by providing single environment variables, or env_files, from which environment variables are parsed and passed into the container.

Where does docker get its environment variables?

The variables from env come from the Dockerfile or command, docker itself and whatever the entrypoint sets.

How do I pass an environment variable in docker run?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...


1 Answers

You can get directly with a command similar to

docker inspect --format '{{ index (index .Config.Env) 1 }}' 797 

which shows for me

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

You will notice that replacing the 1 by 0 like

docker inspect --format '{{ index (index .Config.Env) 0 }}' 797 

gives

DISPLAY=:0 

In fact I had noticed the following for various docker inspect from a more general to a more precise display

docker inspect --format '{{ (.NetworkSettings.Ports) }}' 87c map[8000/tcp:[map[HostIp:0.0.0.0 HostPort:49153]]] docker inspect --format '{{ ((index .NetworkSettings.Ports "8000/tcp") 0) }}' 87c [map[HostIp:0.0.0.0 HostPort:49153]] docker inspect --format '{{ index (index .NetworkSettings.Ports "8000/tcp") 0 }}' 87c map[HostIp:0.0.0.0 HostPort:49153] docker inspect --format '{{ (index (index .NetworkSettings.Ports "8000/tcp") 0).HostIp }}' 87c 0.0.0.0 docker inspect --format '{{ (index (index .NetworkSettings.Ports "8000/tcp") 0).HostPort }}' 87c 49153 

Note: docker inspect -f works and is shorter, of course, I posted the "old" syntax.

like image 109
user2915097 Avatar answered Sep 23 '22 07:09

user2915097