Run `docker-php-ext-install` FROM container other than php
Asked Answered
S

2

23

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
Sirloin answered 19/2, 2017 at 20:53 Comment(2)
Look at #37528303Cara
I've looked at it before, but my problem isn't with extension dependencies, but with the thing, that docker-php-ext-install exists in php containerSirloin
S
32

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
Sirloin answered 20/2, 2017 at 18:44 Comment(2)
Actually, doing this on docker-compose smells like an anti-pattern.Zaxis
Each RUN command adds a layer to the Docker image stack, so whenever possible, try and combine run commands. Pretty sure the docker-php-ext-install script builds and cleans up each time it runs. So if you can pass pdo_mysql and xml in a single call, that is a lot faster I believe.Loxodromic
Z
18

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 start.sh 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 start.sh /tmp/start.sh
CMD ["/tmp/start.sh"]
Zaxis answered 19/2, 2017 at 23:16 Comment(6)
I'm marking your answer as a solution to this particular problem, but you cannot (or I don't know how) get specific PHP version using apt-get and because of that I used something other. But thanks!Sirloin
I am a Fedora user myself, but as far as I know you can do apt-get install <package name>=<version> or at least apt-get install -y php5.6. If you google it, the solution will surely present itself.Zaxis
How can I add helper scripts docker-php-ext-configure, docker-php-ext-install in my docker file ?Turkish
@Turkish copy-paste. From original project to yours.Zaxis
I coppied scripts from github.com/flik/php/tree/master/7.0/stretch/cli and pasted in my docker file but getting error regarding some dependencies for execution.Turkish
@Turkish please consider opening a relevant question. This question is almost 2 years old and it makes no sense to provide help in comments. Opening a new question you at least would be able to provide more details on the matter of your problem, format your code and explain the details, which is impossible in the comments.Zaxis

© 2022 - 2024 — McMap. All rights reserved.