keepcalculator

06 Numbers and Powers

Why WordPress and MySQL Docker Containers Keep Stopping

Card No. 06·36472 · Updated · Standard formula, shown below

WordPress and MySQL containers usually stop because a process inside them exits, the database is not ready or configured correctly, or Docker or the host runs out of resources. Check each container’s exit status and logs first; then correct the specific configuration or startup issue rather than repeatedly restarting the stack.

How container stopping works

A Docker container runs while its main process is running. When that process exits—because of an error, a completed task, or an intentional stop—the container stops too. Docker does not automatically keep every stopped container alive. A restart policy can start it again, but it cannot fix the underlying cause.

In a typical WordPress setup, one container runs the web application and another runs MySQL or a compatible database. WordPress needs the database service to accept connections and needs matching connection settings. These containers are separate processes, so the status of one does not automatically explain the status of the other.

Also distinguish a stopped container from a merely unhealthy or unavailable application. A container may still be running while WordPress returns an error, the web server is not responding, or a health check reports failure. Use Docker status and logs to identify which situation you have.

1. Inspect status and logs before changing settings

Run these commands from the directory containing your Compose file:

  • docker compose ps -a shows the containers, their current state, and—when available—the exit code.
  • docker compose logs --tail=100 wordpress and docker compose logs --tail=100 db show recent output. Replace the service names with those in your file.
  • docker inspect CONTAINER_NAME can show details such as the exit code, error, and configured restart policy.
  • docker events can help reveal when containers start, stop, or restart while you reproduce the issue.

Look for the first relevant error, not just the last line. Database initialization messages may appear before a later failure. If Compose reports that a service is restarting, collect its logs before repeatedly restarting it; a rapid restart loop can obscure what happened.

2. Check MySQL configuration and data

A database container may exit because required environment variables are missing or invalid, the database cannot initialize its data directory, or MySQL encounters an error reading its files. The exact requirements depend on the image and configuration. Read the logs and verify the image’s documented environment variables rather than assuming every MySQL image uses identical settings.

Check that WordPress and the database use the same database name, username, password, and network service name. In Compose, WordPress should generally connect to the database by its service name, such as db, and the database’s internal listening port—not by localhost. Inside the WordPress container, localhost refers to that WordPress container itself.

Review volume mounts and filesystem permissions as well. A bind mount can expose a host directory with permissions or contents the database process cannot use. A named volume may contain old database files from a previous configuration. Do not delete or recreate a database volume as a troubleshooting shortcut: that can permanently remove stored data. Back up data and confirm which volume holds it before making changes.

3. Make startup wait for database readiness

Compose can start services in dependency order, but starting the database container does not necessarily mean that the database is ready to accept connections. WordPress may attempt a connection too early. Depending on its configuration and image, it may display a database error or its process may exit.

Use a database health check and configure the WordPress service to wait for the database to become healthy where the Compose implementation supports that condition. Alternatively, use an application or entrypoint strategy that retries the connection. Confirm readiness from the database logs or by checking that a connection succeeds; a fixed delay alone is less reliable because startup time varies.

For example, the relevant Compose concepts are a database healthcheck and a WordPress dependency with a healthy condition. Check the documentation for your Compose version and image before copying a health-check command: available tools and authentication requirements differ.

4. Verify Compose settings and networking

Validate the Compose file and inspect the resolved configuration before recreating containers. The command docker compose config can reveal syntax problems and show the configuration Compose will use. Take care not to publish its output where it could expose passwords or other secrets.

  • Confirm both services are attached to a common Docker network.
  • Use the database service name as the WordPress database host.
  • Check that environment-variable names are spelled correctly and that referenced .env values are present.
  • Keep secrets out of public repositories and avoid posting unredacted logs or configuration.
  • Check that host ports are not already in use if the web service fails to start; a port conflict often stops the web container, not the database.

After correcting the file, apply the changes with docker compose up -d, then check status and logs again. Avoid changing several unrelated settings at once; doing so makes it harder to identify which change fixed the issue.

5. Check resources, restart policies, and updates

Containers can be stopped by an operator, a deployment tool, a Docker daemon restart, or host-level pressure. Check Docker Desktop or host logs for memory, disk-space, or daemon problems. A full disk can prevent database writes or initialization; insufficient memory can cause the operating system to terminate a process. Docker’s container status may not explain every host-level event, so consult system logs when application logs end abruptly.

A restart policy such as unless-stopped can bring a container back after a failure or daemon restart, subject to Docker’s behavior and any manual stop. It is useful for recovery, not diagnosis. If the process repeatedly exits, the policy may create a restart loop. Fix the root cause, then choose a policy that suits the service.

Image updates can also change behavior or expose incompatibilities. Record which image tags and configuration were in use before updating. Avoid relying on an unpinned floating tag for reproducible deployments, and follow the image’s upgrade guidance—especially for database versions and persistent data.

Frequently Asked Questions

Why does MySQL keep restarting in Docker?
Common causes include invalid initialization settings, an unusable or inaccessible data directory, insufficient resources, or a database process error. The MySQL container logs and exit details are the best starting points.

Why does WordPress say it cannot connect to the database?
Check that the database is running and ready, that both services share a network, and that the host, database name, username, and password match. In Compose, use the database service name rather than localhost.

Will a restart policy fix containers that keep stopping?
No. It can restart a container after it exits, but it does not repair bad settings, missing files, failed database initialization, or resource problems.

Can I remove the MySQL container to fix the problem?
Removing a container does not necessarily remove a separate named volume, but commands that remove volumes can erase database data. Identify and back up the data volume before deleting or recreating anything.

Why do both containers stop at once?
A Compose command, Docker daemon shutdown, host restart, resource issue, or shared configuration change may affect both. Compare timestamps, Compose output, Docker events, and host logs to find a common cause.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →