Drupal docker file example with php-fpm alpine and composer

Note Statistics

Note Statistics

  • Viewed 1737 times
  • Bookmarked 1 times
  • 1 Grateful readers
Sat, 09/05/2020 - 16:43

Here is custom docker file based on the php-fpm alpine image.

Drupal project type

Use this Docker file for Drupal 8^ || ^9 site installed with Composer (composer create-project).

Audience

Use this if you need more control. Maybe

  • You need full control over your Drupal version
  • You are using composer for your Drupal dependencies
  • For each version of your application a new image is build and published to a repository like Dockerhub.

What you get

  • Mysql for php
  • gd: for image processing
  • ssmtp for sending mail (example here).
  • myslq client: needed by drush
  • zip package
  • git

Dockerfile

FROM php:7.4-fpm-alpine

# install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"

# install ssmtp
RUN apk add --no-cache ssmtp

# install php gd extension
RUN apk add --no-cache libpng libpng-dev  && docker-php-ext-install gd && apk del libpng-dev

# install mysql
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql

# install mysql client - required by drush
RUN apk add mysql-client

# install zip
RUN apk add --no-cache zip libzip-dev
RUN docker-php-ext-configure zip
RUN docker-php-ext-install zip

# install git
RUN apk add --no-cache bash git openssh

# copy files
COPY composer.json composer.lock /src/
COPY load.environment.php /src/
COPY /scripts  /src/scripts
COPY /web /src/web
# run composer
WORKDIR /src

# install packages
RUN composer install

CMD ["php-fpm"]

EXPOSE 9000

Next

The next step is a way to run Docker containers based on your image. Here are some alternatives

  • Use docker run command to run the docker image.
  • Use docker-compose. This is more elegant since configuration can be saved with code base to git or any other VSC.
  • Use kubernetes: An even more elegant solutio,n but requires more knowledge about how kubernetes works.
Authored by