Lina
Lina

Reputation: 15

Rebuild only code in docker

I have quite big yii2 application that takes a few minutes to build in docker. Is there a way to build code only, without "re-installing" everything each time? How do I speed up developing/debugging of dockerized yii2 app?

Now I do this:

docker build -t myapp:mytag . docker run --name myapp -p 8000:8000 myapp:mytag

My Dockerfile:

FROM php:5.6-apache

COPY . /var/www/html/

ENV APACHE_DOCUMENT_ROOT /var/www/html/web

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf

RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

RUN apt-get update && \
    apt-get install -y curl nano unzip zlib1g-dev git && \
    docker-php-ext-install pdo pdo_mysql zip && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN cd /var/www/html && composer install


RUN cd /var/www/html/ && mkdir web/assets/
RUN chmod 777 /var/www/html/web/assets/

RUN mv /var/www/html/vendor/bower-asset/ /var/www/html/vendor/bower/

Upvotes: 0

Views: 680

Answers (1)

BMitch
BMitch

Reputation: 265055

Docker will reused cached build steps that it has previously performed and have not changed. However, once you reach a step that breaks the cache, all subsequent steps have to rerun since the cache includes a dependency on the previous step. Therefore, the cache is order dependent and you have the following as one of your very first steps:

COPY . /var/www/html/

Every time you change your code, that line will have to be rerun, which then forces the apt-get lines to rerun as well. By reordering your install you'll see a large speed up:

FROM php:5.6-apache

ENV APACHE_DOCUMENT_ROOT /var/www/html/web

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf

RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

RUN apt-get update && \
    apt-get install -y curl nano unzip zlib1g-dev git && \
    docker-php-ext-install pdo pdo_mysql zip && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# move this line to as late in the build as possible
COPY . /var/www/html/

RUN cd /var/www/html && composer install

RUN cd /var/www/html/ && mkdir web/assets/
RUN chmod 777 /var/www/html/web/assets/

RUN mv /var/www/html/vendor/bower-asset/ /var/www/html/vendor/bower/

Upvotes: 2

Related Questions