Use of environment variables in Docker and solutions to common problems

Use of environment variables in Docker and solutions to common problems

Preface

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

  • When making an image, add environment variables to the image through the ENV command. Use this environment variable when starting the container.
  • When the container is started, the environment variables are configured through parameters. If there are duplicate environment variables in the image, the environment variables of the image will be overwritten.

Use docker exec {containerID} env to 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
...

In the process started by the container, that is, in ENTRYPOINT+CMD, the environment variables of the container can be obtained through the corresponding system library.

Enter the container and view the environment variables of the process. You can view them under /proc.

cat /proc/{pid}/environ

Therefore, the environment variables in the container can also be obtained by viewing the environment variables of process No. 1 in the container. You can view it by executing the cat /proc/1/environ |tr '\0' '\n' command.

[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, child processes generated from a parent process will inherit the environment variables of the parent process by default. Therefore, the environment variables of each process in the container should be roughly the same. Of course, in some special cases, environment variables will also be reset, leading to some misunderstandings and problems. The following is an explanation of some common situations in containers.

Common problems and solutions

Environment variables disappear after switching to different users

In the container, after starting, you switch to different users. For example, after using su - admin to switch to the admin user, you may find that the configured container environment variables are lost.

This is because switching users causes the environment variables to be reset. Therefore, you need to use the method of su -p admin to inherit the previous environment variables.

We can use help to see the description of relevant parameters 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 characters in containers

When some businesses are migrated to containers, they often report that the printed logs are garbled. The general reason is that the locale is not configured correctly.

You can view the current container's locale through locale. If not set, it will typically be POSIX. We can use locale -a to view the language environment supported by the current container, and then set it as needed.

If you want to solve the problem once and for all, the best way is to add LANG={xxx} to the environment variables when starting the container or the image, and select the appropriate language to avoid the garbled code problem caused by it.

ssh environment variable problem

Enabling sshd in the container can facilitate connection and troubleshooting, as well as some daily operation and maintenance operations.

However, many users who enter the container find that the environment variables configured when Docker is started cannot be displayed normally through the env command.

The main reason for this is that the environment variables are reset when ssh establishes a connection for the user.

The biggest problem caused by this is that the container process started through ssh will not be able to obtain the environment variables configured when the container was started.

After understanding the principle, there is a simple way to solve this problem. That is, you can reset the environment variables of the container to the session after the ssh connection.
The specific implementation method is that after ssh connection, source /etc/profile will be automatically executed.

Then we just need to add a few lines of code to /etc/profile, obtain the environment variables of the container itself from process No. 1, and then export the environment variables in a loop.

Following is a simple for loop implementation.

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

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

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Docker solves the problem that the terminal cannot input Chinese
  • Docker exposes port 2375, causing server attacks and solutions
  • Install Jenkins with Docker and solve the problem of initial plugin installation failure
  • Solve the problem that the docker container cannot ping the external network
  • When setting up Jenkins in Docker environment, the console log shows garbled Chinese characters when building tasks
  • Solution to the problem that docker logs cannot be retrieved
  • Share the problem of Ubuntu 19 not being able to install docker source
  • Docker FAQ

<<:  mysql having usage analysis

>>:  How to use JSZip compression in CocosCreator

Recommend

Dynamic SQL statement analysis in Mybatis

This article mainly introduces the dynamic SQL st...

In-depth analysis of MySQL query interception

Table of contents 1. Query Optimization 1. MySQL ...

How to implement element floating and clear floating with CSS

Basic Introduction to Floating In the standard do...

How to correctly modify the ROOT password in MySql8.0 and above versions

Deployment environment: Installation version red ...

How to reset the root password in mysql8.0.12

After installing the database, if you accidentall...

Let's talk in depth about the principle and implementation of new in JS

Table of contents definition Constructor bodies a...

Web development tutorial cross-domain solution detailed explanation

Preface This article mainly introduces the cross-...

How to build a tomcat image based on Dockerfile

Dockerfile is a file used to build a docker image...

CSS3 animation to achieve the effect of streamer button

In the process of learning CSS3, I found that man...

MySQL triggers: creating multiple triggers operation example analysis

This article uses an example to describe the crea...

Detailed steps to install VMware Tools from scratch (graphic tutorial)

VMware Tools is a tool that comes with VMware vir...