【问题标题】:Excecute .sh file on start of Docker container and let container run在 Docker 容器启动时执行 .sh 文件并让容器运行
【发布时间】:2019-08-07 18:08:45
【问题描述】:

我有一个服务于网络服务器的 docker 容器。在容器的每次启动时,我都想执行一个小 shell 脚本。必须执行的脚本只有一个语句。

/var/www/html/app/Console/cake schema update -y

为了实现这一点,我创建了一个名为 schemaupdate.sh 的 .sh 文件,我使用 dockerfile 将其复制到 docker 容器中的 /etc/init.d 文件夹中。此外,我使其可执行并将其注册到启动。

COPY schemaupdate.sh /etc/init.d/schemaupdate.sh
chmod 755 /etc/init.d/schemaupdate.sh
update-rc.d schemaupdate.sh defaults

文件已成功复制到容器中。但是,当 docker 容器启动时,脚本不会执行。当我手动调用 sh 文件时,一切运行正常。

如何实现文件/语句在容器每次启动时执行?重要的是,脚本在启动时执行,并且容器(因此网络服务器)仍然继续运行!该脚本仅进行少量更新检查,检查后网络服务器继续运行。

容器是基于 debian 的容器。这是初始 dockerfile。

#start with base Image from php 
FROM php:7.3-apache

#install system dependencies and enable PHP modules

RUN apt-get update && apt-get install -y \
      libicu-dev \
      libpq-dev \
      libmcrypt-dev \
      mysql-client \
      git \
      zip \
      unzip \
    && rm -r /var/lib/apt/lists/* \
    && docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
    && docker-php-ext-install \
      intl \
      mbstring \
      pcntl \
      pdo_mysql \
      pdo_pgsql \
      pgsql \
      opcache

#     zip \
#     mcrypt \

#configure imap for mails
RUN apt-get update && \
    apt-get install -y \
        libc-client-dev libkrb5-dev && \
    rm -r /var/lib/apt/lists/*

RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
docker-php-ext-install -j$(nproc) imap


#install mcrypt
RUN apt-get update \
    && apt-get install -y libmcrypt-dev \
    && rm -rf /var/lib/apt/lists/* \
    && pecl install mcrypt-1.0.2 \
    && docker-php-ext-enable mcrypt

#install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer

#set our application folder as an environment variable
ENV APP_HOME /var/www/html

#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data

#change the web_root to cakephp /var/www/html/webroot folder
#RUN sed -i -e "s/html/html\/webroot/g" /etc/apache2/sites-enabled/000-default.conf

# enable apache module rewrite
RUN a2enmod rewrite

#copy source files and run composer
#COPY src/ /var/www/html
#COPY src/ $APP_HOME

# install all PHP dependencies
#RUN composer install --no-interaction

#SET Volume
VOLUME /var/www/html/

#change ownership of our applications
RUN chown -R www-data:www-data $APP_HOME

#SET ENV VARIABLES

COPY schemaupdate.sh /etc/init.d/schemaupdate.sh
chmod 755 /etc/init.d/schemaupdate.sh
update-rc.d schemaupdate.sh defaults

EXPOSE 80

【问题讨论】:

    标签: shell docker startup


    【解决方案1】:

    /etc/init.d/ 不相关。容器不是具有重量级 SysV 初始化风格启动序列的成熟操作系统。他们运行一个命令,就是这样。

    您应该将该命令作为 RUN 语句添加到 Dockerfile 中,以便将其结果烘焙到映像中,或者您应该让容器的 CMDENTRYPOINT 指令直接调用它。

    【讨论】:

      【解决方案2】:

      我终于使用了入口点。我删除了 COPY、chmod 和 update-rc。入口点如下所示。

      ENTRYPOINT [ "sh", "-c", "/var/www/html/app/Console/cake schema update -y && /var/www/html/app/Console/cake schema update -y && /usr/sbin/apachectl -D FOREGROUND"]
      

      它首先启动更新语句。完成后(因此终止),调用 apachectl 以保持网络服务器运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-15
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 2023-03-09
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        相关资源
        最近更新 更多