2 minutes
Understanding Docker Commands for PHP on Cloud Run
This post comes from the google Deploy PHP service to Cloud Run guide.
Deploying PHP applications on Cloud Run involves specific Docker commands for configuration and optimization. Below is a detailed guide on each command used in the Dockerfile.
1. Setting the Base Image
FROM php:8.3-apache
This command sets the base image to PHP 8.3 with Apache, providing a pre-configured environment for PHP applications. Using this base image reduces image size, ensures a standardized environment, and speeds up the build process.
2. Installing PHP Extensions
RUN docker-php-ext-install -j "$(nproc)" opcache
This installs the opcache
extension using all available CPU cores, enhancing PHP performance by caching script bytecode.
3. Custom PHP Configuration
RUN set -ex; \
{ \
echo "; Cloud Run enforces memory & timeouts"; \
echo "memory_limit = -1"; \
echo "max_execution_time = 0"; \
echo "; File upload at Cloud Run network limit"; \
echo "upload_max_filesize = 32M"; \
echo "post_max_size = 32M"; \
echo "; Configure Opcache for Containers"; \
echo "opcache.enable = On"; \
echo "opcache.validate_timestamps = Off"; \
echo "; Configure Opcache Memory (Application-specific)"; \
echo "opcache.memory_consumption = 32"; \
} > "$PHP_INI_DIR/conf.d/cloud-run.ini"
This snippet configures PHP settings for Cloud Run, such as disabling memory limits and setting file upload sizes, while optimizing Opcache settings for containerized environments.
4. Setting the Working Directory
WORKDIR /var/www/html
This sets the working directory within the container to /var/www/html
, ensuring that subsequent commands execute relative to this directory.
5. Copying Application Code
COPY . ./
This copies the application code from the host machine to the container’s working directory.
6. Configuring Apache Ports
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
This command replaces the default port (80) with the environment variable ${PORT}
, configuring Apache to use the port specified by Cloud Run.
7. Switching PHP Configuration Files
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
This renames the php.ini-development
file to php.ini
, activating the development configuration.
Conclusion
By understanding and utilizing these Docker commands, you can efficiently configure and deploy PHP applications on Cloud Run, ensuring optimal performance and compatibility with Cloud Run’s environment.