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)

Please authenticate to join the conversation.

Upvoters
Status

βœ… Done

Board

πŸ’‘ Feature Request

ETA
Jun 30, 2025
Date

Over 3 years ago

Author

Martin PAUCOT

Subscribe to post

Get notified by email when there are changes.