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

How is a SQL statement executed in MySQL?

Table of contents 1. Analysis of MySQL architectu...

Problems and experiences encountered in web development

<br />The following are the problems I encou...

Mysql optimization Zabbix partition optimization

The biggest bottleneck of using zabbix is ​​the d...

ie filter collection

IE gave us a headache in the early stages of deve...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

Introduction to install method in Vue

Table of contents 1. Globally registered componen...

MySQL date and time addition and subtraction sample code

Table of contents 1.MySQL adds or subtracts a tim...

VMware virtualization kvm installation and deployment tutorial summary

Virtualization 1. Environment Centos7.3 Disable s...

Detailed explanation of Nginx version smooth upgrade solution

Table of contents background: Nginx smooth upgrad...

Front-end JavaScript Promise

Table of contents 1. What is Promise 2. Basic usa...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

Implementation of Docker Compose multi-container deployment

Table of contents 1. WordPress deployment 1. Prep...

What kinds of MYSQL connection queries do you know?

Preface If the query information comes from multi...