bash Dockerfile CMD 未在容器启动时运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35270493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:13:10  来源:igfitidea点击:

Dockerfile CMD not running at container start

bashdockerboot2dockerdockerfile

提问by James Kirkby

So i've written a Dockerfile for a project, i've defined a CMD to run on starting the container to bootstrap the application.

所以我为一个项目编写了一个 Dockerfile,我定义了一个 CMD 来在启动容器时运行以引导应用程序。

The Dockerfile looks like

Dockerfile 看起来像

# create our mount folders and volumes
ENV MOUNTED_VOLUME_DIR=sites
RUN mkdir /$MOUNTED_VOLUME_DIR
ENV PATH=$MOUNTED_VOLUME_DIR/sbin:$MOUNTED_VOLUME_DIR/common/bin:$PATH
RUN chown -Rf www-data:www-data /$MOUNTED_VOLUME_DIR

# Mount folders
VOLUME ["/$MOUNTED_VOLUME_DIR/"]

# Expose Ports
EXPOSE 443

# add our environment variables to the server
ADD ./env /env

# Add entry point script
ADD ./start.sh /usr/bin/startContainer
RUN chmod 755 /usr/bin/startContainer

# define entrypoint command
CMD ["/bin/bash", "/usr/bin/startContainer"]

The start.sh script, does some git stuff like cloning the right repo, setting environment vars, as well as starting supervisor.

start.sh 脚本执行一些 git 操作,例如克隆正确的存储库、设置环境变量以及启动主管。

The start script begins with this

启动脚本以此开头

#!/bin/bash

now=$(date +"%T")
echo "Container Start Time : $now" >> /tmp/start.txt
/usr/bin/supervisord -n -c /etc/supervisord.conf

I start my new container like this

我像这样启动我的新容器

docker run -d -p expoPort:contPort -t -i -v /$MOUNTED_VOLUME_DIR/$PROJECT:/$MOUNTED_VOLUME_DIR $CONTAINER_ID /bin/bash

when i login to the container i see that supervisor hasn't been started, and neither has nginx or php5-fpm. the /tmp/start.txt file with a timestamp set from the startContainer script doesn't exist, showing its never ran the CMD in the Dockerfile.

当我登录到容器时,我看到主管尚未启动,nginx 或 php5-fpm 也没有启动。带有从 startContainer 脚本设置的时间戳的 /tmp/start.txt 文件不存在,表明它从未在 Dockerfile 中运行 CMD。

Any hints on to get this fixed would be great

任何有关修复此问题的提示都会很棒

回答by Sobrique

This:

这个:

docker run -d -p expoPort:contPort -t -i -v /$MOUNTED_VOLUME_DIR/$PROJECT:/$MOUNTED_VOLUME_DIR $CONTAINER_ID /bin/bash

Says 'run /bin/bash' after instantiating the container. E.g. skip CMD.

实例化容器后说“运行/bin/bash”。例如跳过CMD

Try this:

尝试这个:

docker run -d -p expoPort:contPort -t -i -v /$MOUNTED_VOLUME_DIR/$PROJECT:/$MOUNTED_VOLUME_DIR $CONTAINER_ID