简体   繁体   English

将 Dockerfile 拆分为两个图像

[英]Split up Dockerfile in two Images

I have a Dockerfile including eg apache, further installations and my code that is copied to /var/www/html to create a project.我有一个 Dockerfile 包括例如 apache,进一步的安装和我的代码复制到 /var/www/html 以创建一个项目。 After I created the image locally, I export it as a.tar file and upload the image to portainer.在本地创建图像后,我将其导出为 .tar 文件并将图像上传到 portainer。 Portainer is my productive environment. Portainer 是我的生产环境。 However, everytime when I want to update the Version and the services that are using my software, I have to update the whole new image which has a size of 800MB.但是,每次我想更新版本和正在使用我的软件的服务时,我都必须更新大小为 800MB 的全新映像。 Portainer has furthermore multiple managers, which causes that I have to upload it to each manager. Portainer 还有多个经理,这导致我必须将其上传给每个经理。

Because everything keeps the same, except my code that is inserted by the copy part COPY HRmAppBare/ /var/www/html , I thought about the idea, If it is possible to create two images.因为一切都保持不变,除了我的代码是由复制部分COPY HRmAppBare/ /var/www/html插入的,我想到了这个想法,如果可以创建两个图像。 One Image for the whole Installations (let us say: 1.0-BaseInstall) and a second Image (let us say: 1.9-backend) that only stores my code.整个安装的一个映像(假设:1.0-BaseInstall)和仅存储我的代码的第二个映像(假设:1.9-backend)。 Then, for each Version update, I only have to upload the code and can maybe somehow refere to the 1.0-BaseInstall like eg From 1.0-BaseInstall .然后,对于每个版本更新,我只需要上传代码并且可以以某种方式引用 1.0-BaseInstall ,例如From 1.0-BaseInstall If the BaseInstall changes (which is really rarely), I could just create a new image for that.如果 BaseInstall 发生变化(这真的很少),我可以为此创建一个新图像。

Because I could not find anything about that, I want to know, if this approach is applicable and if yes, how I have to build this?因为我找不到任何相关信息,所以我想知道这种方法是否适用,如果适用,我必须如何构建它?

#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

You can break this into multiple docker files and it would be viable.您可以将其分解为多个 docker 文件,这将是可行的。

If you have no other consumers of the base image it may create confusion and just add more overhead to have to manage versions for your base image and your application but it is certainly viable.如果您没有其他基本映像的使用者,这可能会造成混乱,并且只会增加更多开销来管理基本映像和应用程序的版本,但它肯定是可行的。

It may be worth it to look into multi-stage builds.研究多阶段构建可能是值得的。 https://github.com/docker/docker.github.io/blob/master/develop/develop-images/multistage-build.md 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.如果 Docker 即将生成的文件系统上的对象在构建之间没有变化,那么在主机上重用先前构建的缓存可以节省大量时间。 It makes building a new container really, really fast.它使构建新容器变得非常非常快。

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

I'm unsure when you say我不确定你什么时候说

Then, for each Version update, I only have to upload the code然后,对于每个版本更新,我只需要上传代码

I may be misunderstanding the phrase but instead I would suggest having all of the builds done on your build box and have the versioned image pushed to a docker repo for your production box to pull from when you're ready to grab the next version.我可能误解了这个短语,但我建议在您的构建盒上完成所有构建,并将版本化图像推送到 docker 存储库,以便您的生产盒在您准备好获取下一个版本时从中提取。 You shouldn't need to upload your code anywhere.您不需要在任何地方上传您的代码。 Just the built image to whatever docker repo you store your images on.只是构建图像到您存储图像的任何 docker 存储库。

Edit: Add in link for creating your own docker repo https://docs.docker.com/registry/deploying/编辑:添加链接以创建您自己的 docker 存储库https://docs.docker.com/registry/deploying/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM