Environment variable usage and common problem solving in docker

  • 2021-01-14 07:08:46
  • OfStack

preface

docker can configure environment variables for containers. There are two ways to configure:

When making an image, add an environment variable to the image using the ENV command. This environment variable is used when the container starts. When the container starts, the environment variables are configured with parameters that override the mirrored environment variables if they are duplicated with the image.

use docker exec {containerID} env You can view the environment variables in effect in the container.


[root@localhost ~]# docker exec 984 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/java/default/bin
TERM=xterm
AUTHORIZED_KEYS=**None**
JAVA_HOME=/usr/java/default
HOME=/root
...

Processes started by the container, that is, ENTRYPOINT+CMD, can obtain the container's environment variables from the corresponding system library.

Enter the container to view the process's environment variables, which can be viewed under /proc.


cat /proc/{pid}/environ

Therefore, the environment variable in the container can also be obtained by looking at the environment variable for process 1 in the container. Can perform cat proc / 1 / environ | tr '\ 0' '\ n' commands to look at it.


[root@localhost ~]# docker exec -it 984 cat /proc/1/environ |tr '\0' '\n'
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/java/default/bin
TERM=xterm
AUTHORIZED_KEYS=**None**
JAVA_HOME=/usr/java/default
HOME=/root
...

Generally speaking, the child process from the parent process will inherit the parent's environment variables by default. Therefore, the environment variables for each process in the container should be roughly the same. Of course, in some special cases, the environment variables can also be reset, leading to some misunderstandings and problems. Some common cases in containers are explained below.

Frequently Asked Questions and Solutions

Environment variables disappear after switching between different users

In the container, switch between different users after startup, such as using su - admin After switching the admin user, the configured container environment variable is found missing.

This is because switching users causes the environment variable to reset. So you have to use su -p admin In this way, you can inherit the previous environment variable.

We can refer to help to see the relevant parameter description of su.


[root@adworderp-03a38d62-4103555841-m81qk /]# su --help
Usage: su [OPTION]... [-] [USER [ARG]...]
Change the effective user id and group id to that of USER.

...
 -m, --preserve-environment do not reset HOME, SHELL, USER, LOGNAME
    environment variables
 -p    same as -m
...

Garbled code problem in container

Some businesses often report print log garbled when migrating to containers. 1. The reason is that locale is not configured correctly.

You can view the locale of the current container through locale. If not set, 1 will be POSIX. We can use locale-a to view the locale supported by the current container and then set it up as needed.

The best way to do this is to add LANG={xxx} to the container startup or mirrored environment variable and choose the appropriate language to avoid the resulting garbled code problems.

ssh environment variable problem

Containers with sshd enabled can facilitate connection and troubleshooting, as well as some daily operation and maintenance operations.

However, many users enter the container only to find that the environment variable configured at docker startup is not displayed properly by env command.

The main reason for this is that the environment variable will be reset when ES77en establishes a connection for the user.

The biggest problem with this is that container processes started with ssh will not be able to retrieve the environment variables configured when the container was started.

Having understood the principle, there is a simple solution to the problem. This can be done by resetting the container's environment variables to session after ssh is connected.
The specific implementation is that after ssh is connected, source /etc/profile will automatically execute.

We simply append a few lines of code to /etc/profile, fetch the container's own environment variable from process 1, and loop through the environment variable export1.

Here is a simple implementation of an for loop.


for item in `cat /proc/1/environ |tr '\0' '\n'`
do
 export $item
done

And, of course, there are more concise command is export $(cat/proc / 1 / environ | tr '\ 0' '\ n | xargs), can achieve the same effect.

conclusion


Related articles: