How do I set environment variables for the Docker container?
Before we begin
The previous article described how to create a Docker container. This article (2017-08) describes how to set Docker container environment variables, such as the time zone environment variable in the example. It is important to note that the container environment variable needs to be specified when the container is created, and cannot be added or changed when the container is run.
Problem description
It is found that the time zone of the tomcat container log created using docker is incorrect (time difference is 8 hours). Even if the host / etc/localtime file is mounted (the host time zone is correct), although the operating system time of the container is normal, the time zone of the tomcat log is still incorrect, which brings trouble to the container log analysis. Our goal is to set the correct time zone for the created tomcat container.
Environment description
Software version
Tomcat:9 On Docker 1.13.1
Host time zone file
Tail-N1 / etc/localtimeCST-8
Commands used to create containers
Docker run-tid-- name test-p 10080 etc/localtime:/etc/localtime 8080\-v / usr/local/tomcat/logs\-v / etc/localtime:/etc/localtime\-label aliyun.logs.catalina=stdout\-- label aliyun.logs.access=/usr/local/tomcat/logs/localhost_access_log.*.txt\ tomcat:9
If you are not familiar with docker command parameters, you can use-- help help, or refer to this article
Log time zone
Docker logs-- tail 1 test 10-Aug-2017 06 virtual 43 test 10-Aug-2017 48.390 INFO [Thread-5] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
Note: the log time of 06:43:48 is incorrect.
Solution method
Specify the TZ time zone environment variable when you create the container, for example, set the time zone to Asia/Chongqing.
Docker run-tid-- name new-p 10080 usr/local/tomcat/logs 8080\-v / usr/local/tomcat/logs\-e TZ=Asia/Chongqing\-- label aliyun.logs.catalina=stdout\-- label aliyun.logs.access=/usr/local/tomcat/logs/localhost_access_log.*.txt\ tomcat:9
Verify the container time zone again
Docker logs-- tail 1 new 10-Aug-2017 14 new 10-Aug-2017 44 INFO 51.736 [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
Note: 14:44:51 log time is correct.
Command help
The run subcommand-e parameter sets the environment variable of the container can be specified multiple times, and multiple environment variables can also be read from the file.
Docker run-- help | grep-I'\-env'-e,-- env list Set environment variables-- env-file list Read in a file of environment variables reference articles
How do I set environment variables for the Docker container?
Doker&k8s Qun [703906133]