Skip to content

Instantly share code, notes, and snippets.

@LouWii LouWii/Dockerfile
Last active Dec 21, 2016

What would you like to do?
Dockerfile for a PHP server with Apache and some php extensions pre-installed

Quick Guide to build and run a Docker image

Build and run

Test Wordpress image

Make sure Docker daemon is running

sudo service docker start

Build Docker image

sudo docker build -t louwii/wordpress-test-1 .

  • -t specify a name for that image so we can find it easily Name and optionally a tag in the 'name:tag' format

Start the image

docker run \
    -d \
    -p 8080:80 \
    -v $(pwd)/src:/var/www/html \
    -v $(pwd)/src/vhost.conf:/etc/apache2/sites-enabled/vhost.conf \
  • -d to run it in the background
  • -p map the port 8080 of the hosting machine to the port 80 of the container
  • -v to mount a folder of the hosting machine to a folder on the container The first is loading our local virtual host config into the container Apache folder The second is setting pointing our local code files to the default directory of the container Apache server (default Apache folder)

For a better, self controlled setup, we can do:

docker run \
    -d \
    -p 8080:80 \
    -v $(pwd)/src/server-conf/mysite.conf:/etc/apache2/sites-enabled/mysite.conf \
    -v $(pwd)/src/mysite:/var/www/mysite \

mysite.conf would have DocumentRoot /var/www/mysite to make it point to our code files.

Check if it's up

docker ps

The result:

CONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS                  NAMES
f1b5e98a5893        louwii/wordpress-test-1   "apache2-foreground"   14 minutes ago      Up 4 minutes>80/tcp   tiny_cray

Check in the browser, something should load ! localhost:8080

Get a shell

docker attach [CONTAINER ID]

But your image might not have a shell instance, so this next command is handy (Docker >= 1.3)

docker exec -i -t [CONTAINER ID] /bin/bash or docker exec -i -t [CONTAINER NAME] /bin/bash

Type exit to exit the shell.

Stop and delete

docker stop [CONTAINER ID] docker rm [CONTAINER ID]

If you only stopped a container, you can start it again with docker start [CONTAINER ID]

To list all containers, even the stopped ones, use docker ps -a

Make local Apache2 to point to the container

Be sure you have enabled all required Apache modules :

sudo a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html

Use a virtual host configuration like this one:

<VirtualHost *:80>

    ProxyPreserveHost On
    ProxyRequests Off
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/

    CustomLog /var/log/apache2/docker_apache_proxy_https_access.log combined
    ErrorLog /var/log/apache2/docker_apache_proxy_https_error.log

In you /etc/hosts file, add this line :

Then in your browser, go to and you should be able to see your site from the Docker container.

FROM php:7.0-apache
# Install PHP extensions and PECL modules.
RUN buildDeps=" \
libbz2-dev \
libmysqlclient-dev \
libcurl4-gnutls-dev \
" \
runtimeDeps=" \
libfreetype6-dev \
libicu-dev \
libjpeg-dev \
libmcrypt-dev \
libpng12-dev \
libpq-dev \
" \
&& apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y $buildDeps $runtimeDeps \
&& docker-php-ext-install bz2 calendar curl iconv intl mbstring mcrypt mysqli pdo_mysql pdo_pgsql pgsql zip \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd \
&& apt-get purge -y --auto-remove $buildDeps \
&& rm -r /var/lib/apt/lists/* \
&& a2enmod rewrite
# Install Composer (optionnal)
ENV COMPOSER_HOME /root/composer
RUN curl -sS | php -- --install-dir=/usr/local/bin --filename=composer

Useful links/tutorials to begin with

Offical PHP images

Chialab PHP Docker image with extensions

Docker Images doc

Docker File Best Practice

Creating a virtual host environment with Docker One PHP app, one Java app, an Nginx container to make them accessible

Connecting Docker Containers, Part One

Dockerizing a PHP Application

Docker for PHP Developers Comprehensive tutorial with 3 containers setup (Nginx, PHP-FPM and MySQL), image creation, etc

For production

Backup a MySQL/MariaDB database Docker container

How to deal with permanent storage (like MySQL DBs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.