Adding PHP Extensions using Docker Compose

Jacobm001 picture Jacobm001 · Apr 16, 2018 · Viewed 11.9k times · Source

I'm trying to setup a local development environment in docker that includes nginx and php. I started with this tutorial and have a functioning server. My project requires that a couple PHP extensions be installed, but I'm having difficulty figuring out how to adapt this setup to include them.

The documentation for the image says to put it in a dockerfile, which I have done. However, that gives me an error of:

ERROR: The Compose file is invalid because: Service php has both an image and alternate Dockerfile. A service can either be built to image or use an existing image, not both.

My docker-compose.yml:

web:
  image: nginx:latest
  ports:
    - "80:80"
  volumes:
  - ./code:/code
  - ./site.conf:/etc/nginx/conf.d/site.conf
  links:
    - php
php:
  dockerfile: extensions
  image: php:7-fpm
  volumes:
    - ./code:/code

My extensions file

RUN docker-php-ext-install zip
RUN docker-php-ext-install gd
RUN docker-php-ext-enable zip
RUN docker-php-ext-enable gd

Clearly I'm going about this wrong. Is there a way to install the extensions into this image, or do I need to create my own? I'm using Docker for Windows.

Answer

JemoeE picture JemoeE · Apr 17, 2018

In your extensions file, add this to the top: FROM php:7-fpm

and remove the image: php:7-fpm from your docker-compose file