Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

343
Visualizações
Docker inspect command not showing all the ENV vars

I ran an Alpine Docker container using the following command:

docker run -itd --env "my_env_var=test" --name mycontainer alpine

Now when I run the env command from inside the Docker container to get all the Env vars, I get the following output:

$ docker exec -it mycontainer /bin/sh
$ env
HOSTNAME=9de9045b5264
SHLVL=1
HOME=/root
my_env_var=test
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

So far, so good.

But when I try to get the Environment Variables using the docker inspect command, I see that too many ENV vars are missing (Only 2 of them are found):

$ docker inspect mycontainer
...
...
"Env": [
            "my_env_var=test",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
...
...

Also, I used the docker exec command to print the ENV vars using the following command:

$ docker exec mycontainer /bin/sh -c env
HOSTNAME=9de9045b5264
SHLVL=1
HOME=/root
my_env_var=test
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

This time, all the ENV vars were displayed except for the TERM=xterm.

I observed this behaviour with centos Docker images as well. Why is it so that all the ENV vars are not getting printed using docker inspect or docker exec?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This happens because those environment variables are set by the binaries you are using to display them — here /bin/sh.

There are mechanisms in Linux distributions allowing you setting variables for an interactive shell, like /etc/profile to cite only one out of them.

A better command to run in order to have something closer to what your inspect command would give you would be:

docker exec mycontainer env

Which gives

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1fbc89f485c2
my_env_var=test
HOME=/root

For the different commands that are giving you the TERM variable, this is caused by the -t option of docker:

--tty, -t Allocate a pseudo-TTY

We can show this using

docker exec mycontainer env

Which gives

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1fbc89f485c2
my_env_var=test
HOME=/root

Versus

docker exec -t mycontainer env

Which gives

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1fbc89f485c2
TERM=xterm
my_env_var=test
HOME=/root

Little side note on the usage of docker inspect: you can also filter the output with the format flag to get only the information you need:

e.g.:

docker inspect mycontainer --format "{{.Config.Env}}"

Gives

[my_env_var=test PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]
over 4 years ago · Santiago Trujillo Relatório

0

Why is it so that all the ENV vars are not getting printed using docker inspect or docker exec?

Because /bin/sh sets them.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda