Docker本地Web开发:多站点配置及HTTPS、MySQL问题求助
Hey there, let's tackle those two pain points you're facing with your Docker-based multi-site setup. I've dealt with similar issues while managing client environments, so here's how I'd approach solving them, plus some extra optimizations for your workflow:
解决MySQL相关问题
1. Fixing 2GB Database Import Failures
The default MySQL container config is pretty conservative, which causes timeouts or packet size limits when importing large databases. Here's how to adjust it:
- Create a custom
my.cnffile in your project directory with these tweaks (adjust values based on your host's available RAM):[mysqld] max_allowed_packet=2G innodb_buffer_pool_size=1G wait_timeout=3600 interactive_timeout=3600 - Mount this file into your MySQL container via docker-compose to override default settings:
services: mysql: image: mysql:8.0 volumes: - ./my.cnf:/etc/mysql/conf.d/custom.cnf - mysql_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: your_root_pass - Use command-line import instead of web tools (like phpMyAdmin) for stability. Run this from your host terminal:
This avoids web-based timeouts that often kill large imports.docker exec -i your_mysql_container_name mysql -u root -p your_database_name < /path/to/large_db.sql
2. Connecting Docker Containers to Local MySQL
To let containers access your Ubuntu host's LAMP MySQL instance, follow these steps (safe for local dev only):
- Edit your host's MySQL config (
/etc/mysql/mysql.conf.d/mysqld.cnf) and changebind-addressfrom127.0.0.1to0.0.0.0(allow external connections). Restart MySQL withsudo systemctl restart mysql. - Create a MySQL user that allows connections from any Docker IP (or your local subnet):
CREATE USER 'dev_user'@'%' IDENTIFIED BY 'your_secure_pass'; GRANT ALL PRIVILEGES ON *.* TO 'dev_user'@'%'; FLUSH PRIVILEGES; - In your container's environment variables (or wp-config.php for WordPress), set
DB_HOSTtohost.docker.internal(Docker 18.03+ automatically maps this to your host's IP) or your host's local LAN IP (e.g.,192.168.1.100).
解决HTTPS Access Issues
Since you're using jwilder/nginx-proxy:alpine, setting up HTTPS (with self-signed certs that trigger browser warnings) is straightforward:
- Generate self-signed certificates for your local domains (e.g.,
client1.local,client2.local). Run this from your host:mkdir -p ./certs # For a single domain openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./certs/client1.local.key -out ./certs/client1.local.crt # Or a wildcard cert for all .local domains openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./certs/local.key -out ./certs/local.crt -subj "/CN=*.local" - Update your nginx-proxy docker-compose config to mount the certs directory:
services: nginx-proxy: image: jwilder/nginx-proxy:alpine ports: - "80:80" - "443:443" volumes: - ./certs:/etc/nginx/certs - /var/run/docker.sock:/tmp/docker.sock:ro - Configure your web containers to use HTTPS:
- If your container serves content over HTTP (like default WordPress), just add the
VIRTUAL_HOSTenv var (e.g.,VIRTUAL_HOST=client1.local). nginx-proxy will automatically handle SSL termination—forwarding HTTPS requests from port 443 to the container's port 80. - If your container uses HTTPS natively, add
VIRTUAL_PORT=443andVIRTUAL_PROTO=httpsto its env vars.
- If your container serves content over HTTP (like default WordPress), just add the
Extra Configuration Optimizations
- Per-client docker-compose files: Store each client's stack (web container, optional dedicated MySQL) in its own directory with a
docker-compose.ymland.envfile. This lets you start/stop individual environments withdocker-compose up -dordownwithout affecting others. - Dedicated MySQL containers per client: For better isolation (and to avoid single-container bottlenecks), spin up a separate MySQL container for each client. Use Docker Compose's
extendsfeature to reuse base MySQL config across projects. - Persistent volumes: Always use named volumes for MySQL data and web app files to avoid data loss when containers are deleted.
- Portainer for management: Add a Portainer container to your stack to get a GUI for quickly viewing, starting, and stopping containers—super useful for switching between client environments.
内容的提问来源于stack exchange,提问作者Jeremy Selph




