Install amqp-ext PHP extension to Apache in Docker

I wanted to add RabbitMQ support to my PHP app and prefer to use the PECL amqp package:

This extension can communicate with any AMQP spec 0-9-1 compatible server, such as RabbitMQ, OpenAMQP and Qpid, giving you the ability to create and delete exchanges and queues, as well as publish to any exchange and consume from any queue.

Using pecl install won’t install all the dependencies. There are various options to how to solve this including fetching from Git repos, however this is the one that worked for me and seemed the most straight-forward (ymmv). See below for code used in a Dockerfile with base image FROM php:7.1.14-apache which uses debian:jessie, so the native commands should work in similar Debian/Ubuntu linux distros.

RUN curl -o librabbitmq4_0.8.0-1_amd64.deb
RUN curl -o librabbitmq-dev_0.8.0-1_amd64.deb
RUN dpkg -i librabbitmq4_0.8.0-1_amd64.deb
RUN dpkg -i librabbitmq-dev_0.8.0-1_amd64.deb
RUN pecl install amqp-1.9.3 && docker-php-ext-enable amqp

Note if you do use this in a Dockerfile, you can combine some of the statements to optimize layers. You also may need to update the lib version if it’s updated or if you change the PECL version to install.

Prevent extension version issues using PECL with multiple PHP installs

Simply calling phpize, config and make to compile a new extension, even if you point directly to your php binary can still compile with the wrong version as can installing via PECL, even if you reference the pecl binary directly. Using the peclcmd.php script, however, you should be able to get it to load the correct libraries and install in the right place.

For instance, I recently installed PHP 5.4 in /usr/local/php5 to use instead of the native Mac PHP 5.3 install (via but I wanted to leave the Mac PHP in place so now I have multiple versions of PHP and can use either one, which is great for dev but can cause compile issues.

On one machine I was eventually able to manually download the extension via pecl download and then phpize and install by passing: ./configure --with-php-config=/usr/local/php5/bin/php-config to the build config but wasn’t able to figure out how to do this directly via pecl. When I next tried the same update to another machine, I remembered the peclcmd file and was able to simply use a single command: /usr/local/php5/bin/php /usr/local/php5/lib/php/peclcmd.php install {extension-name} and done.

Ymmv :)

While I’m comfortable compiling from scratch, it definitely can save a lot of headaches to use a packager if it gives you what you need and make updates a lot easier.