PHP is a widely utilized server-side scripting language known for its integration with HTML and efficiency in web development. Docker simplifies the process of containerizing PHP environments, allowing for portable and reproducible deployments.
Running PHP Scripts in Docker
For quick testing or execution of PHP scripts, you do not always need to build a custom image. You can run scripts directly using the official PHP CLI image.
Example script (app.php):
<?php
echo "PHP engine is active and running.";
?>
You can execute this script without a local installation of PHP by mounting your source directory into a transient container:
docker run --rm -it \
--name php-executor \
-v $(pwd):/app \
-w /app \
php:8.2-cli-alpine php app.php
The --rm flag ensures the container is automatically removed after execution, while the -v flag maps your local directory to the container's working directory.
Deploying WordPress with Docker
WordPress relies on a relational database (MySQL/MariaDB). The most efficient way to deploy its by using Docker Compose or linked containers to manage the stack.
1. Provision the Database
docker run --name wp-database \
-e MYSQL_ROOT_PASSWORD=securepassword \
-e MYSQL_DATABASE=wordpress_db \
-d mysql:8.0
2. Launch the WordPress Application
docker run --name my-blog \
--link wp-database:db \
-e WORDPRESS_DB_HOST=db \
-e WORDPRESS_DB_PASSWORD=securepassword \
-e WORDPRESS_DB_NAME=wordpress_db \
-p 8080:80 \
-d wordpress
Once deployed, navigate to http://localhost:8080 to finalize the installation via the web interface.
Managing PHP Configuration (File Upload Limits)
PHP containers often have restrictive default upload limits (e.g., 2MB). To increase these, you must modify the upload_max_filesize and post_max_size settings in the php.ini configuration.
Method A: Mounting a Custom Configuration
You can override specific settings by mapping a local .ini file into the container's configuration directory:
docker run -v ./custom-uploads.ini:/usr/local/etc/php/conf.d/uploads.ini -p 8080:80 wordpress
The contents of custom-uploads.ini should include:
upload_max_filesize = 64M
post_max_size = 64M
Method B: Building a Custom Image
For production environments, it is best practice to bundle your configurations into a custom Dockerfile:
FROM wordpress:latest
COPY ./config/uploads.ini /usr/local/etc/php/conf.d/uploads.ini