Run `docker-php-ext-install` FROM container other than php

New solution

You need to create new Dockerfile for specific service, in this case php:


FROM php:7.1.1-fpm
RUN apt -yqq update
RUN apt -yqq install libxml2-dev
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install xml

And then link to it in your docker-compose.yml file, just like this:

  // other services
    build: ./php
      - "9000:9000"
      - .:/dogopic
      - mariadb

Please look at build parameter - it points to directory in which is that new Dockerfile located.

Old solution

I walked around the problem. I've figured out that I can still run this docker-php-ext-install script using following command:

docker-compose exec <your-php-container> docker-php-ext-install pdo pdo_mysql mbstring

And because of the convenience I've created this simple Batch file to simplify composing containers just to one command: ./docker.bat


docker-compose build
docker-compose exec php docker-php-ext-install pdo pdo_mysql mbstring
docker-compose up

docker-php-ext-install is not some native docker functionality. If you read php docker hub page carefully, you will see, that it's just a script provided to make the installation process easy:

We provide the helper scripts docker-php-ext-configure, docker-php-ext-install, and docker-php-ext-enable to more easily install PHP extensions.

If your image is based on ubuntu, not php, you might find docker-php-ext-install, for example, on github.

But since your Dockerfile is FROM ubuntu, I advise you to install php with apt-get:

FROM ubuntu:16.04

RUN apt -yqq update
RUN apt -yqq install nginx iputils-ping
RUN apt-get install -y php php-fpm pdo-mysql php-mbstring

Do not forget to set up nginx to use php-fpm. To do so, I personally use a script, which starts php-fpm and nginx in the container:

php-fpm -D
nginx -g "daemon off;"

And in the Dockerfile I run the script. not nginx:

COPY /tmp/
CMD ["/tmp/"]