Docker - Build Apache/PHP Image From Scratch

In this tutorial, we will build a docker image to deploy a simple website that was built with PHP. This can be easily extended by swapping your website into wherever we use "my_website" and updating the apache config part accordingly.

Steps

    Create an empty folder in which we are going to put all our files/configurations into for the image.
    mkdir my_new_image
    cd my_new_image
    The very first thing we need to do is create our directory structure for our website and the index.php file. Here we are just going to create a "hello world" index file within a public_html folder. Feel free to change "my_website" to be the name of your website, but you will need to do this for a few more steps below.
    mkdir -p my_website/public_html
    echo '<?php echo "hello world"; ?>' > my_website/public_html/index.php
    
    We are going to have to update the apache configuration to point to this site. The easiest way to do this is to create our configuration and then use this file overwrite the default one that comes when you install apache later.
    vim apache-config.conf

    Paste the following contents into the file before saving

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
    
        DocumentRoot /var/www/my_website/public_html
        <Directory /var/www/my_website/public_html/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order deny,allow
            Allow from all
        </Directory>
    
    </VirtualHost>
    Now we need to create our Dockerfile. The dockerfile is a set of instructions that is used to build our image. Sort of like a kickstart/seed file.
    vim Dockerfile

    Paste the following instructions into the file

    FROM ubuntu:12.04

    MAINTAINER [YOUR USERNAME HERE]

    # Install the relevant packages
    RUN apt-get update && apt-get install apache2 libapache2-mod-php5 -y

    # Enable the php mod we just installed
    RUN a2enmod php5

    # Add our websites files to the default apache directory (/var/www)
    ADD my_website /var/www/my_website

    # Update our apache sites available with the config we created
    ADD apache-config.conf /etc/apache2/sites-enabled/000-default

    # expose port 8080 so that our webserver can respond to requests.
    EXPOSE 8080

    # Manually set the apache environment variables in order to get apache to work immediately.
    ENV APACHE_RUN_USER www-data
    ENV APACHE_RUN_GROUP www-data
    ENV APACHE_LOG_DIR /var/log/apache2

    # Execute the apache daemon in the foreground so we can treat the container as an
    # executeable and it wont immediately return.
    CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

    Now we can use the newly created Dockerfile to build our image.
    docker build .
    That should
    docker build .
    The previous step should result in the terminal outputting something like:
    Successfully built [Image ID here]

    Take that image ID and execute it like so:

    docker run -p 80:80 [Image ID here]

    The -p 80:80 ensures that the containers port 80 is mapped to the hosts port 80. If you dont want to be left watching the apache log output, then simply stick an & on the end of the command
    That's it! You should now be able to see a "Hello World" message when you navigate to the IP of the docker host in your browser. e.g. something like http://192.168.1.87/

References

4 comments:

  1. cat -f ' new_folder/sub_folder/index.php

    should be

    echo ' my_website/public_html/index.php

    ReplyDelete
    Replies
    1. Thanks for pointing that out! I have updated the blog accordingly.

      Delete
  2. Question for you. The image worked really good and I was able to run a server. I wanted to install some libraries though. So I ran the image as /bin/bash so i could run "apt-get install curl". Then i committed the container. now when i try to run the new image as a server process again it won't work. do you have any idea what I'm doing wrong?

    Thanks!

    ReplyDelete
    Replies
    1. To be honest, I haven't bothered building containers through committing changes in such a long time. I highly recommend just adding curl to the docker file instead, so that when the container is built in the first place, it will have curl. E.g add the line "RUN apt-get install curl" after my "RUN apt-get update && apt-get install apache2 libapache2-mod-php5 -y" or in the dockerfile, or just add curl to that line where I'm installing packages.

      Delete