dockerdockerfile

Can a Dockerfile extend another one?


I have a Dockerfile for PHP like this :

FROM php:7-fpm
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && \
    apt-get install -y git libicu-dev libmagickwand-dev libmcrypt-dev libcurl3-dev jpegoptim
RUN pecl install imagick && \
    docker-php-ext-enable imagick

RUN docker-php-ext-install intl
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install opcache
RUN docker-php-ext-install mcrypt
RUN docker-php-ext-install curl
RUN docker-php-ext-install zip

And I'd like to create another Dockerfile, based on the first one, but with some PHP extensions added (for dev purpose) : Xdebug and other stuffs.

Can I create a "dev" Dockerfile that extends my main Dockerfile (without rewrite it) ?


Solution

  • That is exactly what your FROM php:7-fpm is doing: extending the Dockerfile from the php image (with 7-fpm tag) with the contents of your Dockerfile.

    So after building an image from your Dockerfile:

    docker build -t my-php-base-image .
    

    You can extend that by creating a new Dockerfile that starts with:

    FROM my-php-base-image