简体   繁体   English

Docker容器启动命令没有获取.bashrc变量

[英]Docker Container Start Command Did Not Get .bashrc variables

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. 我在启动容器时使用docker来执行命令,但似乎环境变量没有从.bashrc文件中获取,请给我一些建议。 thanks 谢谢

dockerFile I add this to .bashrc: echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc dockerFile我把它添加到.bashrc: echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc

docker-compose.yml file with: docker-compose.yml文件:

command: ["python2", "/usr/bin/supervisord", "--nodaemon", "--configuration", "/etc/supervisor/supervisord.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. PS:如果我从容器中执行echo $ PYTHPATH或只执行python2 / usr / bin / supervisord -c /etc/supervisor/supervisor.conf,则没有问题。

The System is Ubuntu 16.04 该系统是Ubuntu 16.04

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 泊坞窗,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"]

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. 总之,我在Dockfile中写了一个命令echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc make / models / research可以在PYTHON找到。

there have a python model /models/research/object_detection . 有一个python模型/models/research/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. 与我的主管一起,命令python3 detection.py start_mosquitto_subscrible找不到object_detection模型,如果我从docker-compose命令启动supervisord而不是在docker容器中执行它。

supervisord need python2 to start, my code needs python3 supervisord需要python2启动,我的代码需要python3

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

The command you've provided is using the exec syntax. 您提供的命令是使用exec语法。 See the documentation on CMD (the same applies to RUN and ENTRYPOINT ): 请参阅有关CMD文档 (同样适用于RUNENTRYPOINT ):

If you use the shell form of the CMD , then the <command> will execute in /bin/sh -c : 如果你使用CMD的shell形式,那么<command>将在/bin/sh -c执行:

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

If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable. 如果要在没有shell的情况下运行<command> ,则必须将该命令表示为JSON数组并提供可执行文件的完整路径。 This array form is the preferred format of CMD . 此数组形式是CMD的首选格式。 Any additional parameters must be individually expressed as strings in the array: 任何其他参数必须在数组中单独表示为字符串:

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

In your case, you want a bash shell to process the .bashrc file, which means you need something along the lines of: 在您的情况下,您需要一个bash shell来处理.bashrc文件,这意味着您需要以下内容:

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

Edit: with the /root/.bashrc in ubuntu:16.04, you'll see the following at the top of the file: 编辑:使用ubuntu:16.04中的/root/.bashrc,您将在文件顶部看到以下内容:

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

You can modify the file before this line with this sed command: 您可以使用此sed命令修改此行之前的文件:

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

I'd consider placing this in a script used to start the container instead of hacking the .bashrc, eg a start.sh: 我会考虑将其放在用于启动容器的脚本中,而不是黑客攻击.bashrc,例如start.sh:

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

And then add that to your image with: 然后将其添加到您的图像:

COPY start.sh /
RUN chmod 755 /start.sh # if your build server doesn't have this permission set
CMD [ "/start.sh" ]

~/.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 : 〜/ .bashrc不会运行,直到shell以交互方式打开,这就是为什么当你执行docker exec时没有问题,这是交互式的,请参阅bashrc文件的前几行:

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

you need to comment these lines. 你需要评论这些行。

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. 如果您只需要一个Environment变量,最好从容器中获取PYTHON_PATH的值,并将完整变量添加到docker-compose.yml文件中。

尝试使用命令启动docker compose:

PYTHONPATH="$PYTHONPATH:/models/research:/models/research/slim" docker-compose up -d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM