• +228 872 7355
MagneticOps - DevOps and Infrastructure Experts
  • Home
  • Our Services
  • Blog
Dockerizing PHP Like a Pro

Dockerizing PHP Like a Pro

magnetic mi
Category: DevOps
23 July 2025
Hits: 377
Rating:
( 0 Rating )

At MagneticOps, we come in contact with a surprising number of clients who have yet to start on their DevOps journey and who’ve never touched Docker. In today’s fast-moving development landscape, consistency, portability, and scalability are key ingredients of success. Whether you’re a solo developer or managing a full-stack team, Docker is your secret weapon for building reliable, repeatable environments, not to mention leveraging or even assessing new server-side technologies.

Docker has become a ubiquitous part of the development-build-test-deploy lifecycle, and if you are working with PHP - like we do - this is no exception. Let’s explore how to Dockerize your PHP application like a pro using modern, community-validated images and best practices.

Why Docker for PHP?

Before diving into the how, let’s nitty-gritty of the why:

  • Consistent environments across dev, staging, and production
  • Faster onboarding for new developers—just pull and run
  • Microservice readiness using NGINX, MySQL, Redis, and more
  • CI/CD compatibility with modern pipelines

Whether you're running Laravel, Symfony, or vanilla PHP, Docker simplifies your stack and removes the classic “but it worked on my machine!” headache.

Getting Started with a Hello World App

To help developers get started quickly, here's a basic 'Hello World' setup using PHP and NGINX. Place this index.php file in your project root:

<?php echo "Hello, World!"; ?>

Sample Docker Setup Using NGINX-Based Community Image

For a professional, extensible, and community-maintained Docker PHP stack, we’ll use the NGINX-based variation provided by the open-source project at ServerSideUp (very cool, you should check them out). Their images are tailored for production and development use-cases and offer a cleaner separation of concerns compared to other images we’ve seen.

Here’s an example docker-compose.yml file  using their images:

version: '3.8'

services:
  app:
    image: serversideup/php:8.2-fpm-nginx
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
    environment:
      APP_ENV: local
      PHP_DISPLAY_ERRORS: "true"

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: app_db
    ports:
      - "3306:3306"

Running docker compose up with that file in the root of your project will cycle through those instructions, and build your ‘app’ and run a database locally for you.

Base image documentation: https://serversideup.net/open-source/docker-php/docs/getting-started/these-images-vs-others

Extending Your Image

Here is a practical and production-ready Dockerfile example that extends the ServerSideUp php:8.2-fpm-nginx image, commonly used by clients who need custom extensions or configuration tweaks:

In many cases our clients will need more customization of their image, installing special PHP extensions, or tweaking nginx. You get all this done by defining those instructions in the Dockerfile.

Sample Dockerfile: Extending the serversideup/php:8.2-fpm-nginx image

# Start from the official ServerSideUp PHP + NGINX image
FROM serversideup/php:8.2-fpm-nginx

# Set working directory
WORKDIR /var/www/html

# Install additional system packages (e.g., git, zip, and PHP extensions)
RUN apt-get update && apt-get install -y \
    git \
    zip \
    unzip \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd
# Optional: Add Xdebug or other dev tools
# RUN pecl install xdebug && docker-php-ext-enable xdebug

# Copy custom php.ini overrides
COPY .docker/php.ini /usr/local/etc/php/conf.d/99-custom.ini

# Copy application files
COPY . .

# Set proper ownership
RUN chown -R www-data:www-data /var/www/html

# Expose port 80 for NGINX
EXPOSE 80

Notes:

  • Custom php.ini file: You can create a local config at .docker/php.ini with overrides like:
  • You will want to tweak your nginx root directory to serve only files you want to expose in your application. Consider /var/www/html/public and move your ‘index.php’ file there.
display_errors=On
upload_max_filesize=50M
memory_limit=512M

  • Add more layers for supervisor, cron, or queue workers as needed.
  • This pattern gives you full control while preserving the structure of a reliable base image.

Dev & Debug Tips

Here are some real world gems that can help push your Docker in the right direction

  • Use volumes to sync your code during development
  • Enable Xdebug if needed by extending the serversideup image
  • Keep services modular—PHP-FPM, NGINX, queues, and cron should be split
  • Use .dockerignore and multi-stage builds to reduce image size and speed up CI/CD

Testing in Containers

Testing inside Docker ensures consistency across dev and CI environments. You can execute PHPUnit directly inside the container:

docker exec -it <app_container> vendor/bin/phpunit

You can automate this step in your CI/CD pipelines when you are ready.

Ready for Production?

If you are ready to get your code into production, let’s talk about it! MagneticOps can work directly with your team on implementing your CI/CD pipeline and help guide production deployments of your application at scale.

Final Thoughts

Dockerizing PHP using an NGINX-based architecture is a powerful way to modernize your workflow. Leveraging open-source images like those from ServerSideUp gives you more transparency, greater control, and production-level quality from the start. This approach helps eliminate environment-specific issues and supports a scalable, secure, and collaborative development process.

References

- ServerSideUp Docker PHP Stack Documentation: https://serversideup.net/open-source/docker-php/

- These Images vs. Others: https://serversideup.net/open-source/docker-php/docs/getting-started/these-images-vs-others

- Docker Compose Docs: https://docs.docker.com/compose/

- PHP Docker Best Practices: https://phpdocker.io/

- Official MySQL Docker Image: https://hub.docker.com/_/mysql

What’s Your Stack Look Like?

Have you Dockerized your PHP project using NGINX yet?

What tools, setups, or lessons would you share?

Drop your thoughts in the comments!

Share this article with your team or dev community.

#PHP #Docker #NGINX #DevOps #WebDevelopment #Containers #OpenSource #LinkedInTech

Custom Post

  • Prev
© 2025 MagneticOps. All Rights Reserved.
  • Home
  • Our Services
  • Blog
Loading…
We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies. Read more about our Privacy Policy.
I accept