Add PHP Extensions to Docker Compose

Asked

Viewed 359 times

1

what would be the best way to add PHP extensions such as the GD library and more directly into 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" 
  • If it’s any help, you can use an official image that already comes with the most common extensions from php,take a look therein. Or you can use these Dockerfiles as a basis to build your.

  • You can tell me how I would inform this image in my Docker-Compose?

  • You build your own php image (based on the ones I mentioned) and then only reference in image, instead of calling the one you’re using. Ex: Dockerfile for php 7.0-fpm. Know how to build images with the Dockerfile?

  • I don’t know, I’m starting to work with Docker now, I’m gonna do some research on.

  • Take a look at this article, and recommend the play-with-Docker to train in practice ;)

1 answer

2

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

But I’m sure that’s not what you want since command is performed every time your services go up with docker-compose up.

As it is said in the proper official repository PHP on Docker Hub, recommended is to build a new image with 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.