将 Dockerfile 拆分为两个图像

Split up Dockerfile in two Images

我有一个 Dockerfile,包括例如apache,进一步安装和我的代码被复制到 /var/www/html 以创建一个项目。在本地创建图像后,我将其导出为 .tar 文件并将图像上传到 portainer。 Portainer 是我的生产环境。但是,每次当我想更新版本和使用我的软件的服务时,我都必须更新大小为 800MB 的全新映像。 Portainer 还有多个管理器,这导致我必须将它上传到每个管理器。

因为一切都保持不变,除了我的代码被复制部分插入COPY HRmAppBare/ /var/www/html,我想到了这个想法,如果可以创建两个图像。整个安装的一个图像(假设:1.0-BaseInstall)和第二个图像(假设:1.9-backend)仅存储我的代码。然后,对于每个版本更新,我只需要上传带有新代码的图像,并且可能以某种方式引用 1.0-BaseInstall,例如From 1.0-BaseInstall。如果 BaseInstall 发生变化(这种情况很少发生),我可以为此创建一个新图像。

因为我找不到任何相关信息,所以我想知道这种方法是否适用,如果适用,我必须如何构建它?

#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 \
      vim \
      cron \
      libpq-dev \
      libmcrypt-dev \
      default-mysql-client \
      zip \
      unzip \
      libzip-dev \
    && docker-php-ext-configure zip --with-libzip \
    && docker-php-ext-install zip \
    && 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 \
      opcache \
      gettext \
        && pecl install mcrypt-1.0.2 \
        && docker-php-ext-enable mcrypt \
        && rm -rf /var/lib/apt/lists/*

#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 composer
#RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer

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

#copy the source code (Can this be in a 2nd image?)
COPY HRmAppBare/ /var/www/html

#Update apache2.conf
RUN   echo 'Alias ${COOKIE_PATH} "/var/www/html"' >> /etc/apache2/apache2.conf
RUN   echo 'Alias ${COOKIE_PATH}/ "/var/www/html/"' >> /etc/apache2/apache2.conf

#change ownership of our applications
RUN chown -R www-data:www-data /var/www/html/

ENTRYPOINT [ "sh", "-c", "rm /var/www/html/app/tmp/cache/models/* && rm /var/www/html/app/tmp/cache/persistent/* && /var/www/html/app/Console/cake schema update -y && apache2-foreground"]

EXPOSE 80

您可以将其分解为多个 docker 文件,这将是可行的。

如果您没有其他消费者使用基本映像,这可能会造成混淆,并且只会增加更多开销以管理您的基本映像和应用程序的版本,但这当然是可行的。

研究多阶段构建可能是值得的。 https://github.com/docker/docker.github.io/blob/master/develop/develop-images/multistage-build.md

If the objects on the file system that Docker is about to produce are unchanged between builds, reusing a cache of a previous build on the host is a great time-saver. It makes building a new container really, really fast.

https://thenewstack.io/understanding-the-docker-cache-for-faster-builds/

当你说

我不确定

Then, for each Version update, I only have to upload the code

我可能误解了这个短语,但我建议在您的构建盒上完成所有构建,并将版本化图像推送到 docker 存储库,以便您的生产盒在您使用时从中提取'准备好获取下一个版本。您不需要将代码上传到任何地方。只是构建图像到任何存储图像的 docker 回购协议。

编辑:添加 link 以创建您自己的 docker 存储库 https://docs.docker.com/registry/deploying/

编辑 2:为了更好地回答您的问题

FROM php:7.3-apache AS base
//...rest of your dockerfile until copy

FROM base
#copy the source code (Can this be in a 2nd image?)
COPY HRmAppBare/ /var/www/html

#Update apache2.conf
RUN   echo 'Alias ${COOKIE_PATH} "/var/www/html"' >> /etc/apache2/apache2.conf
RUN   echo 'Alias ${COOKIE_PATH}/ "/var/www/html/"' >> /etc/apache2/apache2.conf

#change ownership of our applications
RUN chown -R www-data:www-data /var/www/html/

ENTRYPOINT [ "sh", "-c", "rm /var/www/html/app/tmp/cache/models/* && rm /var/www/html/app/tmp/cache/persistent/* && /var/www/html/app/Console/cake schema update -y && apache2-foreground"]

EXPOSE