Skip to content

Instantly share code, notes, and snippets.

@LouWii LouWii/Dockerfile
Last active Dec 21, 2016

Embed
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 \
    louwii/wordpress-test-1;
  • -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 \
    louwii/wordpress-test-1;

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        0.0.0.0:8080->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>
    ServerName mysite.dev

    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
</VirtualHost>

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

127.0.0.1 mysite.dev

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

FROM php:7.0-apache
MAINTAINER louwii@louwii.fr
# 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 https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH

Useful links/tutorials to begin with

Offical PHP images https://hub.docker.com/_/php/

Chialab PHP Docker image with extensions https://hub.docker.com/r/chialab/php/

Docker Images doc https://docs.docker.com/engine/tutorials/dockerimages/

Docker File Best Practice https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

Creating a virtual host environment with Docker One PHP app, one Java app, an Nginx container to make them accessible http://tech.mybuilder.com/virtual-hosts-with-docker/

Connecting Docker Containers, Part One https://deis.com/blog/2016/connecting-docker-containers-1/

Dockerizing a PHP Application https://semaphoreci.com/community/tutorials/dockerizing-a-php-application

Docker for PHP Developers Comprehensive tutorial with 3 containers setup (Nginx, PHP-FPM and MySQL), image creation, etc http://www.newmediacampaigns.com/blog/docker-for-php-developers

For production

Backup a MySQL/MariaDB database Docker container https://blog.confirm.ch/backup-mysql-mariadb-docker-container/

How to deal with permanent storage (like MySQL DBs) https://stackoverflow.com/questions/18496940/how-to-deal-with-persistent-storage-e-g-databases-in-docker/20652410#20652410

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.