Docker Compose MySQL Keeps Restarting
Run into a strange bug where the MySQL container would keep restarting. The error I would see from the logs would be
[ERROR] --initialize specified but the data directory has files in it. Aborting.
You can view container logs by typing
docker logs -f <db_container_name>
Update, October 2026
The original version of this post suggested docker system prune --volumes. That command is broader than this database: it can remove unrelated stopped containers, unused networks, images and build cache, as well as unused anonymous volumes. Those volumes can contain database data. See the Docker prune documentation.
If you see this error, first stop the database restart loop and inspect the container’s mounts to identify the exact data directory or volume. Back up any existing data before changing it. Check the MySQL logs and the image’s initialization requirements; rebuilding the image does not fix an existing data directory.
For a disposable development database, you can recreate only that database’s data directory or volume once you have confirmed it contains nothing you need. Do not use a system-wide prune as a database repair step.
The original MySQL image issue has more context on initialization and existing files.