程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

docker run -e uses environment variables to pass values ​​(characters, numbers, json) and parse them with python

編輯:Python

The first step, code writing

Dockerfile

The second half of the Dockerfile (set the default value of the environment variable, and pass it to run.sh)
A pit: The form of [] cannot be used after ENTRYPOINT, otherwise the environment cannot be readvariable.

# set environment defaultsENV task_id=123ENV task_param=1# entrypoint is the container entry # The following [] form cannot be used, otherwise the environment variable cannot be read!!# ENTRYPOINT ["/bin/bash", "/code/run.sh"]ENTRYPOINT /bin/bash /code/run.sh $task_id $task_param

run.sh

Used to output the passed environment variables, used to verify whether the environment variables are valid; and execute the python script

echo "task_id = ${task_id}"echo "task_param = ${task_param}"python /code/main.py

main.py

python script, use os.environ to read environment variables.

import ostask_id = os.environ['task_id']task_param = os.environ['task_param']

The second step, run docker

Build the image: docker build -t image_name:v1.211020 -f ./Dockerfile .
Build the container and run: docker run -e task_id="ua084d" -etask_param={"tag": "BERT"} --name MO_CLS_ua084d 9c6ad6da --rm

A pit I stepped on here:
Note: The -e parameter of docker run must follow run, otherwise it will not take effect!!
Note: The -e parameter of docker run must follow run, otherwise it will not take effect!!
Note: The -e parameter of docker run must follow run, otherwiseNot valid!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved