Docker 容器启动命令未获取 .bashrc 变量 [英] Docker Container Start Command Did Not Get .bashrc variables
问题描述
我在启动容器时使用 docker 来执行命令,但似乎没有从 .bashrc 文件中获取环境变量,请给我一些建议.谢谢
I'm using docker to execute a command when starting the container but seems the environment variable did not get from the .bashrc file, please give me some advice. thanks
dockerFile 我将它添加到 .bashrc:echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >>/root/.bashrc
dockerFile I add this to .bashrc:
echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc
docker-compose.yml 文件:
docker-compose.yml file with:
command: ["python2", "/usr/bin/supervisord", "--nodaemon", "--configuration", "/etc/supervisor/supervisord.conf"]
PS:如果我 exec echo $PYTHPATH 或只是 exec python2/usr/bin/supervisord -c/etc/supervisor/supervisor.conf 从容器中,没有问题.
PS:if I exec echo $PYTHPATH or just exec python2 /usr/bin/supervisord -c /etc/supervisor/supervisor.conf from container, there have not issues.
系统是 Ubuntu 16.04
The System is Ubuntu 16.04
主管配置:
[program:mosquitto-subscrible]
process_name=%(program_name)s_%(process_num)02d
command=python3 detection.py start_mosquitto_subscrible
autostart=true
autorestart=true
user=root
numprocs=1
directory=/var/www/html/detection
redirect_stderr=true
stdout_logfile=/var/www/html/detection/logs/detection.log
docker-compose.yml
docker-compose.yml
version: '3'
services:
tensorflow:
container_name: object-detection
build:
context: ./tensorflow
dockerfile: Dockerfile
# environment:
# - PYTHONPATH=:/models/research:/models/research/slim
volumes:
- ./www:/var/www/html:cached
- ./tensorflow/supervisor:/etc/supervisor/conf.d
command: ['tail', '-f', '/dev/null']
# command: ["python2", "-c", "/usr/bin/supervisord", "--nodaemon","--configuration", "/etc/supervisor/supervisord.conf"]
总之,我在Dockfile中写了一个命令 echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >>/root/.bashrc
可以通过 PYTHON 找到/models/research.
In conclusion, I write a command in Dockfile echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc
to make /models/research can be found by PYTHON.
有一个python模型/models/research/object_detection
.
there have a python model /models/research/object_detection
.
对于我的主管,命令 python3 detection.py start_mosquitto_subscrible
无法找到 object_detection 模型,如果我仅从 docker-compose 命令启动 supervisord
而不是在 docker 中执行它容器.
with my supervisor, the command python3 detection.py start_mosquitto_subscrible
can't find object_detection model if I start supervisord
just from docker-compose command instead of exec it inside docker container.
supervisord 需要 python2 启动,我的代码需要 python3
推荐答案
~/.bashrc 不会运行,直到 shell 以交互方式打开,这就是为什么当你执行 docker exec
交互时没有问题的原因,查看 bashrc 文件的前几行:
~/.bashrc wont run untill the shell is opened interactively, that's why no issues when you do docker exec
which is interactive, see the first few lines of bashrc file :
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
您需要注释这些行.
如果您只需要一个环境变量,最好从您的容器中获取 PYTHON_PATH 的值,并将完整的变量添加到您的 docker-compose.yml 文件中.
If you just need one Environment variable, better get the value of PYTHON_PATH from your container and add the complete variable to your docker-compose.yml file.
这篇关于Docker 容器启动命令未获取 .bashrc 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!