Docker部署MySQL容器启动后退出,-d参数无效,现有方案未解决
Hey there, let's dig into why your MySQL container is exiting immediately on Windows 10. I've fought through similar issues with old Docker versions and Windows-specific quirks, so let's break this down step by step.
First off, the Exited(1) status always has a specific cause—we just need to uncover it. Let's start with the most critical step:
Step 1: Grab the Container Exit Logs
This is non-negotiable. Run this right after the container exits to see the exact error message (it'll tell you if it's a port conflict, SQL script issue, permission problem, etc.):
# Replace <container-name-or-id> with the value from `docker ps -a` docker logs <container-name-or-id>
Without these logs, we're guessing—but let's cover the most likely Windows-specific and Dockerfile-related issues anyway.
Step 2: Check for Local Port Conflicts
If you have a native MySQL server running on your Windows machine, it's hogging port 3306, which will crash your container. Verify this with:
netstat -ano | findstr :3306
If you see a process using this port, stop your local MySQL service or run the container on an alternate port:
docker run -p 3307:3306 -d mysqlserver
Step 3: Test Without Your Initialization Script
Your create_schema.sql file might have syntax errors that break the container's startup sequence. Let's rule this out:
- Modify your Dockerfile to comment out the COPY line:
FROM mysql/mysql-server ENV MYSQL_ROOT_PASSWORD root ENV MYSQL_DATABASE blockchain ENV MYSQL_USER block ENV MYSQL_PASSWORD blockchain # COPY create_schema.sql /docker-entrypoint-initdb.d/create_schema.sql EXPOSE 3306 - Rebuild with
docker build -t mysqlserver .and run the container. If it stays up, the problem is in your SQL script—double-check for missing semicolons, unsupported keywords, or mismatched MySQL versions.
Step 4: Fix File Permissions for Your SQL Script
Windows file permissions often get mangled when copied into Linux containers. If the mysql user can't read your script, the container will exit. Add a permission fix to your Dockerfile:
FROM mysql/mysql-server ENV MYSQL_ROOT_PASSWORD root ENV MYSQL_DATABASE blockchain ENV MYSQL_USER block ENV MYSQL_PASSWORD blockchain COPY create_schema.sql /docker-entrypoint-initdb.d/ # Make the script readable by the container's mysql user RUN chmod 644 /docker-entrypoint-initdb.d/create_schema.sql EXPOSE 3306
Rebuild and try again.
Step 5: Update Your Docker Version
Your Docker version (17.12.0-ce) is over 6 years old—Windows Docker has had massive improvements since then, especially with WSL2 integration. Old versions have known compatibility bugs with Windows file systems and container initialization. Head to Docker Desktop's settings to update to the latest stable release.
Step 6: Verify WSL Integration (If Using WSL)
If you're running Docker with WSL, ensure you're using WSL2 (WSL1 has severe file system limitations):
- Open Docker Desktop > Settings > Resources > WSL Integration
- Check that your default WSL distro is set to WSL2 (verify with
wsl --list --verbosein PowerShell) - Restart Docker Desktop after making changes.
Bonus: Run in Foreground to See Live Logs
Instead of using -d right away, run the container in foreground mode to watch the startup process in real time—this makes it easier to catch errors as they happen:
docker run -p 3306:3306 mysqlserver
Once you get the logs from Step 1, you'll have a precise answer—but these steps should cover 90% of the Windows-specific issues causing your container to exit.
内容的提问来源于stack exchange,提问作者david

