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 http://in.archive.ubuntu.com/ubuntu/ubuntu/pool/universe/libr/librabbitmq/librabbitmq4_0.8.0-1_amd64.deb -o librabbitmq4_0.8.0-1_amd64.deb
RUN curl http://in.archive.ubuntu.com/ubuntu/ubuntu/pool/universe/libr/librabbitmq/librabbitmq-dev_0.8.0-1_amd64.deb -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.

Docker: get hash from image name

Particularly if you are using a static image name like “latest” or “develop”, it is useful to be able to know and verify the exact version via the sha256 hash. To get the value you can use:

docker inspect --format='{{index .RepoDigests 0}}' repo/image-name:tag

To use this in a CI/CD workflow for Docker Swarm, you can do something like this:

SHA=$(docker inspect --format='{{index .RepoDigests 0}}' repo/image-name:tag)
docker service update --image $SHA --detach=true --with-registry-auth service-name

Remove Docker images with no tag

Sometimes, particularly on a development environment, you can end up with a lot of Docker images with no tags, which aren’t much practical use. To get rid of them you can do:

docker rmi $(docker images | grep none | awk '{ print $3 }')

Ones in use or with child images will not be deleted and result in output warning, but the rest will be removed so you can deal with the remaining ones as needed.

Docker for Mac clock drift fix

It’s a pain, not sure if there is a “real” fix, but if your Docker host and Mac host clocks drift (presumably the Mac is correct), you can run:

docker run --privileged --rm alpine date -s "$(date -u "+%Y-%m-%d %H:%M:%S")"

I’ve seen differences over 5 minutes, so worth checking…

Docker for Mac fix for max files, transparent hugepages warnings

To avoid Redis and other apps’ “max files limit” and “transparent hugepages” etc errors in Docker containers on Docker for Mac, you can set sysctls options in your docker-compose file by using compose format 2.1 or greater, which should be supported in docker-compose >= 1.10.

in docker-compose.yml file:

version: '2.1'
services:
  redis:
    image: redis:3.2-alpine
    command: redis-server /usr/local/etc/redis/redis.conf
    sysctls:
      - net.core.somaxconn=1024

To update transparent hugepages settings in Docker for Mac’s Alpine hypervisor host system, you can use this hack:

docker run --rm --privileged -ti alpine /bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled && echo never > /sys/kernel/mm/transparent_hugepage/defrag"

In theory you wouldn’t need/want to do this on a production host as you would configure the host properly. If you update Docker, you will need to re-run this.

« Previous Entries