Making your own containers can be a super useful way of not only understanding more about docker, but also for understanding more about the systems administration of your applications or docker images. Maybe you want a specific container for running configured services on your machine, or maybe you want to look at packaging your entire system inside a docker container to make it super easy to run. In this quick post, I am going to dover making a Dockerfile and also what to write in it so that you can start making your own containers, today!

Writing Dockerfiles

Dockerfiles are basically configuration scripts that are used to build your container. In fact, each separate RUN command will run that command in a separate container and add it to the previous one. But lets start with some basics.

FROM php:7
MAINTAINER Will Hall "[email protected]"

So, you can see the base image FROM which is the start point of the container, in this case PHP:7. And you can see the MAINTAINER, me.

# Update image
RUN apt-get update -y

# Install Dependencies
RUN apt-get install -y \
    zip \
    unzip \
    patch

# Remove memory limit for PHP-CLI and set timezone
RUN echo "memory_limit = -1" > /usr/local/etc/php/conf.d/php.ini
RUN echo "date.timezone = UTC" >> /usr/local/etc/php/conf.d/php.ini

# Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/bin/composer
RUN chmod +x /usr/bin/composer

# Allow composer superuser and set environment to use composer executables path
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV PATH "$PATH:/root/.composer/vendor/bin"

# Install PHPCS and Drupal Coding Standards
RUN composer global require squizlabs/php_codesniffer
RUN composer global require drupal/coder

# Set Drupal as default CodeSniffer Standard
RUN phpcs --config-set installed_paths /root/.composer/vendor/drupal/coder/coder_sniffer/
RUN phpcs --config-set default_standard Drupal

WORKDIR /app

CMD ["phpcs"]
CMD ["phpcbf"]

You can see that in here we are RUN-ing different systems admin commands. Installing and getting programs and applications. You can then use this docker container to run a Drupal specific version of PHPCS and PHPCBF locally with no setup. To build this new Dockerfile and create the new container you can run:

➜ docker build -t php-7-drupalcs .
Sending build context to Docker daemon 6.656 kB
Step 1 : FROM php:7
...
Step 14 : CMD phpcbf
 ---> Running in 96d5afe4b020
 ---> 251904585933
Removing intermediate container 96d5afe4b020
Successfully built edd2b1339bb4

You can then run commands inside that container (if you remember the container ID).

➜ docker run -it -v $(pwd):/app edd2b1339bb4 phpcbf test.php

So, as an example, here is the whole process of running the custom Drupal PHPCS/PHPCBF container:

PHPCS

➜ echo "<?php " > test.php
➜ docker run -it -v $(pwd):/app edd2b1339bb4 phpcs test.php

----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
1 | ERROR | [x] The PHP open tag must be followed by exactly one
| | blank line
1 | ERROR | [x] Missing file doc comment
1 | ERROR | [x] Expected 1 space after "="; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 240ms; Memory: 6Mb

PHPCBF

➜ docker run -it -v $(pwd):/app edd2b1339bb4 phpcbf test.php

Changing into directory /app
Processing test.php [PHP => 2 tokens in 1 lines]... DONE in 16ms (2 fixable violations)
=> Fixing file: 0/2 violations remaining [made 4 passes]... DONE in 15ms
Patched 1 file
Time: 225ms; Memory: 6Mb