Py学习  »  docker

docker container start命令未获取.bashrc变量

Raymond Cheng • 5 年前 • 2054 次点击  

我正在使用Docker在启动容器时执行一个命令,但是似乎环境变量没有从.bashrc文件中获得,请给我一些建议。 谢谢

Dockerfile我将此添加到.bashrc: echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc

docker-compose.yml文件具有:

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

注意:如果我从container执行echo$pythpath或只是执行python2/usr/bin/supervisord-c/etc/supervisor/supervisor.conf,则没有问题。

系统是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文件

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"]

最后,我用温顺的语言写了一个命令 echo“export pythonpath=$pythonpath:/models/research:/models/research/slim”>>/root/.bashrc 制作/模型/研究可以通过python找到。

有一个python模型 /models/research/object_detection .

我的上司,指挥部 python3 detection.py start_mosquitto_subscrible 如果我启动,找不到对象探测模型 supervisord 只是从Docker编写命令,而不是在Docker容器中执行它。

主管需要python2启动,我的代码需要python3

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40045
 
2054 次点击  
文章 [ 3 ]  |  最新文章 5 年前
ugly_animal
Reply   •   1 楼
ugly_animal    6 年前

尝试启动docker compose命令:

PYTHONPATH="$PYTHONPATH:/models/research:/models/research/slim" docker-compose up -d
BMitch
Reply   •   2 楼
BMitch    6 年前
command: ["python2", "/usr/bin/supervisord", "--nodaemon", "--configuration", "/etc/supervisor/supervisord.conf"]

您提供的命令使用exec语法。见 documentation on CMD (同样适用于 RUN ENTRYPOINT ):

如果使用 CMD ,然后 <command> 将执行 在里面 /bin/sh -c :

FROM ubuntu
CMD echo "This is a test." | wc -

如果你想 <命令> 如果没有外壳,你必须 将命令表示为json数组,并给出 可执行。此数组形式是 CMD . 任何 附加参数必须单独表示为 数组:

FROM ubuntu
CMD ["/usr/bin/wc","--help"]

在您的例子中,您需要一个bash shell来处理 .bashrc 文件,这意味着您需要以下内容:

command: ["/bin/bash", "-c", "python2 /usr/bin/supervisord --nodaemon --configuration /etc/supervisor/supervisord.conf"]

编辑:使用ubuntu:16.04中的/root/.bashrc,您将在文件顶部看到以下内容:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

可以使用此sed命令在此行之前修改文件:

sed -i '4s;^;export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim\n;' /root/.bashrc

我会考虑将它放在用于启动容器的脚本中,而不是破解.bashrc,例如start.sh:

#!/bin/sh
export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim
exec python2 /usr/bin/supervisord --nodaemon --configuration /etc/supervisor/supervisord.conf

然后将其添加到图像中:

COPY start.sh /
RUN chmod 755 /start.sh # if your build server doesn't have this permission set
CMD [ "/start.sh" ]
Shubham Jain
Reply   •   3 楼
Shubham Jain    6 年前

~/.bashrc在以交互方式打开shell之前不会运行,这就是为什么在打开shell时不会出现问题 docker exec 这是交互式的,请参阅bashrc文件的前几行:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

你需要评论这些行。

如果只需要一个环境变量,最好从容器中获取python_path的值,并将完整的变量添加到docker-compose.yml文件中。