While Docker is an excellent platform for rapidly developing and deploying code, the official Docker containers are often quite minimal. Consequently, they often lack tools that we need from time to time to debug network problems and to use Composer. In this tutorial, I’m going to take you through several tools that I often add to the official Docker PHP images I use to make my life that much easier.
:idseparator: -
:idprefix:
:experimental:
:source-highlighter: rouge
:rouge-style: pastie
:imagesdir: /images
:source-linenums-option: on
== Prerequisites
To follow along with this tutorial, make sure that you have Docker installed for your operating system, ideally the latest version.
You can find all the instructions you need, regardless of whether you’re running Linux, macOS, or Windows https://docs.docker.com/get-docker/[in the Docker documentation].
== Here are the tools that I regularly install
The main issues that I run into on a fairly regular basis are doing ping tests, performing basic network debugging, and running Composer install and update, https://matthewsetter.com/docker-development-environment/[when I’m developing].
=== For working with Composer
Let’s start with https://getcomposer.org[Composer].
It might sound weird to run Composer inside link:/tags/docker/[a Docker container], but I like to keep my development machine as light as possible, rather having all of the runtime dependencies and related services in Docker containers.
So it’s quite often the case that the PHP runtime on my macOS or Linux development machine is either EOL or missing one or more extensions, or the extensions that are there have only the default configuration options set.
TIP: This is especially important as https://www.macobserver.com/analysis/macos-catalina-deprecates-unix-scripting-languages/[macOS 10.15 Catalina deprecates UNIX scripting languages], including PHP.
Given that, it’s not uncommon to find that composer install
and composer update
fail locally because the container’s missing zip and unzip.
What’s more, the official Docker PHP container also doesn’t come with Git, so because of that, install and update often fail, as the packages cannot be downloaded nor unzipped.
.Basic Dockerfile
[source,dockerfile,linenums]
FROM php:7.4-fpm
So let’s assume that you’re building your container using the Dockerfile above, which bases itself on https://hub.docker.com/_/php/[the official php:7.4-fpm container].
To augment it so that it has at least the minimum required to run a Composer install or update, update your Dockerfile, so that it has the highlighted lines in the Dockerfile configuration below:
.Dockerfile that will install the required packages to use Composer
[source,dockerfile,linenums,highlight=3..8]
FROM php:7.4-fpm
RUN apt-get update
&& apt-get install -y
git
libzip-dev
unzip
&& docker-php-ext-install zip
As this container is based on Debian, then the Dockerfile configuration is using apt-get to install git, unzip, and PHP’s https://www.php.net/manual/en/book.zip.php[zip extension], which requires libzip-dev.
[TIP]
If you’re using Alpine Linux, then use the follow configuration instead.
[source,dockerfile]
====
=== For basic network debugging
Next, let’s look at basic network debugging.
As any Network or Linux System Administrator will tell you, there is a significant array of ways and reasons to debug a Linux system.
I’d argue in development that the needs are more limited, which is a good thing, as we don’t need to install as many tools.
The tools that I most often need are ping, to check for connectivity to other containers, and one of https://www.tutorialspoint.com/unix_commands/ifconfig.htm[ifconfig] or https://linuxize.com/post/linux-ip-command/[ip] to analyse the current network configuration.
For example, recently I needed to find out the IP of the host machine, from the Docker container’s perspective, so that I could setup step-through debugging with PhpStorm properly.
For that, I needed ip
.
To have these available, I needed to install these iputils-ping, iproute2, and net-tools.
To do that, add the RUN
element below to your Dockerfile.
[source,dockerfile,linenums]
=== To find out which package I need to install
For the longest time, I’ve struggled to find out, when I needed to know, which package provided a given binary.
Recently, I found out about apt-file
, which does just that.
According to https://wiki.debian.org/apt-file[its documentation]:
[quote]
[apt-file] is a software package that indexes the contents of packages in your available repositories and allows you to search for a particular file among all available packages.
You can see an example of how to use it to find out which package provides /sbin/ifconfig
in the following example.
[source,bash]
apt-file search
–package-only
–regexp ‘/sbin/ifconfig$’
So, to install it, add the following RUN element to your Dockerfile.
[source,dockerfile]
RUN apt-get update
apt-get install -y apt-file
In the example below, you can see how to combine searching and installing in one command.
.Find and install the package (iproute2) that provides “/sbin/ifconfig”
[source,bash]
apt-get install -y $( apt-file search –regexp ‘/sbin/ifconfig$’ )
== That’s a wrap
While link:/tags/docker[Docker] makes it so much easier than ever before to develop and to deploy software with a minimum of effort, the official containers are, justifiably, only equipped with just enough to perform certain tasks.
It makes them light and nimble, out of the proverbial box, it can be hard to debug them when things, invariably, go wrong, or don’t work as expected.
This article has stepped through a couple of the extra tools that I usually install to help out with my debugging needs.
It’s not the complete list, but definitely the most common.
Which ones do you use?