August 12, 2017

Building a PHP Image

Let's see how we created our PHP image and saved to Docker Hub under the Shipping Docker namespace.

We cover how I built the PHP image we used in our Docker setup.

Note that the video, as usual, is more comprehensive than these notes!

The Dockerfile

We use a Dockerfile to define a base image, and the steps to take to use that base image and create our image from it. We can then use that built image and upload it to a container Registry, such as Docker Hub. Or we can use it locally. Either way, the image can be used to spin up new containers.

Here is the Dockerfile for our PHP container:

Note: An update not in the video:

Near the top of the following Dockerfile, I had to add the installation of the package locales in order to set the locale correctly. A newer version of the base Ubuntu image had this package uninstalled by default.

FROM ubuntu:16.04


RUN apt-get update \
    && apt-get install -y locales \
    && locale-gen en_US.UTF-8


RUN apt-get update \

    && apt-get install -y curl zip unzip git software-properties-common \
    && add-apt-repository -y ppa:ondrej/php \
    && apt-get install -y php-fpm php-cli php-mcrypt php-gd php-mysql \
       php-pgsql php-imap php-memcached php-mbstring php-xml \
    && php -r "readfile('');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && mkdir /run/php \
    && apt-get remove -y --purge software-properties-common \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ADD php-fpm.conf /etc/php/7.0/fpm/php-fpm.conf
ADD www.conf /etc/php/7.0/fpm/pool.d/www.conf

CMD ["php-fpm7.0"]

Build and Push

To build this image, we run the following from the same directory that the Dockerfile resides in. This will the image once, and create (or overwrite!) two new tags:

  • latest tag (by convention, points to the latest build, and is the default tag Docker will look for)
  • 1.0 tag (any other arbitrary tag you'd like to use)

Here's the command to build an image and apply two tags to that build:

docker build -t shippingdocker/php:1.0 -t shippingdocker/php:latest

To push it to Docker Hub, we can login to our account (made previously) and push our images up to it:

# Log into Docker Hub from our CLI client
docker login

# Push up each tagged image
docker push shippingdocker/php:1.0
docker push shippingdocker/php:latest

We also saved this to a Bash script we can use to build our two images (PHP and Nginx). To run the following, use bash build 1.0 to build a new image with tag 1.0 and latest:

#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd $DIR/nginx
docker build -t shippingdocker/nginx:$1 -t shippingdocker/nginx:latest .
docker push shippingdocker/nginx:$1
docker push shippingdocker/nginx:latest

cd $DIR/php
docker build -t shippingdocker/php:$1 -t shippingdocker/php:latest .
docker push shippingdocker/php:$1
docker push shippingdocker/php:latest

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics