Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungReact Children vs Slots: When to Use Each Pattern?(19.09.2026 um 18:43 Uhr)
Sichere ProgrammierungDoppar 4 Has Been Released, and Here Is Why It Is Special(19.09.2026 um 18:47 Uhr)
Sichere ProgrammierungA Kafka Streams topology starts in a sandbox, not on your cluster(19.09.2026 um 19:05 Uhr)
Sichere ProgrammierungThe End of Traditional UX(19.09.2026 um 19:12 Uhr)
Sichere ProgrammierungThe best comment I ever got was someone proving my code wrong(19.09.2026 um 19:16 Uhr)
Sichere ProgrammierungStop Hosting Client WordPress Sites in One Shared Account(19.09.2026 um 19:20 Uhr)
Sichere ProgrammierungReact Children vs Slots: When to Use Each Pattern?(19.09.2026 um 18:43 Uhr)
Sichere ProgrammierungDoppar 4 Has Been Released, and Here Is Why It Is Special(19.09.2026 um 18:47 Uhr)
Sichere ProgrammierungA Kafka Streams topology starts in a sandbox, not on your cluster(19.09.2026 um 19:05 Uhr)
Sichere ProgrammierungThe End of Traditional UX(19.09.2026 um 19:12 Uhr)
Sichere ProgrammierungThe best comment I ever got was someone proving my code wrong(19.09.2026 um 19:16 Uhr)
Sichere ProgrammierungStop Hosting Client WordPress Sites in One Shared Account(19.09.2026 um 19:20 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Dockerize Your PHP and MySQL Application: A Step-by-Step Guide to Multi-Container Apps

In the world of modern software development, containerization has become a cornerstone of efficient deployment and scalability. Docker makes it easy to package applications and their dependencies into portable containers. In this blog, we’ll walk you through creating a multi-container PHP and MySQL application using Docker Compose.

Why Use Docker for PHP and MySQL?

Consistency: Run your app in the same environment across all stages of development and production.
Isolation: Keep services like PHP and MySQL in separate containers for modular and maintainable setups.
Ease of Scaling: Add or remove containers easily as the app grows.

Project Structure

Your project directory should look like this:

project/
│
├── php-app/
│   ├── Dockerfile
│   └── index.php
│
└── docker-compose.yml

Docker Compose File

Here’s the docker-compose.yml file:

version: '3.8'

services:
  php-app:
    build: ./php-app  # Builds the PHP app Docker image from the ./php-app directory
    ports:
      - "8080:80"  # Maps port 8080 on the host to port 80 in the container
    depends_on:
      - mysql  # Ensures MySQL starts before the PHP app

  mysql:
    image: mysql:8.0  # Uses the official MySQL image
    ports:
      - "3306:3306"  # Maps port 3306 on the host to port 3306 in the container
    environment:
      MYSQL_ROOT_PASSWORD: root  # Root password for MySQL
      MYSQL_DATABASE: mydb  # Initial database to create
    volumes:
      - mysql-data:/var/lib/mysql  # Persists MySQL data in a named volume

volumes:
  mysql-data:  # Declares a named volume for MySQL data persistence

PHP Application Code

The index.php file connects to the MySQL container:

<?php
$servername = "mysql";  // Service name defined in docker-compose.yml
$username = "root";
$password = "root";
$database = "mydb";

try {
    // Create connection
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);

    // Set PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "Connected to MySQL successfully!";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

Dockerfile for PHP App

The Dockerfile builds the PHP container with required dependencies:

# Use the official PHP image
FROM php:8.2-apache

# Install PDO MySQL extension
RUN docker-php-ext-install pdo pdo_mysql

# Set the working directory
WORKDIR /var/www/html

# Copy the application code
COPY index.php .

# Expose port 80
EXPOSE 80

Steps to Build and Run

Create the Directory Structure

  • Place Dockerfile and index.php in the php-app folder.
  • Place docker-compose.yml in the root project folder.

Build and Start the Containers Open a terminal in the project directory and run:

docker-compose up --build

Access the PHP Application Visit http://localhost:8080 in your browser. If everything is set up correctly, you should see:

Connected to MySQL successfully!

Verify the MySQL Database

Use a MySQL client or MySQL Workbench.
Connect to localhost:3306 with:
Username: root
Password: root
Verify the database mydb is created.

Key Features of This Setup

Service Dependency Management: depends_on ensures MySQL starts before the PHP application.
Data Persistence: The mysql-data volume stores MySQL data, preventing loss across container restarts.
Port Mapping: Easily access services through ports mapped to the host machine.

Extending the Setup

Add more services like Nginx or Redis to the Compose file.
Use .env files to manage sensitive configurations.
Scale services using Docker Compose’s scale option for horizontal scaling.

Conclusion

With this setup, you've built a scalable, modular PHP and MySQL application using Docker Compose. Containerization simplifies environment management and prepares your app for cloud deployments. Whether you’re a beginner or a seasoned developer, Docker is an indispensable tool for modern application development.

Ready to take your setup to the next level? Try integrating this with CI/CD pipelines or Kubernetes for a production-grade environment.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Dockerize Your PHP and MySQL Application: A Step-by-Step Guide to Multi-Container Apps

Thematisch verwandte Begriffe: Dockerize, Your, MySQL, Application · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-61591 | djust provides Phoenix LiveView-style reactive server-side rendering for…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick