Docker and Docker Compose with PHP
Docker in practice: Learn how to use Docker Compose to put together an application and set up a PHP development environment. Check it out!

Editorial translation by Impulso.
Containers are here to stay in tech, whatever your specialty. If you’re new to them, they’re a way of developing software in which all the configuration is packaged as a container image. In other words, a single unit can contain different dependencies or pieces of code. This means it is much easier to use and paves the way for a faster setup.
And today, in that world, Docker is the leading container technology on the market. With that in mind, I put together this article to tell you more about Docker and how to use it as a PHP development environment.
Getting started
Since this is a broad and complex topic, I’ve also picked out a few tutorials for anyone who’s never used these tools:
To get everything installed correctly, just follow the links below:
Once the tools are installed, just run these commands to make sure everything worked and check which versions you installed:
$ docker -v Docker version 17.06.1-ce, build 874a737 $ docker-compose -v docker-compose version 1.15.0, build e12f3b9
Now that everything is ready, we can get started. The first file I need to introduce is docker-compose.yml, which defines our services. To start, we’ll work with PHP 7.1.11, Apache and MySQL.
1. docker-compose.yml
version: "3" services: database: image: mysql:5.7.20 restart: always environment: MYSQL_ROOT_PASSWORD: 12345 MYSQL_DATABASE: usuarios MYSQL_USER: dbadmin MYSQL_PASSWORD: dbpassword volumes: - "data:/var/lib/mysql" webserver: image: webdevops/apache:alpine depends_on: - php ports: - "80:80" - "443:443" volumes: - ".:/var/www/html" environment: WEB_PHP_SOCKET: "php:9000" WEB_PHP_TIMEOUT: 600 WEB_DOCUMENT_ROOT: "/var/www/html" php: image: mlalbuquerque/php:7.1 build: context: ./dockerfiles dockerfile: php7.1.dockerfile args: - "UID=$UID" - "GID=$GID" - "USER=$USER" volumes: - ".:/var/www/html" - "./dockerfiles/config/php.ini:/usr/local/etc/php/php.ini" - "./dockerfiles/config/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini" environment: PATH: "/root/.composer/vendor/bin:${PATH}" volumes: data:
How the database service works
The main tool we’re using here is MySQL.
- Image – I’m using the official MySQL (
mysql) image. I also always prefer to specify the tag I’m working with so I know exactly which version of the database I’m using; - Restart – this directive controls what happens if the service stops unexpectedly. In this case, it will always restart. If it’s stopped explicitly (for example, if you stop it on purpose), it won’t restart;
- Environment – this entry defines environment variables for the service. These are usually variables the container uses internally. Of the ones above, the only required one is
MYSQL_ROOT_PASSWORD. All the others are optional, but I set them anyway for clarity. That way, we know the database name, username, and password the infrastructure will use, defined respectively inMYSQL_DATABASE,MYSQL_USER, andMYSQL_PASSWORD; - Volumes – this is always important for container-based services. Otherwise, data is lost when the container is stopped or destroyed. Here, the single entry in
volumeskeeps the database data on the host machine. The image documentation specifies/var/lib/mysqlas the location inside the container where the data is stored. So by mapping thedatavolume to that path, the data will persist on the host, and Docker itself will take care of everything. But remember: when we map volumes, any existing data in the container will be deleted and overwritten by the data on the host.
How the webserver service works
The web server in this case is Apache. I’m using an unofficial image because it offers some handy environment variables that make Apache easier to configure.
- Image – this image comes from the WebDevOps group. It has great configuration settings, is well structured, and offers several options. I use the Alpine version because it’s much smaller (24MB) than versions based on Debian (~90MB) or Ubuntu (~110MB);
- Depends_on – specifies a dependency for this service. In other words, the
webserverservice depends on thephpservice. The services need to start in a particular order, so this one starts only after the service it depends on has started; - Ports – defines the port mappings between the host and the container. The order is always
HOST:CONTEINER. Here, port 80 (HTTP) on the host points to port 80 in the container. The same goes for port 443 (HTTPS). That way, we can access the service athttp://localhost/orhttps://localhost/. Without this mapping, we’d need to know the container’s IP address to access it; - Volumes – this part is important because the volume is created by mapping the local directory (where the docker-compose.yml file lives) to the
/var/www/htmldirectory in the container, which is configured as Apache’s Document Root. This lets Apache serve all the files in the project directory; - Environment – here we have 3 variables that help us configure Apache. We can configure how to access PHP via FPM (
WEB_PHP_SOCKET), the PHP timeout (WEB_PHP_TIMEOUT), and Apache’s Document Root (WEB_DOCUMENT_ROOT). There are other variables you can use if you like.
How the php service works
This service defines the PHP setup used in the environment. Here, I create a new image that doesn’t exist yet. And Docker Compose can handle that pretty easily. I did it this way to bundle the extensions I normally use, Composer (you’re a PHP developer, so of course you know what that is, right?), and Git (Composer depends on it, and we can use it for the project too).
For Image, you can use whatever name you like. I followed Docker’s naming convention: NAMESPACE/CONTAINER:TAG (mlalbuquerque/php:7.1). It’s also worth mentioning docker build. Since the image doesn’t exist locally or on Docker Hub, Docker uses the information below to build it:
- Context – the directory where Docker looks for the files it needs to build the image;
- Dockerfile – if your Dockerfile has a different name, use this directive to specify the filename. In this case, the file is php7.1.dockerfile, and it’s in the
./dockerfilesdirectory; - Args – these are arguments passed at build time (
ARGdirectives in the Dockerfile). Here, I created 3 arguments (UID,GID, andUSER) that take their values from host environment variables ($UID,$GID, and$USER). There’s a catch, though: Docker doesn’t have access to $UID and $GID, so we can use a.envfile (an INI-style file) to set values for environment variables that don’t exist.
2. php7.1.dockerfile
ARG PHP_VERSION=7.1.11-fpm-alpine ARG XDEBUG_VERSION=2.5.5 FROM php:${PHP_VERSION} ARG UID=root ARG GID=root ARG USER
# Installing the required PHP extensions RUN apk add --update --no-cache \ alpine-sdk autoconf curl curl-dev freetds-dev \ libxml2-dev jpeg-dev openldap-dev libmcrypt-dev \ libpng-dev libxslt-dev postgresql-dev \ && rm /var/cache/apk/* RUN docker-php-ext-configure ldap --with-ldap=/usr RUN docker-php-ext-configure xml --with-libxml-dir=/usr RUN docker-php-ext-configure gd --with-jpeg-dir=/usr/include --with-png-dir=/usr/include RUN docker-php-ext-install \ bcmath calendar curl dom fileinfo gd hash json ldap mbstring mcrypt \ mysqli pgsql pdo pdo_dblib pdo_mysql pdo_pgsql sockets xml xsl zip
# Installing XDebug RUN pecl install xdebug-${XDEBUG_VERSION} RUN docker-php-ext-enable xdebug
# Configuring XDebug RUN echo "xdebug.remote_enable = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini RUN echo "xdebug.remote_autostart = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini RUN echo "xdebug.connect_back = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
# Installing Git (Composer uses it to download some packages) RUN apk add --update --no-cache git && rm /var/cache/apk/*
# Installing Composer RUN php -r "copy('http://getcomposer.org/installer', 'composer-setup.php');" RUN php composer-setup.php RUN php -r "unlink('composer-setup.php');" RUN mv composer.phar /usr/local/bin/composer
# Setting the container’s user:group to match the host machine’s user:group (see .env and docker-compose.yml)
This way, Composer uses the same user:group as the host user
This also sets up the directories for the new user
RUN chown -R ${UID}:${GID} /var/www/html RUN chown -R ${UID}:${GID} /root/.composer RUN mkdir -p /.composer && chown -R ${UID}:${GID} /.composer RUN mkdir -p /.config && chown -R ${UID}:${GID} /.config VOLUME /var/www/html VOLUME /root/.composer VOLUME /.composer VOLUME /.config USER ${UID}
3. .env
# For use in Docker
USER => your host username
UID => your user ID (to find it => $ id -u)
GID => your group ID (to find it => $ id -g)
UID=1000 GID=888
Notice that the.env file lets you set environment variables that don’t exist or that Docker can’t use. Docker can access $USER, so there’s no need to set it. The others need to be set and will be substituted in docker-compose.yml. To see the effect, run the command below and check the substitutions:
$ docker-compose config
Since the values appear in the args section within the build section of the php service, they’ll be passed as values for the Dockerfile arguments php7.1.dockerfile (the ARG directives on lines 4 through 6). That way, the image will be built using the arguments in.env.
At this point, we have an environment with Apache 2.4, PHP 7.1, and MySQL 5.7, along with Composer and Git to help with development. Now that everything is in place, just run the command below to start the environment:
$ docker-compose up
If you want to start just one of the services, use one of these commands:
$ docker-compose up database $ docker-compose up webserver $ docker-compose up php
In these cases, the service logs appear in the terminal. If you want to keep the terminal free, just add the -d option after up:
$ docker-compose up -d
All set!
Next steps
Now that you’ve worked through the tutorial, it’s time to put it all into practice. If you have more questions, there’s a great place to get answers from experts: our Discord community! There, you can talk to other developers, get answers, and build your network. Don’t forget to check out other articles on our blog and pick up a few more programming tricks


