Docker容器立即运行并退出

时间:2019-11-06 15:24:13

标签: docker dockerfile

我一直在尝试创建一个泊坞窗文件,该文件创建一个可以存储html和php页面的网络服务器。

我需要构建映像,该映像在运行时会在启动时执行一次shell脚本。

我一直在尝试使用ENTRYPOINT进行此操作,但我发现容器正在运行,但随后立即退出。

我要求将其编码在dockerfile中,因为映像是使用Container-Optimized OS从VM上的GCP直接运行在VM上的,该容器在VM启动时运行docker映像(因此我无法选择使用docker run -c)。

这是我目前的尝试。

FROM php:7.2-apache
COPY / /var/www/html/
EXPOSE 80

ADD start_container.sh /usr/bin/start_container
RUN chmod +x /usr/bin/start_container

ENTRYPOINT ["start_container"]

我也尝试过使用

ENTRYPOINT ["docker-php-entrypoint && start_container"]; sleep infinity

CMD ["docker-php-entrypoint"]

目前,bash脚本仅在创建html文件。

#! /usr/bin/env bash

cat>/var/www/html/indextest.html

3 个答案:

答案 0 :(得分:1)

您不需要覆盖CMD中的base image start appache作为https://www.life2coding.com/convert-image-frames-video-file-using-opencv-python/entrypointCMD,这就是为什么容器在执行bash脚本后立即死亡只会创建index.html。容器的寿命就是入口点的寿命,因此入口点应该运行一个长时间运行的进程来保持它们的运行。

#!/usr/bin/env bash

echo "hello world" > /var/www/html/indextest.html

在上述情况下,容器一旦创建index.html,就会退出。

解决方案

#!/usr/bin/env bash
echo "hello" > /var/www/html/index.html
exec apache2-foreground

Dockerfile

FROM php:7.2-apache
EXPOSE 80
ADD start_container.sh /usr/bin/start_container
RUN chmod +x /usr/bin/start_container
ENTRYPOINT ["start_container"]

然后

docker build -t test . && docker run -dit -p 8090:80 --rm abc && sleep 2 &&  curl localhost:8090

您将从终端中的容器中看到世界。

但最好将index.html用于docker构建时间,这样就不必覆盖入口点或CMD。

FROM php:7.2-apache
RUN echo "hello world" > /var/www/html/index.html
EXPOSE 80

答案 1 :(得分:0)

如果“运行”表示bash提示符,则可以执行以下操作:

docker build -t [NAME]
docker run -it [NAME] /bin/bash

这将提示您输入容器bash。要检查容器是否已构建,可以执行docker ps -a

答案 2 :(得分:0)

我面临的问题是因为apache Web服务器将从脚本启动,然后在脚本中运行命令,该命令将完成然后关闭容器。

解决方案是将错误日志记录写入脚本中,并通过添加等待服务来使apache服务保持运行,这将使服务永远保持活动状态:等待“ $ START_WS_PID”将使最后一个服务PID保持运行。

# SIGTERM-handler
term_handler() {
  exit 143; # 128 + 15 -- SIGTERM
}
# setup handlers
trap 'term_handler' SIGTERM


function exportBoolean {
    if [ "${!1}" = "**Boolean**" ]; then
            export ${1}=''
    else 
            export ${1}='Yes.'
    fi
}


# Start Job Server
if [ $LOG_LEVEL == 'debug' ]; then
    /usr/sbin/apachectl -DFOREGROUND -k start -e debug &
    START_WS_PID="$!"
else
    &>/dev/null /usr/sbin/apachectl -DFOREGROUND -k start &
    START_WS_PID="$!"
    echo $!
fi
echo $START_WS_PID
**Do something on the command line**
wait "$START_WS_PID"