Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker run -e 使用环境变量传值(字符、数字、json)并用 python 解析_木尧大兄弟的博客 - CSDN 博客_docker run 使用环境变量 #20

Open
customsshen opened this issue Apr 8, 2024 · 0 comments
Labels
NAS 群晖

Comments

@customsshen
Copy link
Owner

第一步,代码编写

Dockerfile

Dockerfile 的后半部分(设置环境变量默认值,以及传到 run.sh 中)
一个坑:ENTRYPOINT 后面不能用 [] 的形式 否则无法读取环境变量值。

# 设置环境默认值
ENV task_id=123
ENV task_param=1

# entrypoint是容器入口 
# 不能用下面[]这种形式,否则读取不到环境变量!!
# ENTRYPOINT  ["/bin/bash", "/code/run.sh"]
ENTRYPOINT  /bin/bash /code/run.sh $task_id $task_param

run.sh

用来输出传过来的环境变量,用来验证环境变量是否生效;并执行 python 脚本

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

main.py

python 脚本,用os.environ读取环境变量。

import os

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

第二步,运行 docker

构建镜像:docker build -t image_name:v1.211020 -f ./Dockerfile .
生成容器并运行:docker run -e task_id="ua084d" -e task_param={"tag": "BERT"} --name MO_CLS_ua084d 9c6ad6da --rm

这里踩的一个坑:
注意:docker run 的 - e 参数要紧跟 run,否则不生效!!
注意:docker run 的 - e 参数要紧跟 run,否则不生效!!
注意:docker run 的 - e 参数要紧跟 run,否则不生效!!

@customsshen customsshen added the NAS 群晖 label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NAS 群晖
Projects
None yet
Development

No branches or pull requests

1 participant