How to Modify php.ini in Docker Environments

Overview

When running web applications like MediaWiki or WordPress in a Docker environment, you might encounter limitations on file upload sizes regardless of modifications made within the service itself (e.g. changing values for size upload within Localsettings.php config file for a mediawiki installments). This is often due to default PHP settings that restrict upload sizes to a small value, such as 2 MB. Modifying these settings can be challenging in Docker setups because you typically have limited access to the container’s filesystem. This blog post will guide you through the process of increasing the upload size by modifying the php.ini file within an existing Docker container setup working with docker compose.

This tutorial is specifically aimed at Docker setups where these limitations can be a significant hurdle. We’ll walk you through the steps to modify your PHP settings to increase the upload size for services like MediaWiki, WordPress, and others. By the end of this guide, you’ll be able to adjust your Docker configuration to allow larger file uploads without needing extensive access to the container’s filesystem.

Challenges with Docker Setups

Docker containers are built to encapsulate an application and its dependencies into a single, isolated unit. This design ensures that the application runs consistently across different environments, from development to production. Here are some key reasons why Docker containers are designed this way:

Portability: Containers package everything needed to run an application, including code, runtime, system tools, and libraries. This makes it easy to move containers between different environments without compatibility issues.

Efficiency: Unlike virtual machines, containers share the host operating system’s kernel, which reduces overhead and allows for more efficient use of system resources. This means you can run more containers on the same hardware compared to virtual machines.

Isolation: Containers isolate applications from each other and the host system, providing a secure environment. This isolation ensures that applications do not interfere with each other, which is crucial for maintaining stability and security.

Consistency: By encapsulating all dependencies, containers ensure that applications run the same way regardless of where they are deployed. This consistency is vital for development, testing, and production environments.

Finding the right Directory Path

First, you need to find the location of the php.ini file within your Docker container. This step is crucial because the php.ini file contains the configuration settings for PHP, including upload size limits. The file’s location can vary depending on the Docker image and the PHP version used. Finding the correct path ensures that you are modifying the right configuration file.

Locating the php.ini file can be challenging because Docker containers are designed to be isolated environments with minimal access to the filesystem. You might not have direct access to all directories, especially those that are integral to the Docker image. To find the php.ini file, you can use the following bash script which must be applied on your host maschine running docker. Just simply login with ssh to the host maschine and execute the following command:

docker exec -it <container_name> /bin/bash -c 'find / -name php.ini'

This command will search the entire filesystem for the php.ini file and return its path.

Note: On systems like Synology, where you are running your Docker containers within Container Manager, you might have a graphical user interface (GUI) that shows you the exact location of the php.ini file. This can simplify the process of locating and modifying the file.

Webserver Configuration Files

Web servers and PHP are designed to be highly configurable, allowing administrators to fine tune their environments. One way they achieve this flexibility is by supporting additional configuration files in specific directories whereby the primary configuration file (e.g., php.ini) contains the default settings for PHP. This file is read when the PHP interpreter starts. Directories like conf.d are used to store additional configuration files. These files are read after the main configuration file and can override its settings. This allows for modular configuration, where different settings can be managed independently. When PHP starts, it reads the main php.ini file first. Then, it scans the conf.d directory (or similar directories) for additional .ini files and applies their settings. This mechanism allows administrators to add or modify specific settings without altering the main configuration file, making it easier to manage and maintain.

Hence by creating a custom configuration file, you avoid directly modifying the main php.ini file, which can help maintain the integrity of the original configuration. Custom configuration files are easier to manage and can be version-controlled separately from the main configuration.

Creation of Custom Config File and merging with Docker Setup

On your host machine, create a new file named uploads.ini with the following content:

upload_max_filesize = 2G
post_max_size = 2G

Note: Please align the maximum file size to the configuration change made within the service we are going to modify (e.g. Mediawiki).

This file will override the default settings in the main php.ini file when PHP is initialized.

In the next step we are going to modify our docker compose.yml file to mount this custom configuration file into the appropriate directory within the container. This could look like this:

version: '3'
services:
  web:
    image: your_image
    volumes:
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    ports:
      - "80:80"

Note: Please use your existing compose.yaml where you only have to add the mounting point for your new uploads.ini file. Please also ensure that this file was uploaded into the directory beforehand.

By mounting the custom configuration file, you ensure that the settings are applied every time the container starts. After making these changes, restart your Docker container to apply the new settings:

docker-compose down
docker-compose up -d

Note: If you are running a Synology Setup with container manager. You can just rebuild the container with the container manager men entry.

By following these steps, you can effectively increase the upload size for your web applications running in Docker. This approach ensures that your custom PHP settings are loaded when the container starts, allowing you to handle larger file uploads without needing extensive modifications to the container’s filesystem.

Leave a Reply

Your email address will not be published. Required fields are marked *