Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Dockerfile extend another one?

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

like image 819
Sylvain Avatar asked Apr 01 '16 17:04

Sylvain


People also ask

Can you extend a Dockerfile?

Dockerfiles don't extend Dockerfiles but images, the FROM line is not an "include" statement. So, if you want to "extend" another Dockerfile, you need to build the original Dockerfile as an image, and extend that image.

Can a Dockerfile have more than one from?

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.

Can Dockerfile run 2 commands?

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect. If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

Can Dockerfile have multiple Workdir?

you can have multiple WORKDIR in same Dockerfile. If a relative path is provided, it will be relative to the previous WORKDIR instruction. If no WORKDIR is specified in the Dockerfile then the default path is / . The WORKDIR instruction can resolve environment variables previously set in Dockerfile using ENV.

How do I extend a dockerfile?

Dockerfiles don't extend Dockerfilesbut images, the FROMline is not an "include" statement. So, if you want to "extend" another Dockerfile, you need to build the original Dockerfile as an image, and extend that image. For example; Dockerfile1: FROM alpine RUN echo "foo" > /bar Dockerfile2: FROM myimage RUN echo "bar" > /baz

What is the use of a custom dockerfile?

Dockerfile is a file that contains all the instructions needed for assembling the Docker image. Docker can automatically build images using it, without the need for any additional commands or parameters.

How to use multiple from and as commands in dockerfile?

We can use multiple FROM commands combined with AS commands in our Dockerfile where the last FROM command will actually build the image. All the FROM commands before that, will lead to the creation of intermediate images which are cached regularly.

How do I build a docker image without a dockerfile?

docker build [OPTIONS] -f- PATH This syntax can be useful in situations where you want to build an image from a repository that does not contain a Dockerfile, or if you want to build with a custom Dockerfile, without maintaining your own fork of the repository.


1 Answers

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    # ... 
like image 76
Cethy Avatar answered Oct 31 '22 09:10

Cethy