Tag: Docker

  • Custom PHP Container

    Custom PHP Container

    As part of the creation of my new lab installation I wanted to migrate WordPress from barebones Apache/FPM into a custom PHP container.

    The original thought was to segretate the web server and PHP FPM from one another – but it seemed to be far to complex. I eventually found this web site:

    Containerize a PHP Application

    This site is talking about a generalised solution for embedding any PHP application into a container, but it turns out the approach worked fine for WordPress too.

    In short, it is easier to start off with a PHP-FPM container. So I chose one from the DockerHub PHP site.

    Having done that, I created a Docker file:

    # Dockerfile.nginx - PHP-FPM with Nginx in a single container
    FROM php:8.3-fpm-alpine
    
    # Install Nginx
    RUN apk add --no-cache nginx curl
    
    # Install PHP extensions
    RUN apk add --no-cache \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        libzip-dev \
        icu-dev \
        postgresql-dev \
        && docker-php-ext-configure gd --with-freetype --with-jpeg \
        && docker-php-ext-install -j$(nproc) \
            gd zip intl pdo pdo_mysql pdo_pgsql opcache
    
    # PHP-FPM configuration
    COPY php-fpm.conf /usr/local/etc/php-fpm.d/www.conf
    COPY php-production.ini /usr/local/etc/php/conf.d/production.ini
    
    # Nginx configuration
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY nginx-default.conf /etc/nginx/http.d/default.conf
    
    # Copy application code
    COPY . /var/www/html/
    RUN chown -R www-data:www-data /var/www/html
    
    # Startup script that runs both Nginx and PHP-FPM
    COPY docker-entrypoint.sh /docker-entrypoint.sh
    RUN chmod +x /docker-entrypoint.sh
    
    EXPOSE 80
    
    HEALTHCHECK --interval=15s --timeout=5s --retries=3 \
      CMD curl -f http://localhost/health.php || exit 1
    
    ENTRYPOINT ["/docker-entrypoint.sh"]

    This file:

    • Installs NGINX
    • Configures NGINX
    • Installs PHP modules.
    • Configures FPM
    • Installs CURL
    • Defines a CURL healthcheck
    • Mounts the application and fixes the permissions.

    TODO: add actual example files.

    I then just archived my installation on the old server, and transferred the directories to the Lab server. These where then mounted as volumes.

    Obviously, I need to do regular checks of the image to make sure it is patched up – but WordPress is working in my custom PHP container (as you can tell by being here now!)