I want to create a multistage build process whereas each of the docker files are nested inside their own directories locally with their corresponding dependencies that are ADDed in for each Docker file. 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?
If not, would I be able to ADD
the other staged Docker file's into the current Docker file and then use FROM
inside the docker container, deleting it after it is added and used in FROM
?
Maybe I am thinking about multistage builds the wrong way.
Or can I simply run FROM {path/to/docker/locally}
? This is not working for me.
It turns out that you cannot include files outside Docker's build context. However, you can copy files from the Dockerfile's parent directory.
With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.
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
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
The best solution i have found to this so far is to use Dockerfile stages
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With