Skip to main content

Dockerfile

Another way of using Docker is to use Dockerfile, which is especially useful when working on continuous integration and deployment (CI/CD). For instance, one such Dockerfile can look like this

# Stage 0, build app
FROM php:7.2-fpm as build-container
RUN curl -sS https://getcomposer.org/installer | php \
&& chmod +x composer.phar && mv composer.phar /usr/local/bin/composer
RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
apt-get install -yq nodejs build-essential \
git unzip \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
&& curl -sL https://deb.nodesource.com/setup_8.x | bash - \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install -j$(nproc) mysqli
RUN npm install -g npm
WORKDIR /build
COPY . /build
RUN cp /build/wp-config.php.template /build/wp-config.php
RUN bash /build/scripts/build-plugins.sh
# Stage 1, build app container
FROM php:7.2-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
unzip \
mysql-client \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-install -j$(nproc) mysqli \
&& docker-php-ext-install gd mysqli opcache zip
ADD https://downloads.wordpress.org/release/wordpress-4.9.8-no-content.zip /var/www/latest.zip
RUN cd /var/www && unzip latest.zip && rm latest.zip
RUN rm -rf /var/www/html
RUN mkdir -p /var/www/html/ \
&& mv /var/www/wordpress/* /var/www/html/
# Copy wp files
COPY --from=build-container /build/ /var/www/html/
RUN chown www-data:www-data /var/www/html/ -R
COPY config/php.ini /usr/local/etc/php/
WORKDIR /var/www/html/
CMD ["exec","php-fpm"]

This Dockerfile will first build a local container with PHP, composer, and node. At that stage, your theme/plugins can be built. It also contains the RUN bash bin/build-plugins.sh command. You can replace this with your installation script (be it for plugins or theme). You'll place composer and npm builds in that shell script.

After that, you'll build the WordPress container and copy the plugins built in the previous stage. You can modify this to your liking. Beside this, you'll probably need a Docker image for nginx. But we won't be going into too much detail about it.

If you named this file Dockerfile, you'll run

docker build -t yourtag .

In case you named it something like Dockerfile.php (if you need multiple stages and builds), you'll run

docker build -t yourtag -f Dockerfile.php .

The Dockerfiles should be located in the root of your project.

To go in the interactive shell of the built image, type

docker run -it CONTAINER_NAME bash

If that fails, you can try

docker run -d CONTAINER_NAME
docker exec -it CONTAINER_NAME bash

Running the docker run with -d means that you are detaching it, and you can check the logs created while building it.

Docker notes

Depending on your project, the Docker compose or Dockerfile may differ. For more information, consult your friendly DevOps.

Structuring the Docker setup for PHP projects