Docker Container Start Command Did Not Get .bashrc variables

前端 未结 3 551
慢半拍i
慢半拍i 2021-01-16 13:02

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 <

3条回答
  •  甜味超标
    2021-01-16 13:50

    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 will execute in /bin/sh -c:

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

    If you want to run your 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" ]
    

提交回复
热议问题