Docker Container Start Command Did Not Get .bashrc variables

本秂侑毒 提交于 2019-12-07 04:38:22

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

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

Try to start docker compose with command:

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