Docker与SlimFramework新手求助:如何自动初始化MySQL数据表
Hey there! As someone new to Docker and Slim Framework, it makes total sense to hit a few bumps when setting up a multi-service environment like PHP-Apache + MySQL. Let's walk through what's missing in your current setup and build a working solution from scratch.
What's Off With Your Current Dockerfile?
Your Dockerfile sets up a basic PHP-Apache image, but it has two big gaps for your goal:
- It can't start a MySQL service on its own — Dockerfiles build single images, so you need
docker-composeto orchestrate both PHP-Apache and MySQL containers together. - There's no logic to create your required database tables when MySQL starts up.
- Also, the
RUN service apache2 restartline is unnecessary: Apache will automatically start when the container launches, and RUN commands only run during image building (so this restart won't persist to runtime).
Step-by-Step Working Setup
1. Project Structure
First, organize your files like this to keep things clean:
your-slim-project/ ├── docker-compose.yml # Orchestrates PHP and MySQL services ├── Dockerfile # Builds your PHP-Apache image ├── apache2.conf # Your custom Apache config ├── src/ # All your Slim Framework code goes here └── mysql-init/ # Holds SQL scripts to initialize MySQL └── init.sql
2. Updated Dockerfile
Let's tweak your Dockerfile to better fit a Slim project:
FROM php:7.1-apache # Set working directory (where your Slim code lives in the container) WORKDIR /var/www/html # Copy your custom Apache config COPY apache2.conf /etc/apache2/ # Install required extensions and dependencies RUN apt-get update -y && apt-get install -y \ libpng-dev \ curl \ libcurl4-openssl-dev \ && docker-php-ext-install pdo pdo_mysql gd curl \ && a2enmod rewrite # Copy your Slim project code into the container COPY src/ . # Fix file permissions for Apache RUN chown -R www-data:www-data /var/www/html
3. Docker Compose File (docker-compose.yml)
This is where we'll define both the PHP-Apache and MySQL services, and link them together:
version: '3.8' services: php-apache: build: . ports: - "8080:80" # Map host port 8080 to container port 80 volumes: - ./src:/var/www/html # Mount local src folder for live development depends_on: - mysql # Ensure MySQL starts before PHP-Apache environment: # Database connection details (match MySQL service below) DB_HOST: mysql DB_PORT: 3306 DB_NAME: your_database_name DB_USER: your_db_user DB_PASSWORD: your_db_password mysql: image: mysql:5.7 # Use MySQL 5.7 (compatible with PHP 7.1) ports: - "3306:3306" volumes: - mysql-data:/var/lib/mysql # Persist MySQL data between container restarts - ./mysql-init:/docker-entrypoint-initdb.d # Auto-run init scripts on first start environment: MYSQL_ROOT_PASSWORD: your_root_password MYSQL_DATABASE: your_database_name MYSQL_USER: your_db_user MYSQL_PASSWORD: your_db_password volumes: mysql-data: # Named volume to keep MySQL data safe
4. MySQL Initialization Script (mysql-init/init.sql)
Put your table creation SQL here — this script will run automatically when MySQL starts for the first time:
-- Create your tables here CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Add any other table creation or seed data queries here
5. How to Run It
- Replace all placeholder values (like
your_database_name,your_db_user, etc.) in thedocker-compose.ymlandinit.sqlfiles with your actual details. - Open a terminal in your project folder and run:
docker-compose up --build - Once everything starts up:
- Your Slim app will be accessible at
http://localhost:8080 - MySQL will be running on
localhost:3306(use the credentials you set) - Your database and tables will already be created!
- Your Slim app will be accessible at
Key Notes for Your Slim App
In your Slim database configuration, use the environment variables from docker-compose.yml to connect to MySQL. For example:
$dbHost = getenv('DB_HOST'); $dbPort = getenv('DB_PORT'); $dbName = getenv('DB_NAME'); $dbUser = getenv('DB_USER'); $dbPass = getenv('DB_PASSWORD'); $pdo = new PDO( "mysql:host=$dbHost;port=$dbPort;dbname=$dbName;charset=utf8mb4", $dbUser, $dbPass );
内容的提问来源于stack exchange,提问作者Schnecke

