Dockerizing a PHP Application
Build a Docker image for PHP + Nginx and orchestrate with Docker Compose.
Dockerizing a PHP Application is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Docker for PHP?
Docker packages your application with its dependencies into a portable container image. The same image runs identically in development, CI, and production — eliminating "works on my machine" problems.
Basic PHP Dockerfile
A minimal Dockerfile for a PHP + Nginx application.
# Dockerfile
FROM php:8.3-fpm-alpine
# Install extensions
RUN docker-php-ext-install pdo pdo_mysql opcache
# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader
COPY . .
RUN chown -R www-data:www-data /app/storage /app/bootstrap/cacheMulti-Stage Build
Use multi-stage builds to keep the final image small — install build tools in one stage, copy only artifacts to the final image.
# Stage 1: builder
FROM composer:2 AS builder
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader
# Stage 2: runtime
FROM php:8.3-fpm-alpine
WORKDIR /app
COPY --from=builder /app/vendor ./vendor
COPY . .docker-compose.yml
Orchestrate multiple services (PHP-FPM, Nginx, MySQL, Redis) with Docker Compose.
# docker-compose.yml
services:
app:
build: .
volumes:
- .:/app
nginx:
image: nginx:alpine
ports: ["80:80"]
volumes:
- .:/app
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
mysql:
image: mysql:8
environment:
MYSQL_DATABASE: myapp
MYSQL_ROOT_PASSWORD: secret
volumes:
- db_data:/var/lib/mysql
redis:
image: redis:alpine
volumes:
db_data:Nginx Container Config
Mount a custom Nginx config to proxy PHP-FPM requests.
# docker/nginx.conf
server {
listen 80;
root /app/public;
index index.php;
location / { try_files $uri /index.php?$query_string; }
location ~ \.php$ {
fastcgi_pass app:9000; # service name from docker-compose
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}Environment Variables
Pass env vars to containers — never bake secrets into images.
# docker-compose.yml:
services:
app:
env_file: .env
# or explicit:
environment:
- APP_ENV=production
- DB_HOST=mysqlLaravel Artisan Commands
Run Artisan commands inside the container.
$ docker compose exec app php artisan migrate
$ docker compose exec app php artisan config:cache
$ docker compose exec app php artisan queue:workHealth Checks
Add health checks so orchestrators (Kubernetes, ECS) know when the container is ready.
# Dockerfile:
HEALTHCHECK --interval=30s --timeout=5s \
CMD curl -f http://localhost/health || exit 1Building and Pushing Images
Build and push your image to a registry (Docker Hub, ECR, GHCR).
$ docker build -t myorg/myapp:1.0.0 .
$ docker push myorg/myapp:1.0.0Kubernetes Deployment
In Kubernetes, define a Deployment and Service resource using your image. Roll out new versions with kubectl set image or update the YAML and apply.
Production Hardening
Production Docker practices: use non-root user, read-only root filesystem, minimal base image (Alpine), no dev dependencies, pin image tags, and scan for CVEs with docker scout.
Summary
Create a Dockerfile for PHP-FPM + Composer. Use docker-compose for local development with Nginx, MySQL, and Redis services. In production, use multi-stage builds, inject env vars, and run via Kubernetes or ECS.
Quick Check
What is the main benefit of multi-stage Docker builds?
Frequently asked questions
Is the “Dockerizing a PHP Application” lesson free?
Yes — the full text of “Dockerizing a PHP Application” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Dockerizing a PHP Application”?
Build a Docker image for PHP + Nginx and orchestrate with Docker Compose. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Dockerizing a PHP Application” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nginx and PHP-FPM Configuration
- Environment Variables and .env Files
- Deploying with GitHub Actions
- Dockerizing a PHP Application