How to Deploy PHP Development Environment With Docker

How to Deploy PHP Development Environment With Docker

This article mainly introduces how to use Docker to build a PHP development environment. The author also explores whether to build a Docker-based development environment using a single container or multiple containers.

This article mainly introduces how to use Docker to build a PHP development environment. The author also explores whether to build a Docker-based development environment using a single container or multiple containers. Recommended for PHP developers. I hope to be helpful.

Environment deployment has always been a big problem, whether it is a development environment or a production environment, but Docker packages the development environment and production environment in a lightweight way to provide a consistent environment. Greatly improved the consistency of development and deployment. Of course, the actual situation is not so simple, because the configuration of the production environment and the development environment are completely different, such as logging and other issues need to be configured separately, but at least it is simpler and more convenient than here. PHP development is used as an example to explain how Docker Layout the development environment.

In general, a PHP project will require the following tools:

  1. Web server: Nginx / Tengine
  2. Web Application: PHP-FPM
  3. Database: MySQL / PostgreSQL
  4. Cache service: Redis / Memcache

This is the simplest architectural method. In the early days of Docker development, Docker was abused a lot. For example, multiple services are started in one image. The log collection is still based on Syslog or other old methods. The image capacity is very large and the basic image can be reached. 80M, which is completely different from the idea originally proposed by Docker, and the Alpine Linux distribution as a lightweight Linux environment is very suitable as a Docker base image. Docker officially recommends using Alpine instead of Debian as the base image. Existing official images will also be migrated to Alpine. All images in this article will be based on Alpine.

Nginx / Other

In this part, the author has explained the Docker practice of Tengine in the Nginx practice of Docker container, and given the Dockerfile. Since Tengine is preferred, and the official Alpine image of Nginx has been given, so Tengine is used here. I have uploaded the image to the official DockerHub.

docker pull chasontang/tengine:2.1.2_f

Get the image, see Dockerfile for details.


Docker official has provided the 7.0.7-fpm-alpine image of PHP. The Dockerfile is as follows:

FROM alpine:3.4


# persistent / runtime deps


    autoconf \

    file \

    g++ \

    gcc \

    libc-dev \

    make \

    pkgconf \


RUN apk add --no-cache --virtual .persistent-deps \

    ca-certificates \



# ensure www-data user exists

RUN set -x \

  && addgroup -g 82 -S www-data \

  && adduser -u 82 -D -S -G www-data www-data

# 82 is the standard uid/gid for "www-data" in Alpine





ENV PHP_INI_DIR /usr/local/etc/php

RUN mkdir -p $PHP_INI_DIR/conf.d



ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data




ENV PHP_FILENAME php-7.0.7.tar.xz

ENV PHP_SHA256 9cc64a7459242c79c10e79d74feaf5bae3541f604966ceb600c3d2e8f5fe4794

RUN set -xe \

  && apk add --no-cache --virtual .build-deps \


    curl-dev \

    gnupg \

    libedit-dev \

    libxml2-dev \

    openssl-dev \

    sqlite-dev \

  && curl -fSL "$PHP_FILENAME/from/this/mirror" -o "$PHP_FILENAME" \

  && echo "$PHP_SHA256 *$PHP_FILENAME" | sha256sum -c - \

  && curl -fSL "$PHP_FILENAME.asc/from/this/mirror" -o "$PHP_FILENAME.asc" \

  && export GNUPGHOME="$(mktemp -d)" \

  && for key in $GPG_KEYS; do \

    gpg --keyserver --recv-keys "$key"; \

  done \

  && gpg --batch --verify "$PHP_FILENAME.asc" "$PHP_FILENAME" \

  && rm -r "$GNUPGHOME" "$PHP_FILENAME.asc" \

  && mkdir -p /usr/src \

  && tar -Jxf "$PHP_FILENAME" -C /usr/src \

  && mv "/usr/src/php-$PHP_VERSION" /usr/src/php \

  && rm "$PHP_FILENAME" \

  && cd /usr/src/php \

  && ./configure \

    --with-config-file-path="$PHP_INI_DIR" \

    --with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \


    --disable-cgi \

# --enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself)

    --enable-mysqlnd \

# --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see

    --enable-mbstring \

    --with-curl \

    --with-libedit \

    --with-openssl \

    --with-zlib \

  && make -j"$(getconf _NPROCESSORS_ONLN)" \

  && make install \

  && { find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \

  && make clean \

  && runDeps="$( \

    scanelf --needed --nobanner --recursive /usr/local \

      | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \

      | sort -u \

      | xargs -r apk info --installed \

      | sort -u \

  )" \

  && apk add --no-cache --virtual .php-rundeps $runDeps \

  && apk del .build-deps

COPY docker-php-ext-* /usr/local/bin/


WORKDIR /var/www/html

RUN set -ex \

  && cd /usr/local/etc \

  && if [ -d php-fpm.d ]; then \

    # for some reason, upstream's php-fpm.conf.default has "include=NONE/etc/php-fpm.d/*.conf"

    sed 's!=NONE/!=!g' php-fpm.conf.default | tee php-fpm.conf > /dev/null; \

    cp php-fpm.d/www.conf.default php-fpm.d/www.conf; \

  else \

    # PHP 5.x don't use "include=" by default, so we'll create our own simple config that mimics PHP 7+ for consistency

    mkdir php-fpm.d; \

    cp php-fpm.conf.default php-fpm.d/www.conf; \

    { \

      echo '[global]'; \

      echo 'include=etc/php-fpm.d/*.conf'; \

    } | tee php-fpm.conf; \

  fi \

  && { \

    echo '[global]'; \

    echo 'error_log = /proc/self/fd/2'; \

    echo; \

    echo '[www]'; \

    echo '; if we send this to /proc/self/fd/1, it never appears'; \

    echo 'access.log = /proc/self/fd/2'; \

    echo; \

    echo 'clear_env = no'; \

    echo; \

    echo '; Ensure worker stdout and stderr are sent to the main error log.'; \

    echo 'catch_workers_output = yes'; \

  } | tee php-fpm.d/docker.conf \

  && { \

    echo '[global]'; \

    echo 'daemonize = no'; \

    echo; \

    echo '[www]'; \

    echo 'listen = [::]:9000'; \

  } | tee php-fpm.d/zz-docker.conf


CMD ["php-fpm"]


First, the image inherits from the alpine: 3.4 image. Use the apk command to install PHP's minimum dependencies. At the same time, add www-data as the running user of php-fpm. Specify the php configuration file to / usr / local / etc / php, then download php-src, compile and install, you can refer to the php compile and install article I wrote before. The parameters are quite satisfactory. The installation directory is specified to /usr/local, then scanelf is used to obtain a list of dependent runtime libraries, and other installation packages are deleted. Copy docker-php-ext-configure, docker-php-ext-enable, docker-php-ext-install to the container, these three files are used for subsequent installation of extensions. Then copy php-fpm.conf to the configuration directory, specify error_log and access_log to the terminal standard output, daemonize = no means not to run as a service process. The EXPOSE 9000 port is used to communicate with other containers, and then CMD ["php-fpm"] runs php-fpm. And the working directory is specified to /var/www/html.


Now that we have the base image, we can use the base image to configure the container, but starting the container through the manual docker command will be very troublesome. Fortunately, the official docker-compose command has been provided to orchestrate the container. You only need to write a docker-compose.yaml file. For details, please refer to the official documentation.

version: '2'



  image: php:7.0.7-fpm-alpine


   - "./src:/var/www/html"

  restart: always




   - php-fpm


   - php-fpm

  image: chasontang/tengine:2.1.2_f


   - "./nginx.vh.default.conf:/etc/nginx/conf.d/default.conf"


   - "80:80"

  restart: always

Very easy to understand, two services are defined here. Php-fpm depends on the php: 7.0.7-fpm-alpine image, and the src folder is mapped to the /var/www/html folder. The tengine service depends on the php-fpm service. And link the php-fpm service so that it can communicate with the php-fpm container over the network. The tengine service is based on the chasontang/tengine:2.1.2_f image and maps the nginx.vh.default.conf file to /etc/nginx/conf.d/default.conf file. Then look at nginx.vh.default.conf

server {

  listen    80;

  server_name localhost;


  #charset koi8-r;


  #access_log logs/host.access.log main;


  location / {

    root  html;

    index index.html index.htm;



  #error_page 404       /404.html;


  # redirect server error pages to the static page /50x.html


  error_page  500 502 503 504 /50x.html;

  location = /50x.html {

    root  html;



  # proxy the PHP scripts to Apache listening on


  #location ~ \.php$ {

  #  proxy_pass;



  location ~ [^/]\.php(/|$) {

    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    fastcgi_pass php-fpm:9000;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;

    fastcgi_param PATH_INFO $fastcgi_path_info;

    include fastcgi_params;



  # deny access to .htaccess files, if Apache's document root

  # concurs with nginx's one


  #location ~ /\.ht {

  #  deny all;



All the files, because /etc/nginx/nginx.conf uses include /etc/nginx/conf.d/*.conf; This directory is included, that is, you don't need to manage other configurations of nginx, just use You can replace the default virtual host configuration with your own nginx virtual host configuration, or add a virtual host configuration.

As you can see from the above, the default.conf file defines a location that matches a URL containing .php, then splits it out of the PATH_INFO parameter, and passes these variables to the php-fpm:9000 php-fpm service.

It should be noted here that because Nginx and PHP-FPM are not on the same host, Nginx only performs static file processing and routing and forwarding. The actual PHP file execution occurs in the PHP-FPM container. So the SCRIPT_FILENAME variable must use the directory in the PHP-FPM container, so here it is specified using hard coding. Of course, it is also possible to let two containers share the same data volume, but the author believes that this is only for the convenience of container orchestration, and the other is completely unhelpful.

It's easy! Now we can quickly start and update the environment.

The above is the details of how to deploy PHP development environment using Docker

WordPress in Docker. Part 1: Dockerization

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

PHP Interview Questions with Solutions: Prepare for PHP Interview

PHP Interview Questions with Solutions: Prepare for PHP Interview

Prepare for PHP Interviews. Set Variable With php.ini File. Logic & Output Behind PHP Code Segment. Class Concept, Error & Functions in PHP. Start PHP Now!

In this course you will be introduced with some tricky questions that everyone face during their interview. In each solutions I have included some useful functions which we generally use at the time of development also. I have covered the following area in my course with Questions, Attractive Presentations and Practical Solutions that will help you to understand the logic behind PHP in a different way.

Some useful functions
PHP error types
Class concept
Access specifiers public, private and protected
Set variable with php.ini file
Operators introduced in PHP7
Logic and its output behind some code segment
Basic knowledge
PHP7, XAMPP Server, Notepad++
What will you learn
This course is build for the person who is facing interviews. Every question is well explained with the practical solution through videos. So that everybody can prepare themselves for the tricky questions asked during interviews
To continue:

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by