Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 599
Baily Avatar asked Mar 27 '18 13:03

Baily


People also ask

Can Dockerfile copy from parent directory?

It turns out that you cannot include files outside Docker's build context. However, you can copy files from the Dockerfile's parent directory.

Can we use two from in Dockerfile?

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.


3 Answers

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

like image 91
edrevo Avatar answered Oct 12 '22 15:10

edrevo


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
like image 21
Yuankun Avatar answered Oct 12 '22 15:10

Yuankun


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.

like image 3
Riki137 Avatar answered Oct 12 '22 15:10

Riki137