Dockerizing PHP Applications: A Complete Guide πŸš€

Dockerizing PHP Applications: A Complete Guide πŸš€

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:

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! πŸŽ‰πŸš€

Leave a Reply