ssh connection to docker, the solution to the change of environment variables

Question : During the test, run docker locally and enter it, and found that it can run normally. But once the image is uploaded to the mirror warehouse, after starting with k8s, ssh in, it prompts that the required software is not installed.

The reason for the problem : when ssh establishes a connection, the environment variable will be reset. In the final analysis, it is the problem of environment variables, it can not find those things installed. Hmm... like a road idiot, if you change the road, you can't go home.

Solution : Step 1) Modify the /etc/profile file

                a. Use for loop

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

                b. Use a command

export $(cat /proc/1/environ |tr '\0' '\n' | xargs)

Personal test, a command thief is easy to use~

                Step 2) source /etc/profile to make it take effect immediately

The principle of the solution : After the ssh connection, it will be executed automatically source /etc/profile. These additions mean that the environment variables of the container itself are obtained from process 1.

Reference source : Analyzing the use of environment variables in docker and solving common problems - xinkun's blog | Xinkun Blog

Guess you like

Origin blog.csdn.net/m0_59029800/article/details/125479518