Skip to main content

Define target for multi-stage Dockerfile

When configuring an application, we should be able to define the target to work properly with multi-staged Dockerfile
https://docs.docker.com/develop/develop-images/multistage-build/

Why is it a must have ?

  • We can easily use the same Dockerfile for local environment and remote environments

  • We can use the same image for different applications

  • It improves the cache usability

A simple use case

Let's say we have a Laravel application and we want to have three apps:

  • The php-fpm app (contains the full application with php-fpm running)

  • The nginx app (contains only the public folder of the app with nginx running)

  • The worker app (contains the full application with php artisan worker:run command)

Without the multi-stage feature, we must have three Dockerfiles where we could only have one:

FROM php:fpm-alpine AS build

// Build the app

FROM build AS app

CMD ["php-fpm"]

FROM build AS worker

CMD ["php artisan worker:run"]

FROM nginx:latest AS nginx

COPY --from=build /var/www/html/public /var/www/html/public

The we could just define the target (app, worker and nginx)

Status: ✅ Done1 comment

Log in to comment and vote

Comments1

  • Alessandro Carrano

    Team•

    Apr 24, 2025

    This has been released! you can now specify the target of your multi-stage dockerfile