Docker Container Start Command Did Not Get .bashrc variables

孤者浪人 提交于 2019-12-08 08:04:15

问题


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 I add this to .bashrc: echo "export PYTHONPATH=$PYTHONPATH:/models/research:/models/research/slim" >> /root/.bashrc

docker-compose.yml file with:

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.

The System is 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

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.

there have a python model /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.

supervisord need python2 to start, my code needs python3


回答1:


~/.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.

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.




回答2:


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

The command you've provided is using the exec syntax. See the documentation on CMD (the same applies to RUN and ENTRYPOINT):

If you use the shell form of the CMD, then the <command> will execute in /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. This array form is the preferred format of 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:

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:

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

You can modify the file before this line with this sed command:

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, e.g. a 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" ]



回答3:


Try to start docker compose with command:

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


来源:https://stackoverflow.com/questions/52940777/docker-container-start-command-did-not-get-bashrc-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!