Import Dockerfile from different local directory via FROM

If you use Docker 20.10+, you can do this:

# syntax = edrevo/dockerfile-plus

INCLUDE+ {path/to/docker/locally}

RUN ...

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


The best solution i have found to this so far is to use Dockerfile stages

Put all this in a single Dockerfile:

FROM php:8.1-fpm AS base

RUN apt-get update && apt-get install -y \
    libzip-dev libonig-dev \
    libwebp-dev # ...

RUN docker-php-ext-install zip mbstring pdo pdo_mysql gd bcmath sockets opcache soap gmp intl

# Worker
FROM base AS worker
RUN apt install -y supervisor 

# CLI
FROM base AS cli
RUN apt-get install -yq git unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

And then you can use it in docker-compose.yml:

services:
  worker:
    build:
      context: php
      dockerfile: Dockerfile
      target: worker

  cli:
    build:
      context: php
      dockerfile: Dockerfile
      target: cli

This will make the containers build efficiently and the base will be built only once. You can also use the base itself as a target.


Is there a way to import a Docker file from a different directory locally whereas I am able to import it with Docker's FROM command, to create several stages in a build?

No. The FROM instruction is used to import pre-built images only, it can not be used to import a Dockerfile.

If not, would I be able to ADD the other staged Docker file's into the current Docker file?

No. The ADD instruction can only copy files inside the build context (which usually is the current working directory) to containers. You cannot ADD ../something /something.

Or can I simply run FROM {path/to/docker/locally}?

No. But one way that is gonna work for you is, to build the other image first, say its name is first-image:latest, then you can use the COPY instruction to copy files from that image:

COPY --from=first-image:latest /some/file /some/file