Docker has revolutionized how we develop and deploy PHP applications. Instead of dealing with dependency conflicts, manual installations, and server configuration, you can package your entire PHP app into a Docker container that runs the same everywhere!
π‘ With Docker, you can:
β
Set up isolated development environments
β
Avoid βit works on my machineβ issues
β
Deploy PHP apps quickly on any server
β
Scale applications easily with Docker Compose & Kubernetes
π― In this guide, youβll learn:
β
What Docker is and why you should use it
β
How to Dockerize a PHP application
β
How to use Docker Compose for multi-container apps
β
How to connect PHP with MySQL in Docker
β
How to deploy a Dockerized PHP app to production
By the end, youβll have a fully Dockerized PHP application running in a containerized environment! π
1οΈβ£ What is Docker? (Quick Overview)
π‘ Docker is a tool that lets you package your application and its dependencies into a standardized unit called a container.
π Why Use Docker for PHP Apps?
Problem Without Docker β | Solution With Docker β |
---|---|
Different PHP versions on dev/prod | Works the same on all environments |
Conflicts between dependencies | Each app runs in its own container |
Manual server setup required | Instant setup with docker run |
Hard to share project setup | Share Dockerfile and everyone can run it |
π₯ Docker makes PHP development easy, consistent, and scalable!
2οΈβ£ Install Docker (Skip if You Already Have It)
First, install Docker on your machine:
- Windows & Mac: Download Docker Desktop
- Linux (Ubuntu/Debian):
sudo apt update
sudo apt install -y docker.io
Verify installation:
docker --version
π₯ If you see a version number, Docker is installed! π
3οΈβ£ Create a Simple PHP Application
Before we Dockerize, letβs create a simple PHP application.
1οΈβ£ Create a Project Folder
mkdir php-docker-app && cd php-docker-app
2οΈβ£ Create index.php
<?php
echo "Hello from Dockerized PHP!";
?>
4οΈβ£ Write a Dockerfile for PHP
π‘ A Dockerfile is a blueprint for building a Docker image. It defines which OS, PHP version, and dependencies your application needs.
1οΈβ£ Create a Dockerfile
in the project root
# Use official PHP image with Apache
FROM php:8.2-apache
# Enable PHP extensions
RUN docker-php-ext-install pdo pdo_mysql
# Copy project files to the container
COPY . /var/www/html/
# Expose port 80
EXPOSE 80
π₯ What this does:
β
Uses PHP 8.2 with Apache
β
Installs PDO for database support
β
Copies your PHP files to /var/www/html/
β
Opens port 80 for web access
5οΈβ£ Build and Run the Docker Container
Now, letβs turn our Dockerfile into a running container! π
1οΈβ£ Build the Docker Image
Run:
docker build -t php-docker-app .
π₯ Whatβs happening?
β
docker build
creates an image named php-docker-app
β
.
means use the current directory
2οΈβ£ Run the PHP Container
docker run -d -p 8080:80 php-docker-app
π₯ Now your PHP app is running on http://localhost:8080
! π
6οΈβ£ Adding MySQL to Your PHP Docker Setup
Most PHP applications need a database. Letβs add MySQL in Docker.
1οΈβ£ Create a docker-compose.yml
File
version: "3.8"
services:
php:
build: .
ports:
- "8080:80"
volumes:
- .:/var/www/html
depends_on:
- mysql
mysql:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_db
MYSQL_USER: app_user
MYSQL_PASSWORD: app_pass
ports:
- "3306:3306"
π₯ What this does:
β
Creates a PHP container and links it to MySQL
β
Creates a MySQL database (app_db
) with user app_user
β
Persists data even after container restarts
2οΈβ£ Connect PHP to MySQL
Modify index.php
to connect to MySQL:
<?php
$host = "mysql"; // Docker service name
$user = "app_user";
$pass = "app_pass";
$db = "app_db";
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Database connection failed: " . $conn->connect_error);
} else {
echo "Connected to MySQL in Docker!";
}
?>
3οΈβ£ Start Docker Compose
docker-compose up -d
π₯ Now PHP and MySQL are running together in Docker! π
7οΈβ£ Debugging & Managing Docker Containers
Check Running Containers
docker ps
Stop a Container
docker stop <container_id>
Delete a Container
docker rm <container_id>
View Logs
docker logs php-docker-app
π₯ These commands help manage your containers easily! π
8οΈβ£ Deploying Your Dockerized PHP App
Once your app works locally, deploying it on a server is easy.
1οΈβ£ Push Your Image to Docker Hub
docker tag php-docker-app your-dockerhub-username/php-docker-app
docker push your-dockerhub-username/php-docker-app
2οΈβ£ Deploy on a Remote Server
On the server, run:
docker pull your-dockerhub-username/php-docker-app
docker run -d -p 80:80 your-dockerhub-username/php-docker-app
π₯ Now your PHP app is live on a cloud server! π
9οΈβ£ Best Practices for Dockerizing PHP
β
Use .dockerignore
to exclude unnecessary files
vendor/
node_modules/
.git/
β
Use multi-stage builds to reduce image size
β
Use volumes
instead of copying files to prevent rebuilds
β
Use environment variables for database credentials
π Final Thoughts
Now you can Dockerize PHP applications like a pro!
β
Build a PHP + Apache container
β
Use Docker Compose to add MySQL
β
Run everything locally and in production
π Next Challenge: Deploy this setup to AWS or Kubernetes!
Happy coding! ππ