Adding PHP Extensions using Docker Compose

15,425

Solution 1

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

Solution 2

this is my code. its already running on my server.

web:
  image: nginx:latest
  ports:
    - "80:80"
  volumes:
  - ./code:/code
  - ./site.conf:/etc/nginx/conf.d/site.conf
  links:
    - php
php:
  #remove this
  #dockerfile: extensions
  #image: php:7-fpm
  #change with build ...
  build: './docker/php'
  volumes:
    - ./code:/code

Then, add Dockerfile file to the docker/php folder:

FROM php:7-fpm

RUN apt-get update && apt-get install -y \
        libicu-dev \
    && docker-php-ext-install \
        intl \
    && docker-php-ext-enable \
        intl

Now you can run Dockerfile inside docker-compose.

Share:
15,425
Jacobm001
Author by

Jacobm001

I'm an Analyst Programmer working for Oregon State University. I have a rather eclectic set of interests and strive to be constantly learning new things.

Updated on June 03, 2022

Comments

  • Jacobm001
    Jacobm001 almost 2 years

    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.