I set the environment variable with Docker and displayed it in Python

Target

Print the environment variables set during the docker run command with Python executed by the shell script in the container.

Assumption

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.

environment

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

Set up a Python container for the time being

$ 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 shell scripts and Python files

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 image.png

Outlook

I also want to summarize it in the Dockerfile.

I posted it on the code I used this time on GitHub (copype is faster, but w)

https://github.com/k-ashigaki/DockerStudyFile/tree/master/environment_variable_test

Recommended Posts

I set the environment variable with Docker and displayed it in Python
Install CaboCha in Ubuntu environment and call it with Python.
I tried running the offline speech recognition system Julius with python in the Docker virtual environment
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
[Python] I introduced Word2Vec and played with it.
Prepare the execution environment of Python3 with Docker
Environment maintenance made with Docker (I want to post-process GrADS in Python
Build PyPy and Python execution environment with Docker
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Note that I understand the least squares algorithm. And I wrote it in Python.
Japanese can be used with Python in Docker environment
Prepare python3 environment with Docker
I checked the reference speed when using python list, dictionary, and set type in.
I want to replace the variables in the python template file and mass-produce it in another file.
It is easy to execute SQL with Python and output the result in Excel
[Python] I installed the game from pip and played it
I tried programming the chi-square test in Python and Java.
I wrote python3.4 in .envrc with direnv and allowed it, but I got a syntax error
Build Mysql + Python environment with docker
Recursively get the Excel list in a specific folder with python and write it to Excel.
I liked the tweet with python. ..
I played with PyQt5 and Python3
I wrote the queue in Python
I also tried to imitate the function monad and State monad with a generator in Python
I wrote the stack in Python
I set the python operating environment on OSX Yosemite (pyenv + pip)
Life game with Python [I made it] (on the terminal & Tkinter)
I compared the speed of Hash with Topaz, Ruby and Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Build and try an OpenCV & Python environment in minutes using Docker
[Python3] Save the mean and covariance matrix in json with pandas
I want to create a pipfile and reflect it in docker
Install pip in Serverless Framework and AWS Lambda with Python environment
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Is the Serverless environment more than 600 times slower? ~ I tried benchmarking with Go, Node.js and Python! ~
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
[Python] Set the graph range with matplotlib
Build Jupyter Lab (Python) environment with Docker
Using venv in Windows + Docker environment [Python]
Behind the flyer: Using Docker with Python
[Python] Get the variable name with str
Display Python 3 in the browser with MAMP
Create Python + uWSGI + Nginx environment with Docker
Dealing with "years and months" in Python
I installed and used Numba with Python3.5
Launch environment with LineBot + Heroku + Docker + Python
Clean python environment with pythonz and virtualenv
Find it in the procession and edit it
Introduced sip-4.14 in python3.2.2 environment with MacOS 10.7.4
Fill the string with zeros in python and count some characters from the string
I replaced the numerical calculation of Python with Rust and compared the speed
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I wanted to use jupyter notebook with docker in pip environment (opticspy)
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
How to get the date and time difference in seconds with python
Enclose the cat result in double quotes and put it in a variable
I vectorized the chord of the song with word2vec and visualized it with t-SNE
The file name was bad in Python and I was addicted to import
Get and convert the current time in the system local timezone with python