Skip to content

Instantly share code, notes, and snippets.

@rquadling
Last active October 5, 2020 01:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rquadling/53fd99994d653b0a1b88f4a0705ae655 to your computer and use it in GitHub Desktop.
Save rquadling/53fd99994d653b0a1b88f4a0705ae655 to your computer and use it in GitHub Desktop.
Adding XDebug to Bitnami's php-fpm container.
# An example Dockerfile to build a Bitnami PHP-FPM minimal container with xDebug and some other extensions installed.
# Please note, I have cobbled this together from limited understanding as well as dissecting the Dockerfiles that
# Bitnami have used.
#
# If you find problems, or suggestions, PLEASE PLEASE help me and the rest of the community with your comments,
# suggestions, fixes, etc. Let's all learn something new!!!!
#
# If you want to contact me directly, then RQuadling at the Google Mail, and I'll be happy to answer any questions.
# This example Dockerfile is going to build a PHP-FPM 7.2 container with xDebug 2.6.0.
# You can modify those version numbers to suite your requirement. Note that PHP 7.2 and xDebug 2.5.5 do not mix as per
# https://xdebug.org/download.php
# This is going to be a multi-stage build. The main benefit I've found of a multi-stage build is that the final image
# is very minimal in terms of the number of layers and overall image size. Which, in theory, should mean a more
# efficient overall experience.
# First up, we need the non-prod version of the Bitnami PHP-FPM container that you need to build xDebug for.
FROM bitnami/php-fpm:7.2 as builder
# We need to install some build tools.
# Initially, I thought that these would be part of the non-prod container, but seems not.
# It is entirely possible I've misunderstood something. If so, help me correct this!!!
# I am also installing memcached extension which requires additional shared libraries.
RUN install_packages \
build-essential autoconf ca-certificates pkg-config \
libmemcached-dev libmemcached11 libmemcachedutil2 libhashkit2
# We are going to use PECL to install xDebug, as well as some additional extensions.
RUN \
pecl install \
igbinary \
memcached \
msgpack \
pdo_dblib \
redis \
timecop-beta \
timezonedb \
xdebug-2.6.0
# So. That's the build of all the extensions.
# Now we need to build our minimal production ready container.
# To do that, we are doing a multi-stage build.
# So. The next step is to use the production ready version of PHP-FPM.
# Make sure you use the same version of the -prod as you did above. Otherwise, you will have built the extensions
# with the wrong ABI and then there will be tears when things don't work.
FROM bitnami/php-fpm:7.2-prod
# Copy dependant shared libraries.
# I found this using trial and error. Each extension could have its own dependant binary libraries.
# If the build container has them, then they can just be copied. If not, they they would need to be installed as part
# of the install_packages command.
COPY --from=builder \
/usr/lib/x86_64-linux-gnu/libmemcached*.so.*.*.* \
/usr/lib/x86_64-linux-gnu/libsybdb.so.*.*.* \
/usr/lib/x86_64-linux-gnu/
# Make symlinks for shared dependant libraries.
RUN \
ln -sf /usr/lib/x86_64-linux-gnu/libmemcached.so.11.0.0 /usr/lib/x86_64-linux-gnu/libmemcached.so.11.0 && \
ln -sf /usr/lib/x86_64-linux-gnu/libmemcached.so.11.0.0 /usr/lib/x86_64-linux-gnu/libmemcached.so.11 && \
ln -sf /usr/lib/x86_64-linux-gnu/libmemcached.so.11.0.0 /usr/lib/x86_64-linux-gnu/libmemcached.so && \
ln -sf /usr/lib/x86_64-linux-gnu/libmemcachedutil.so.2.0.0 /usr/lib/x86_64-linux-gnu/libmemcachedutil.so.2.0 && \
ln -sf /usr/lib/x86_64-linux-gnu/libmemcachedutil.so.2.0.0 /usr/lib/x86_64-linux-gnu/libmemcachedutil.so.2 && \
ln -sf /usr/lib/x86_64-linux-gnu/libmemcachedutil.so.2.0.0 /usr/lib/x86_64-linux-gnu/libmemcachedutil.so && \
ln -sf /usr/lib/x86_64-linux-gnu/libsybdb.so.5.0.0 /usr/lib/x86_64-linux-gnu/libsybdb.so.5.0 && \
ln -sf /usr/lib/x86_64-linux-gnu/libsybdb.so.5.0.0 /usr/lib/x86_64-linux-gnu/libsybdb.so.5 && \
ln -sf /usr/lib/x86_64-linux-gnu/libsybdb.so.5.0.0 /usr/lib/x86_64-linux-gnu/libsybdb.so
# Copy the built extensions. I suppose this is the clever bit. Taking the production container and copying binaries
# from the builder container. No build tools, no temp/junk/artefacts. Just the required binaries. Just one of the
# very useful uses of multi-stage builds.
COPY --from=builder \
/opt/bitnami/php/lib/php/extensions/igbinary.so \
/opt/bitnami/php/lib/php/extensions/memcached.so \
/opt/bitnami/php/lib/php/extensions/msgpack.so \
/opt/bitnami/php/lib/php/extensions/pdo_dblib.so \
/opt/bitnami/php/lib/php/extensions/redis.so \
/opt/bitnami/php/lib/php/extensions/timecop.so \
/opt/bitnami/php/lib/php/extensions/timezonedb.so \
/opt/bitnami/php/lib/php/extensions/xdebug.so \
/opt/bitnami/php/lib/php/extensions/
# And that's it really.
# Initially, there was no simple way to get the necessary ini entries in, but recently, Bitnami updated their build
# environment to support the '--with-config-file-scan-dir' option, so that means we can now mount a directory of
# ini files into the container rather that needing to bake them into the image.
#
# The Bitnami images that now support the scan directory option are 7.2.3-r2, 7.1.15-r2, 7.0.28-r2, and 5.6.34-r2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment