Can a Dockerfile extend another one?

DockerDockerfile

Docker Problem Overview


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) ?

Docker Solutions


Solution 1 - Docker

Using multi-stage build is definitely one part of the answer here.

docker-compose v3.4 target being the second and last.

Here is a example to have 2 containers (1 normal & 1 w/ xdebug installed) living together :

Dockerfile

FROM php:7-fpm AS php_base 
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

FROM php_base AS php_test

RUN pecl install xdebug 
RUN docker-php-ext-enable xdebug

docker-compose.yml

version: '3.4'

services:
  php:
    build:
      context: ./
      target: php_base

  php_test:
    build:
      context: ./
      target: php_test
  
# ...

Solution 2 - Docker

If you don't want to tag your first Dockerfile in order to use it in a FROM instruction in your next Dockerfile, and you are using Docker 20.10+, you can also do this:

# syntax = edrevo/dockerfile-plus

INCLUDE+ Dockerfile.base

RUN whatever

The INCLUDE+ instruction gets imported by the first line in the Dockerfile. You can read more about the dockerfile-plus at https://github.com/edrevo/dockerfile-plus

Solution 3 - Docker

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSylvainView Question on Stackoverflow
Solution 1 - DockerCethyView Answer on Stackoverflow
Solution 2 - DockeredrevoView Answer on Stackoverflow
Solution 3 - DockerlarsksView Answer on Stackoverflow