PHP – Docker Development Environment (DDE Part 5)


In this post, we are going to setup our PHP container. In order to make this post a little shorter, I will skip explaining concepts covered in previous posts.

Let’s start by adding the following service in our docker-compose.yml file below the mysqldb service we created in the last post.

    context: ./docker/php-fpm/${PHP_VERSION}
    restart: always
      - "${PHP_FPM_PORT}:${PHP_FPM_PORT}"
      - "${SSH_PUBLIC_PORT}:22"
      - ./${PROJECT_NAME}:/var/www/${PROJECT_NAME}
      - mysqldb

In the above, we are:

  1. Defining arguments that we will pass to our Dockerfile,
  2. Creating a volume that is shared between the local host machine and the docker container that allows us to access and edit our code on the local machine. This allows us to use tools such as PHPStorm to develop our application.
  3. Creating a link between our php-fpm container and the mysqldb container that we created in the last post. This will do two things:  makes sure that mysqldb is created first and allows php-fpm to use the database mysqldb.
  - mysqldb

Next, copy the files found here into the docker/php-fpm/7.0/ directory and these files into the docker/php-fpm/5.6/ directory. In order to keep this post a reasonable length, we will just look at some key points that may be unclear.

Dockerfile (7.0)

FROM php:7.0-fpm

From DockerHub, we will use the7.0-fpm version of the official PHP image.


These are the arguments that are passed in from docker-compose.yml.

COPY 00-opcache.ini /usr/local/etc/php/conf.d/
COPY 00-xdebug.ini /usr/local/etc/php/conf.d/
COPY .bashrc /root/
COPY supervisord.conf /etc/supervisor/conf.d/

These lines copy the files from the local host computer to a directory inside the php-fpm container.

RUN export TERM=xterm && \
    apt-get clean && apt-get update && apt-get install -y \
    apt-utils \
    apt-transport-https \
    ca-certificates \
    ctags \
    indent \
    curl \
    imagemagick \
    graphicsmagick \
    mysql-client \

In the Dockerfile the RUN instruction executes any commands in a new layer on top of the current image and commit the results. This resulting committed image will be used for the next step in the Dockerfile. You can use RUN to break the Dockerfile into steps.

The && ANDs commands together. So, RUN action1 && actions will run action1 and action2. The trailing \ on each line lets the Dockerfile know that the command continues on the next line.

# Change php fpm port
sed -i "s/listen = =${PHP_FPM_PORT}/g" /usr/local/etc/php-fpm.d/www.conf && \
sed -i "s/listen = =${PHP_FPM_PORT}/g" /usr/local/etc/php-fpm.d/www.conf.default && \
sed -i "s/#XDEBUG_IDE_KEY#/${XDEBUG_IDE_KEY}/g" /usr/local/etc/php/conf.d/00-xdebug.ini && \
sed -i "s/#XDEBUG_REMOTE_PORT#/${XDEBUG_REMOTE_PORT}/g" /usr/local/etc/php/conf.d/00-xdebug.ini && \

These lines do inline edits to change the fpm port to the port we specify in the .env file.


Sets the directory that will be used when you log in.

CMD ["/usr/bin/supervisord"]

CMD runs a command inside the container. Here we are calling supervisord.


The supervisord.conf is used when we run the command above.

command=sed -i "s/listen = \[\:\:\]\:9000/listen = \[\:\:\]\:#PHP_FPM_PORT#/g" /usr/local/etc/php-fpm.d/zz-docker.conf

Makes sure that the port we have chosen for php fpm in our configuration file is used.

command=/usr/sbin/sshd -D

This starts the SSH server. Normally, you should not use SSH to access a container. But, if you are using windows, this is the best way to get full keyboard functionality (i.e. arrow keys). And, since this is a development environment, it should be no problem.

command=/usr/local/sbin/php-fpm -c /usr/local/etc/php-fpm.d/www.conf

Start php-fpm.

Now run:

$ docker-compose up --build -d

This will build and run our php-fpm service.

Name               Command                 State        Ports
mysqldb_1 mysqld    Up>3306/tcp
php-fpm_1    docker-php-entrypoint /usr     Up>22/tcp,

We now have a MySQL container and a php-fpm container that you can SSH into using port 2222 with the username:  root and password:  password.

In the next post, we will build the NGINX container.

One thought on “PHP – Docker Development Environment (DDE Part 5)

  • February 8, 2017 at 10:41 pm

    Good point. I hadn’t thghout about it quite that way. 🙂


Leave a Reply

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