CodelyTV / php-ddd-example

🐘🎯 Hexagonal Architecture + DDD + CQRS in PHP using Symfony 7
https://pro.codely.tv/library/ddd-en-php
2.96k stars 1.08k forks source link

Optimize PHP Dockerfile with multistage builds (builder pattern) #61

Closed JavierCane closed 5 years ago

JavierCane commented 5 years ago

We've already added Docker Compose to this repository thanks to the @sdecandelario PR: https://github.com/CodelyTV/cqrs-ddd-php-example/pull/56

However, we had an interesting discussion about how we should implement the etc/infrastructure/php/Dockerfile for the PHP container specifically regarding the provision (composer install) phase.

Provisioning in an early stage of the Dockerfile would allow us to perform some optimizations 😬

One of these optimizations would be applying the builder pattern or Multistage builds. You can read more information about them in the "Docker: De 0 a deploy" CodelyTV Pro course by @fiunchinho (here you have the course repository with some examples), and a blog post about this technic and its benefits.

However, we've tried to apply this strategy and we've miserably failed (🤣). The reason why we haven't been able to do so is because the PHP Dockerfile is in a subdirectory and it can't copy the composer.json file (it is inside the parent folder).

We don't want to move the PHP Dockerfile to the project root folder because it would add too much noise, but we don't know the best way to have the needed files available. Any suggestions?

JavierCane commented 5 years ago

First attempt:

FROM composer as builder

COPY composer.json composer.lock /app/
RUN composer install  \
    --ignore-platform-reqs \
    --no-ansi \
    --no-autoloader \
    --no-interaction \
    --no-scripts

COPY . /app/
RUN composer dump-autoload --optimize --classmap-authoritative

FROM php:7.2-fpm-alpine
WORKDIR /app

RUN pecl install amqp apcu xdebug-2.6.0 \
    && docker-php-ext-enable xdebug \
    && docker-php-ext-install pdo pdo_mysql

COPY --from=builder /app /var/www/
COPY php.ini xdebug.ini /usr/local/etc/php/
fiunchinho commented 5 years ago

Having a Dockerfile on the root of the project allows potentials developers to just docker build and docker run the repository, without having to know where the hell the Dockerfile is. I don't see how adding one more file to the root of the project adds noise.

JavierCane commented 5 years ago

PR opened moving the PHP Dockerfile to the root directory: https://github.com/CodelyTV/cqrs-ddd-php-example/pull/63

We can go ahead with this approach by the moment. However, when I was referring to the fact of adding more noise because of adding this Dockerfile to the root directory, I was thinking about adding different Dockerfile-whatever in a near future. We could end up having different PHP Dockerfiles depending on the environment (not installing dev dependencies neither generating the dev classes autoload classsmap), so it could end up being a little mess not being able to group these files into a subfolder.

At the end of the day, we're using Docker Compose, so if we want to locate where the Docker magic happens, we would always have the docker-compose.yml in the root directory 🙂

JavierCane commented 5 years ago

Issue closed by PR: https://github.com/CodelyTV/cqrs-ddd-php-example/pull/63