'Import Dockerfile from different local directory via FROM
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.
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
If I understand the question correctly, what you are trying to achieve is to build an image and then use the same image and build something (the next »stage«) on top of it.
Docker supports this concept, but not by importing another Dockerfile.
Dockerfiles and the directory they are in are meant to be self-contained, i. e. the only things you need to build the image are these directory contents. Therefore, you cannot load other Dockerfiles from other directories.
The way to go would be to build a base image, e. g. myappimage
, using a Dockerfile myappimage/Dockerfile
. After you have built it, you can refer to this base image in another Dockerfile (e. g. mytestingimage/Dockerfile
) using FROM myappimage
to build an image (mytestingimage
) on top of myappimage
.
Then, mytestingimage
is exactly like myappimage
but with additional layers added by your ADD
and other commands.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yuankun |
Solution 2 | edrevo |
Solution 3 | Riki137 |
Solution 4 | fqxp |