1 introduction

Welcome to visit www.pkslow.com for more wonderful articles!

It is very important to understand the running environment of Docker container. We put the application in the container to execute, and environment variables will directly affect the execution effect of the program. So we need to know the environment variables inside the container, and we need to know how to change those environment variables.

2 View environment variables

2.1 Method 1: Docker inspect

With the docker inspect command, you can not only view environment variables, but also view other container related information, which is very rich and output in Json format.

$ docker inspect centos
Copy the code

Take a look at the clip:

Readability is ok, but not very good. You can filter this by using the grep command:

$ docker inspect centos | grep SERVER
                "SERVER_PORT=80".Copy the code

Or parse the Json text:

$ docker inspect -f '{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' centos
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Copy the code

2.2 Method 2: Doecker exec env

The environment variables obtained this way are the same as the environment variables obtained in Linux. Just run the env command on the container. As follows:

$ docker exec centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=f8b489603f31
HOME=/root
Copy the code

3 Set environment variables

3.1 Method 1: Package Set dockerFile

Environment variables can be configured when packaging images with Dockerfile:

ENV SERVER_PORT 80
ENV APP_NAME pkslow
Copy the code

3.2 Method 2: Start set docker run –env

Using –env works the same as -e, as shown in the following example:

$ docker run -itd --name=centos -e SERVER_PORT=80 --env APP_NAME=pkslow centos:7
b3d42726ca6cdddd7ae09d70e720d6db94ff030617c7ba5f58374ec43f8e8d68

$ docker exec centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=b3d42726ca6c
SERVER_PORT=80
APP_NAME=pkslow
HOME=/root
Copy the code

Instead of giving a value, you can use the host environment variable and pass the HISTSIZE of the host as follows:

$ docker run -itd --name=centos -e SERVER_PORT=80 --env APP_NAME=pkslow -e HISTSIZE centos:7
f8b489603f31779cdae88c77fb446aac80d61f41922a79128d6d26ee6b70a091

$ docker exec centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=f8b489603f31
SERVER_PORT=80
APP_NAME=pkslow
HISTSIZE=1000000
HOME=/root
Copy the code

3.3 Method 3: Loading files at startup docker run –env-file

First put the configuration information in the file env.list:

$ cat env.list 
VAR1=www
VAR2=pkslow.com
VAR3=www.pkslow.com
Copy the code

File passed when starting the container:

$ docker run -itd --name=centos --env-file env.list centos:7
1ef776e2ca2e4d3f8cdb816d3a059206fc9381db58d7290ef69301472f9b4186

$ docker exec centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1ef776e2ca2e
VAR1=www
VAR2=pkslow.com
VAR3=www.pkslow.com
HOME=/root
Copy the code

If you want to pass all environment variables from the host to the container, you can do this:

$ env > env.list
$ docker run -itd --name=centos --env-file env.list centos:7
Copy the code

4 summarizes

This article introduces a variety of ways to view and set up the Docker container environment, which can be used according to your own needs.


Welcome to follow the wechat public account < pumpkin shuo >, will continue to update for you…

Read more, share more; Write more. Organize more.