Creating a base Dockerfile for php application

A Dockerfile is a text file that contains all the commands a user could call on the command line to assemble an image. I will be creating a php dockerfile example

Using docker build users can create an automated build that executes several command-line instructions in succession.

This page describes the use of a Dockerfile to create a base image for your PHP application. The first thing you’ll want to do is create a new file called Dockerfile.base in the root directory of your project.

Then using a text editor like Sublime Text, you can add the following contents to your Dockerfile.base:

FROM php:8.1-fpm-alpine as main

WORKDIR /var/www/html/

# Essentials
RUN echo "UTC" > /etc/timezone
#RUN apk add --no-cache nginx supervisor
RUN apk add  zip curl sqlite nginx supervisor vim libxml2-dev git libpng-dev libzip-dev

# Installing bash
RUN apk add bash && \
 sed -i 's/bin\/ash/bin\/bash/g' /etc/passwd

# Installing PHP & PHP dependencies
RUN apk add  php8 \
    php8-common \
    php8-fpm \
    php8-pdo \
    php8-opcache \
    php8-zip \
    php8-phar \
    php8-iconv \
    php8-cli \
    php8-curl \
    php8-openssl \
    php8-mbstring \
    php8-tokenizer \
    php8-fileinfo \
    php8-json \
    php8-xml \
    php8-xmlwriter \
    php8-simplexml \
    php8-dom \
    php8-pdo_mysql \
    php8-pdo_sqlite \
    php8-tokenizer \
    php8-pecl-redis \
    build-base \
    zlib-dev \
    php8-dev \
    postgresql-dev \
    php8-pecl-mongodb

RUN docker-php-ext-install zip exif  mysqli pdo pdo_mysql opcache pdo_pgsql pgsql
RUN pecl install apcu
RUN docker-php-ext-enable apcu

RUN ln -s /usr/bin/php8 /usr/bin/php

# Installing composer
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php && \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
    rm -rf composer-setup.php

# Configure supervisor
RUN mkdir -p /etc/supervisor.d/
COPY docker-conf/supervisord.ini /etc/supervisor.d/supervisord.ini

# Configure PHP
RUN mkdir -p /run/php/
RUN touch /run/php/php8.1-fpm.pid

COPY docker-conf/php-fpm.conf /etc/php8/php-fpm.conf
COPY docker-conf/php.ini-production /etc/php8/php.ini

# Configure nginx
COPY docker-conf/nginx.conf /etc/nginx/
COPY docker-conf/nginx-laravel.conf /etc/nginx/modules/
COPY docker-conf/default-nginx.conf /etc/nginx/http.d/default.conf

RUN mkdir -p /run/nginx/ && \
    touch /run/nginx/nginx.pid

RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

Let’s break down what each line in the Dockerfile does:

FROM php:8.1-fpm-alpine: This line specifies that we want to use the PHP 8.1 Apache image as our base image.

RUN apk add  php8  php8-common php8-fpm php8-pdo php8-opcache php8-zip php8-phar \
    php8-iconv php8-cli php8-curl php8-openssl php8-mbstring php8-tokenizer \
    php8-fileinfo php8-json php8-xml php8-xmlwriter php8-simplexml php8-dom \
    php8-pdo_mysql php8-pdo_sqlite php8-tokenizer php8-pecl-redis build-base \
    zlib-dev php8-dev postgresql-dev php8-pecl-mongodb

This installs all the php-dependencies necessary to run your php docker image

Now that we’ve created our Dockerfile, we can build our image. To do this, we’ll run the following command:

docker build -t my-php-base -f Dockerfile.base . 

This will create an image called “my-php-base” that we can use to run our PHP application.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top