Adding PHP Extensions in Docker Compose

1

What would be the best way to add PHP extensions like the GD library among others directly in docker-compose.yml?

My file is as below:

nginx:
    image: tutum/nginx
    ports:
        - "80:80"
    links:
        - phpfpm
    volumes:
        - ./nginx/default:/etc/nginx/sites-available/default
        - ./nginx/default:/etc/nginx/sites-enabled/default

        - ./logs/nginx-error.log:/var/log/nginx/error.log
        - ./logs/nginx-access.log:/var/log/nginx/access.log
phpfpm:
    image: php:fpm
    ports:
        - "9000:9000"
    volumes:
        - ./public:/usr/share/nginx/html
        - ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
mysql:
  image: mariadb
  environment:
    MYSQL_DATABASE: lista
    MYSQL_USER: root
    MYSQL_PASSWORD: 123
    MYSQL_ROOT_PASSWORD: 123
  command: mysqld --innodb-buffer-pool-size=1024M
  ports:
    - "3306:3306" 
    
asked by anonymous 05.08.2018 / 20:36

1 answer

1

You can use the command command to install a new extension:

# docker-compose.yml
phpfpm:
    command: sh -c "apk update && apk upgrade && apk add postgresql-dev && docker-php-ext-install pdo_pgsql && php-fpm"
    container_name: phpfpm
    image: php:7.2-fpm-alpine

# $ docker exec -it phpfpm php -m | grep pdo

However, I'm sure this is not what you want since command runs every time your services go up with docker-compose up .

As is said in the official PHP repository in the Docker Hub, it is recommended to build a new image with the its extensions via Dockerfile .

# phpfpm.Dockerfile
FROM php:7.2-fpm-alpine

RUN apk update && apk upgrade && apk add postgresql-dev
RUN docker-php-ext-install pdo_pgsql

And let Docker Compose create and manage your containers:

# docker-compose.yml
phpfpm:
    build: ./phpfpm.Dockerfile
    container_name: phpfpm

# $ docker exec -it phpfpm php -m | grep pdo
    
06.08.2018 / 01:50