Print the environment variables set during the docker run command with Python executed by the shell script in the container.
At work, I am constructing a scheduled email sending system based on the SMTP server information passed based on the environment variables in Python by executing the docker run command with environment variables (SMTP server information etc.) on Amazon ECS. Trial environment variables at home for that purpose.
PC:windows 10 home docker tool-box
$ docker version
Client:
Version: 19.03.1
API version: 1.40
Go version: go1.12.7
Git commit: 74b1e89e8a
Built: Wed Jul 31 15:18:18 2019
OS/Arch: windows/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:28:45 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
$ docker run --env-file=env.txt -v /c/Users/.../environment_variable_test/code:/code -it python:3.6 bin/bash
root@82a5c3949b49:/#
Run the docker run command in the env.txt directory. The environment variable is set in --env-file = env.txt. By the way, env.txt is
env.txt
AAA=asdfgg
BBB=bnmbnm
CCC=cvbnm
There are three environment variables: AAA, BBB, and CCC. I tried to separate it into another file because it was a big deal. The environment variable settings of docker run are listed here in detail ↓ https://qiita.com/KEINOS/items/518610bc2fdf5999acf2
You win if you display asdfgg, bnmbnm, cvbn in python.
-v / c / Users / ... / environment_variable_test / code: / code is a directory mount. The directory on the host side and docker are synchronized.
By the way, check that the environment variables are written properly Reference: https://www.kabegiwablog.com/entry/2018/07/25/100000
root@82a5c3949b49:/# env | grep -e "AAA\|BBB\|CCC"
AAA=asdfgg
BBB=bnmbnm
CCC=cvbnm
root@82a5c3949b49:/#
Create in / c / Users / ... / environment_variable_test / code.
test.sh
#!/bin/sh
python test.py $AAA $BBB $CCC
echo "OK"
The point of addiction is that if you do not enter #! / Bin / sh on the first line, it will not be recognized as .sh Also, add $ to the environment variable Pass environment variables as arguments to test.py.
echo "OK" will display OK on the shell. bonus.
test.py
#!/usr/bin/env python
import sys
args = sys.argv
print("First argument:" + args[1])
print("Second argument:" + args[2])
print("Third argument:" + args[3])
This also doesn't recognize as .py unless you put #! / Usr / bin / env python on the first line. See here for argument operations in Python ↓ https://qiita.com/orange_u/items/3f0fb6044fd5ee2c3a37
root@82a5c3949b49:/code# bash test.sh
First argument: asdfgg
Second argument: bnmbnm
Third argument: cvbnm
OK
root@82a5c3949b49:/code#
Alright! OK!
It's a shame, but if you see #! / Bin / sh: No such file or directory, check the character code of each file.
test.sh: line 1: #!/bin/sh: No such file or directory
First argument: asdfgg
Second argument: bnmbnm
Third argument: cvbnm
OK
As a beginner, I opened it with Notepad, corrected the character code, and overwrote it. Wrong: UTF-8 (with BOM) Correct: UTF-8
I also want to summarize it in the Dockerfile.
https://github.com/k-ashigaki/DockerStudyFile/tree/master/environment_variable_test
Recommended Posts