Various PHP Project Dockerfile Examples

Bookstack Dockerfile

https://github.com/BookStackApp/BookStack/blob/master/dev/docker/Dockerfile

FROM php:7.4-apache

ENV APACHE_DOCUMENT_ROOT /app/public
WORKDIR /app

# Install additional dependacnies and configure apache
RUN apt-get update -y \
    && apt-get install -y git zip unzip libpng-dev libldap2-dev libzip-dev wait-for-it \
    && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu \
    && docker-php-ext-install pdo_mysql gd ldap zip \
    && a2enmod rewrite \
    && sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf \
    && sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php composer-setup.php \
    && mv composer.phar /usr/bin/composer \
    && php -r "unlink('composer-setup.php');"

# Use the default production configuration and update it as required
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
    && sed -i 's/memory_limit = 128M/memory_limit = 512M/g' "$PHP_INI_DIR/php.ini"

docker-apache-php Github Project

https://github.com/keopx/docker-apache-php/blob/master/8.0/Dockerfile

FROM debian:buster

MAINTAINER keopx <keopx@keopx.net>

# Step 1: Installation

# System environments
ENV DEBIAN_FRONTEND="noninteractive" \
    LOCALE="es_ES.UTF-8" \
    GOTPL_VER="0.1.5" \
    DEFAULT_ROOT="/var/www/html" \
    UID="1000" \
    GID="1000" \
    UNAME="keopx"

# Set repositories
RUN \
  echo "deb http://ftp.de.debian.org/debian/ buster main non-free contrib" > /etc/apt/sources.list && \
  echo "deb-src http://ftp.de.debian.org/debian/ buster main non-free contrib" >> /etc/apt/sources.list && \
  echo "deb http://security.debian.org/ buster/updates main contrib non-free" >> /etc/apt/sources.list && \
  echo "deb-src http://security.debian.org/ buster/updates main contrib non-free" >> /etc/apt/sources.list && \
  apt-get -qq update && apt-get -qqy upgrade && \
# Install some basic tools needed for deployment
  apt-get -yqq install \
  apt-utils \
  build-essential \
  debconf-utils \
  debconf \
  default-mysql-client \
  locales \
  curl \
  wget \
  unzip \
  patch \
  rsync \
  vim \
  nano \
  openssh-client \
  git \
  bash-completion \
  locales \
  libjpeg-turbo-progs libjpeg-progs \
  pngcrush optipng && \
# Install locale
  sed -i -e "s/# $LOCALE/$LOCALE/" /etc/locale.gen && \
  echo "LANG=$LOCALE">/etc/default/locale && \
  dpkg-reconfigure --frontend=noninteractive locales && \
  update-locale LANG=$LOCALE && \
# GOTPL
  gotpl_url="https://github.com/wodby/gotpl/releases/download/${GOTPL_VER}/gotpl-linux-amd64-${GOTPL_VER}.tar.gz"; \
  wget -qO- "${gotpl_url}" | tar xz -C /usr/local/bin; \
# Configure Sury sources
# @see https://www.noobunbox.net/serveur/auto-hebergement/installer-php-7-1-sous-debian-et-ubuntu
  apt-get -yqq install apt-transport-https lsb-release ca-certificates && \
  wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg && \
  echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list && \
# Install PHP7 with Xdebug (dev environment)
  apt-get -qq update && \
  apt-get -yqq install \
  php8.0         \
  php8.0-bcmath   \
  php8.0-bz2   \
  php8.0-curl         \
  php8.0-dev         \
  php8.0-gd         \
  php8.0-dom        \
  php8.0-imap     \
  php8.0-intl         \
  php8.0-ldap         \
  php8.0-mbstring    \
  php8.0-mysql        \
  php8.0-oauth        \
  php8.0-odbc        \
  php8.0-xml        \
  php8.0-yaml        \
  php8.0-zip        \
  php8.0-solr        \
  php8.0-apcu        \
  php8.0-opcache    \
  php8.0-redis        \
  php8.0-memcache     \
  php8.0-xdebug        \
  libapache2-mod-php8.0 && \
# Install manually xhprof
#  cd /tmp && \
#  wget https://github.com/Yaoguais/phpng-xhprof/archive/master.zip && \
#  unzip master.zip && \
#  cd phpng-xhprof-master && \
#  phpize8.0 && \
#  ./configure --with-php-config=/usr/bin/php-config8.0 && \
#  make && make install && \
#  mv /usr/lib/php/20190902/phpng_xhprof.so /usr/lib/php/20190902/xhprof.so  && \
#  echo "extension=xhprof.so" > /etc/php/8.0/mods-available/xhprof.ini && \
#  echo "xhprof.output_dir=/var/www/xhprof" >> /etc/php/8.0/mods-available/xhprof.ini && \

# Install manually APC
#  echo "extension=apcu.so" > /etc/php/7.2/mods-available/apcu_bc.ini && \
#  echo "extension=apc.so" >> /etc/php/7.2/mods-available/apcu_bc.ini && \
# Install SMTP.
  apt-get -yqq install libgnutls-openssl27 && \
  wget ftp.de.debian.org/debian/pool/main/s/ssmtp/ssmtp_2.64-8+b2_amd64.deb && \
  dpkg -i ssmtp_2.64-8+b2_amd64.deb && rm ssmtp_2.64-8+b2_amd64.deb && \
# Install Apache web server.
  apt-get -yqq install apache2 && \
#
# Step 2: Configuration
#
# Enable uploadprogress, imagick, redis and solr.
  phpenmod uploadprogress imagick redis solr && \
# Disable by default apcu, apcu_bc, opcache, xdebug and xhprof. Use docker-compose.yml to add file.
  phpdismod apcu apcu_bc opcache xdebug xhprof && \
# Remove all sites enabled
# RUN rm /etc/apache2/sites-enabled/*
# Configure needed apache modules and disable default site
# mpm_worker enabled.
  a2dismod mpm_event cgi && \
  a2enmod        \
  access_compat        \
  actions        \
  alias            \
  auth_basic        \
  authn_core        \
  authn_file        \
  authz_core        \
  authz_groupfile    \
  authz_host         \
  authz_user        \
  autoindex        \
  dir            \
  env             \
  expires         \
  filter         \
  headers        \
  mime             \
  negotiation         \
  php8.0         \
  mpm_prefork         \
  reqtimeout         \
  rewrite         \
  setenvif         \
  status         \
  ssl && \
# without the following line we get "AH00558: apache2: Could not reliably determine the server's fully qualified domain name"
# autorise .htaccess files
  sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf && \
# Install composer (latest version) | prestissimo to speed up composer
  php -r "copy('https://getcomposer.org/composer-1.phar', 'composer.phar');"  && \
  php composer.phar  && \
  mv composer.phar /usr/local/bin/composer && \
  chmod 755 /usr/local/bin/composer && \
  composer global require "hirak/prestissimo:^0.3" && \
### Install DRUSH (latest stable) ###
# Run this in your terminal to get the latest DRUSH project version:
  composer global require drush/drush && \
  ~/.composer/vendor/bin/drush init -y && \
  curl -OL https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar && \
  mv drush.phar /usr/local/bin/drush && \
  chmod +x /usr/local/bin/drush && \
### Install DRUPAL CONSOLE (latest version) ###
# Run this in your terminal to get the latest project version:
#   curl https://drupalconsole.com/installer -L -o drupal.phar && \
#   mv drupal.phar /usr/local/bin/drupal && \
#   chmod +x /usr/local/bin/drupal  && \
#   drupal self-update && \
# Bash setup.
  echo ". /usr/share/bash-completion/bash_completion" >> ~/.bashrc && echo "alias ll='ls -lahs'" >> ~/.bashrc && \
#
# Step 3: Clean the system
#
# Cleanup some things.
  apt-get -q autoclean && \
  rm -rf /var/lib/apt/lists/* && \
#
# Step 4: Run
#
# Create 'keopx' user like local machime user.
  groupadd -g $UID $GID ; \
  useradd -m -u $UID -g $GID -s /bin/bash $UNAME ; \
  usermod -aG www-data $UNAME; \
  echo ". /usr/share/bash-completion/bash_completion" >> ~/.bashrc && echo "alias ll='ls -lahs'" >> /home/$UNAME/.bashrc
# Working dir
WORKDIR ${DEFAULT_ROOT}
# Configure templates
COPY templates /etc/gotpl/
COPY scripts/apache2-foreground /usr/bin/
EXPOSE 80 443
CMD ["apache2-foreground"]

Laravel Octane

https://github.com/exaco/laravel-octane-dockerfile/blob/main/Dockerfile

###########################################
# PHP dependencies
###########################################

FROM composer:latest AS vendor
WORKDIR /var/www/html
COPY composer* ./
RUN composer install \
  --no-dev \
  --no-interaction \
  --prefer-dist \
  --ignore-platform-reqs \
  --optimize-autoloader \
  --apcu-autoloader \
  --ansi \
  --no-scripts

###########################################

FROM php:8.0-cli-buster

ARG WWWUSER=1000
ARG WWWGROUP=1000

ARG deployment_env="Production"
ENV deployment_env=${deployment_env}

ARG TZ=Asia/Tehran
ENV DEBIAN_FRONTEND=noninteractive

ENV TERM=xterm-color

WORKDIR /var/www/html

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN set -eux; \
    apt-get update; \
    apt-get upgrade -yqq; \
    pecl channel-update pecl.php.net \
    && apt-get install -yqq --no-install-recommends \
            apt-utils \
            gnupg \
            gosu \
            git \
            curl \
            ca-certificates \
            supervisor \
            libmemcached-dev \
            libz-dev \
            libbrotli-dev \
            libc-ares-dev \
            libpq-dev \
            libjpeg-dev \
            libpng-dev \
            libfreetype6-dev \
            libssl-dev \
            libwebp-dev \
            libmcrypt-dev \
            libonig-dev \
            libzip-dev zip unzip \
            libargon2-1 \
            libidn2-0 \
            libpcre2-8-0 \
            libpcre3 \
            libxml2 \
            libzstd1 \
            procps


RUN set -xe; \
    docker-php-ext-configure zip \
            && docker-php-ext-install zip \
            && php -m | grep -q 'zip'; \
    docker-php-ext-install \
            mbstring \
            pdo_mysql; \
    docker-php-ext-configure gd \
            --prefix=/usr \
            --with-jpeg \
            --with-webp \
            --with-freetype \
    && docker-php-ext-install gd; \
    php -r 'var_dump(gd_info());'

###########################################
# OPcache
###########################################

ARG INSTALL_OPCACHE=true

RUN if [ ${INSTALL_OPCACHE} = true ]; then \
    docker-php-ext-install opcache; \
  fi

###########################################
# PHP Redis
###########################################

ARG INSTALL_PHPREDIS=true

RUN if [ ${INSTALL_PHPREDIS} = true ]; then \
      pecl install -o -f redis \
      && rm -rf /tmp/pear \
      && docker-php-ext-enable redis; \
  fi

###########################################
# PCNTL
###########################################

ARG INSTALL_PCNTL=true

RUN if [ ${INSTALL_PCNTL} = true ]; then \
    docker-php-ext-install pcntl; \
  fi

###########################################
# BCMath
###########################################

ARG INSTALL_BCMATH=true

RUN if [ ${INSTALL_BCMATH} = true ]; then \
    docker-php-ext-install bcmath; \
  fi

###########################################
# RDKAFKA
###########################################

ARG INSTALL_RDKAFKA=true

RUN if [ ${INSTALL_RDKAFKA} = true ]; then \
      apt-get install -y librdkafka-dev \
      && pecl install rdkafka \
      && docker-php-ext-enable rdkafka; \
  fi

###########################################
# Swoole extension
###########################################

ARG INSTALL_SWOOLE=true

RUN set -eux; \
    if [ ${INSTALL_SWOOLE} = true ]; then \
      pecl install -D 'enable-openssl="yes" enable-http2="yes" enable-swoole-curl="yes" enable-mysqlnd="yes" enable-cares="yes"' swoole; \
      docker-php-ext-enable swoole; \
      php -m | grep -q 'swoole'; \
    fi

###########################################################################
# Human Language and Character Encoding Support
###########################################################################

ARG INSTALL_INTL=true

RUN if [ ${INSTALL_INTL} = true ]; then \
    apt-get install -yqq zlib1g-dev libicu-dev g++ \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl; \
  fi

###########################################
# MySQL Client
###########################################

USER root

ARG INSTALL_MYSQL_CLIENT=true

RUN if [ ${INSTALL_MYSQL_CLIENT} = true ]; then \
    apt-get -y install default-mysql-client; \
  fi

###########################################

RUN groupadd --force -g $WWWGROUP octane
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u $WWWUSER octane

RUN apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && rm /var/log/lastlog /var/log/faillog

COPY . .

RUN mkdir -p \
  ./storage/framework/sessions \
  ./storage/framework/views \
  ./storage/framework/cache \
  ./storage/logs \
  ./bootstrap/cache \
  && chown -R octane:octane \
  ./storage \
  ./bootstrap/cache \
  && chmod -R ug+rwx ./storage ./bootstrap/cache

COPY --from=vendor /var/www/html/vendor ./vendor

COPY ./deployment/octane/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY ./deployment/octane/php.ini /usr/local/etc/php/conf.d/octane.ini
COPY ./deployment/octane/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

RUN chmod +x ./deployment/octane/entrypoint.sh
RUN cat ./deployment/octane/utilities.sh >> ~/.bashrc

EXPOSE 9000

ENTRYPOINT ["./deployment/octane/entrypoint.sh"]

Laravel Sail

Dockerfile

FROM ubuntu:21.04

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP
ARG NODE_VERSION=16

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
    && mkdir -p ~/.gnupg \
    && chmod 600 ~/.gnupg \
    && echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C \
    && echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu hirsute main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php7.4-cli php7.4-dev \
       php7.4-pgsql php7.4-sqlite3 php7.4-gd \
       php7.4-curl php7.4-memcached \
       php7.4-imap php7.4-mysql php7.4-mbstring \
       php7.4-xml php7.4-zip php7.4-bcmath php7.4-soap \
       php7.4-intl php7.4-readline php7.4-pcov \
       php7.4-msgpack php7.4-igbinary php7.4-ldap \
       php7.4-redis php7.4-xdebug \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && apt-get update \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y postgresql-client \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php7.4

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/7.4/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]

Entrypoint Script

https://github.com/laravel/sail/blob/1.x/runtimes/7.4/start-container

Uses an interesting piece of software called gosu to help with permissions: https://github.com/tianon/gosu

#!/usr/bin/env bash

if [ ! -z "$WWWUSER" ]; then
    usermod -u $WWWUSER sail
fi

if [ ! -d /.composer ]; then
    mkdir /.composer
fi

chmod -R ugo+rw /.composer

if [ $# -gt 0 ]; then
    exec gosu $WWWUSER "$@"
else
    /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
fi

Tags

 Docker  Dockerfile  PHP  web development