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