Docker容器启动命令未获取.bashrc变量 [英] Docker Container Start Command Did Not Get .bashrc variables

查看:1361
本文介绍了Docker容器启动命令未获取.bashrc变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动容器时,我正在使用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:

命令:[ python2, / usr / bin / supervisord,-nodaemon,-configuration, / etc / supervisor / supervisord.conf]

PS:如果我执行echo $ PYTHPATH或只是执行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

supervisor config:

supervisor config:

[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导出PYTHONPATH = $ PYTHONPATH:/ models / research:/ models / research / slim>> PYTHON可以找到/root/.bashrc 来创建/ 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 supervisord ,则$ c>找不到object_detection模型。

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不会运行,直到以交互方式打开外壳,这就是为什么当您进行交互式的 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

您需要在以下行中添加注释。

you need to comment these lines.

如果只需要一个Environment变量,最好从容器中获取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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆