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

Albert221 picture Albert221 · Feb 19, 2017 · Viewed 31.4k times · Source

I have a problem with Docker (docker-compose). I want to install some PHP extensions using docker-compose.yml, but I am unable to do this, because my .yml has FROM ubuntu and not FROM php. Is there any way I can achieve or access the docker-php-ext-install?

Dockerfile

FROM ubuntu:16.04

RUN apt -yqq update
RUN apt -yqq install nginx iputils-ping
RUN docker-php-ext-install pdo pdo_mysql mbstring

WORKDIR /usr/local/src

COPY docker/nginx/dev.conf /etc/nginx/conf.d/dev.conf
COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf

CMD ["nginx"]

docker-compose.yml

version: "2"
services:
  mariadb:
    image: mariadb
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=1
      - MYSQL_ROOT_PASSWORD=
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8080:80"
    restart: always
    environment:
      - PMA_HOST=mariadb
    links:
      - mariadb
  php:
    image: php:7.1.1-fpm
    ports:
      - "9000:9000"
    volumes:
      - .:/dogopic
    links:
      - mariadb
  nginx:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/dogopic
    links:
      - php

Console output (fragment)

Step 5/9 : RUN docker-php-ext-install pdo pdo_mysql mbstring
 ---> Running in 445f8c82883d
/bin/sh: 1: docker-php-ext-install: not found

Answer

Albert221 picture Albert221 · Feb 20, 2017

New solution

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

php/Dockerfile

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:

services:
  // other services
  php:
    build: ./php
    ports:
      - "9000:9000"
    volumes:
      - .:/dogopic
    links:
      - 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

@ECHO OFF

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