In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of the use of environmental variables and common problems in docker", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn "sample analysis of the use of environmental variables and common problems in docker".
Preface
Docker can configure environment variables for the container. There are two ways to configure:
When making an image, use the ENV command to add environment variables to the image. Use this environment variable when the container starts.
When the container starts, the environment variable is configured with parameters. If there is a duplicate environment variable in the image, the environment variable in the image will be overwritten.
Use docker exec {containerID} env to view the environment variables that are in effect in the container.
[root@localhost ~] # docker exec 984 envPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/java/default/binTERM=xtermAUTHORIZED_KEYS=**None**JAVA_HOME=/usr/java/defaultHOME=/root...
The process started by the container, that is, in ENTRYPOINT+CMD, can obtain the environment variables of the container through the corresponding system library.
Go to the container and view the environment variables of the process, which can be viewed under / proc.
Cat / proc/ {pid} / environ
Therefore, the environment variables in the container can also be obtained by looking at the environment variables of process 1 in the container. You can view it by executing the command cat / proc/1/environ | tr'\ 0'\ n'.
[root@localhost ~] # docker exec-it 984 cat / proc/1/environ | tr'\ 0'\ nBinalPATHANGUTHORIZEDKEYSIZEDKEYSZEDKEYSINTHORIZEDAUTHORIZEDANGUTHORIZEDAUTHORIZEDANGUTORIZED
In general, child processes generated from the parent process inherit the environment variables of the parent process by default. Therefore, the environment variables for each process in the container should be roughly the same. Of course, in some special cases, environmental variables will also be reset, resulting in some misunderstandings and problems. Here is an explanation of some common situations in containers.
Common problems and their Solutions
Environment variables disappear after switching between different users
In the container, switch different users after startup, for example, after switching admin users using su-admin, it is found that the configured container environment variables are missing.
This is because switching users causes environment variables to reset. Therefore, you have to use su-p admin to inherit the previous environment variables.
We can take a look at the relevant parameter description of su through help.
[root@adworderp-03a38d62-4103555841-m81qk /] # su-- helpUsage: 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
When some businesses migrate to containers, they often report printing log garbled. The general reason is that locale is not configured correctly.
You can view the locale of the current container through locale. If it is not set, it is usually POSIX. We can view the current locale supported by the container through locale-a, and then set it as needed.
To get rid of it once and for all, the best way is to add LANG= {xxx} to the environment variables launched or mirrored by the container, and choose the appropriate language, so as to avoid the garbled code problem.
The problem of Environmental variables in ssh
Enable sshd in the container to facilitate connection and troubleshooting, as well as some daily operation and maintenance operations.
However, many users enter the container and find that the environment variables configured at docker startup are not displayed properly through the env command.
The main reason for this is that when ssh establishes a connection for the user, it causes the environment variable to be reset.
The biggest problem caused by this is that the container process started through ssh will not be able to get the environment variables configured by the container at startup.
After understanding the principle, there is a simple way to solve this problem. It is possible to reset the environment variable of the container to the session after the ssh connection.
The specific implementation is that source / etc/profile is automatically executed after the ssh is connected.
So all we have to do is add a few lines of code to / etc/profile to get the environment variable of the container itself from process 1, and then export the environment variable in a loop.
The following is a simple for loop implementation.
For item in `cat / proc/1/environ | tr'\ 0''\ n' `do export $itemdone
Of course, there is a more concise command, export $(cat / proc/1/environ | tr'\ 0'\ n' | xargs), which can achieve the same effect.
The above is all the content of the article "sample Analysis of the use of Environment variables and frequently asked questions in docker". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.