惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
The Cloudflare Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
I
InfoQ
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
H
Help Net Security
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Run PHP and MySQL in Docker
Rafli Zocky · 2026-05-09 · via DEV Community
Cover image for Run PHP and MySQL in Docker

Rafli Zocky

It works on my machine.

Steps

1. Create a new directory

.
├── docker-compose.yml
├── Dockerfile
├── .env.example
├── README.md
└── src/
    └── index.php

Enter fullscreen mode Exit fullscreen mode

2. Create docker-compose.yml

version: '3.8'
services:
  app:
    build: .
    container_name: php_app
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      db:
        condition: service_healthy
    env_file:
      - .env
    networks:
      - app_network

  db:
    image: mysql:8.0
    container_name: php_db
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    env_file:
      - .env
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD}"]
      interval: 5s
      timeout: 10s
      retries: 10
    networks:
      - app_network

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      db:
        condition: service_healthy
    ports:
      - 8081:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      PMA_ARBITRARY: ${PMA_ARBITRARY}
    env_file:
      - .env
    networks:
      - app_network

volumes:
  mysql_data:

networks:
  app_network:
    driver: bridge

Enter fullscreen mode Exit fullscreen mode

3. Create Dockerfile

FROM php:8.2-apache
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN a2enmod rewrite

Enter fullscreen mode Exit fullscreen mode

4. Create src/index.php

<?php
$wait = 30;
$db_host = 'db';
$db_name = getenv('DB_DATABASE');
$db_user = getenv('DB_USERNAME');
$db_pass = getenv('DB_PASSWORD');

$start = time();
$connected = false;

while (time() - $start < $wait && !$connected) {
    try {
        $pdo = new PDO(
            "mysql:host=$db_host;dbname=$db_name",
            $db_user,
            $db_pass,
            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
        );
        $connected = true;
        echo "<h2>Database connection successful!</h2>";
        $stmt = $pdo->query("SELECT VERSION()");
        echo "<p>MySQL version: " . $stmt->fetchColumn() . "</p>";
    } catch (PDOException $e) {
        if (time() - $start >= $wait) {
            echo "<h2>Connection failed after $wait seconds</h2>";
            echo "<p>" . $e->getMessage() . "</p>";
        }
        sleep(2);
    }
}

Enter fullscreen mode Exit fullscreen mode

5. Create .env

DB_ROOT_PASSWORD=rootpassword
DB_DATABASE=myapp
DB_USERNAME=myuser
DB_PASSWORD=mypassword

PMA_ARBITRARY=1

Enter fullscreen mode Exit fullscreen mode

How to start

# Start everything
docker-compose up -d

# Check if running
docker-compose ps

# Stop everything
docker-compose down

Enter fullscreen mode Exit fullscreen mode

How to access

Full code: raflizocky/php-docker

References


Need help building your app?
I'm available for freelance web & Android development — raflizocky.netlify.app

☕ Support my writing: paypal.me/raflizocky · saweria.co/raflizocky