dockerportainer

Split up Dockerfile in two Images


I have a Dockerfile including e.g. apache, further installations and my code that is copied to /var/www/html to create a project. After I created the image locally, I export it as a .tar file and upload the image to portainer. Portainer is my productive environment. 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. Portainer has furthermore multiple managers, which causes that I have to upload it to each manager.

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. 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. Then, for each Version update, I only have to upload the image with the new code and can maybe somehow refere to the 1.0-BaseInstall like e.g. From 1.0-BaseInstall. If the BaseInstall changes (which is really rarely), I could just create a new image for that.

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

Solution

  • You can break this into multiple docker files and it would be viable.

    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://docs.docker.com/build/building/multi-stage/

    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/

    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. You shouldn't need to upload your code anywhere. Just the built image to whatever docker repo you store your images on.

    Edit: Add in link for creating your own docker repo https://docs.docker.com/registry/deploying/

    Edit 2: To better answer your question

    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