What happens to data in a MySQL container if you don't use a volume?

IntermediateConceptMySQL
Answer

Short answer

The data is written to the container's writable layer, so it's lost when the container is removed (docker rm, recreated by Compose, rescheduled pod). Mounting a volume at /var/lib/mysql keeps the data outside the container's lifecycle.

Example

docker run -d --name db -e MYSQL_ROOT_PASSWORD=x mysql:8.4      # ❌ no volume
docker rm -f db && docker run -d --name db -e MYSQL_ROOT_PASSWORD=x mysql:8.4
# → empty database again

docker run -d --name db -e MYSQL_ROOT_PASSWORD=x -v dbdata:/var/lib/mysql mysql:8.4   # ✅

Related surprise

The MYSQL_ROOT_PASSWORD / MYSQL_DATABASE env vars only take effect when the data directory is empty (first start). Changing them later on an existing volume does nothing — a common "why doesn't my new password work?" confusion.

In Kubernetes

Use a PVC (StatefulSet volumeClaimTemplates). A pod using emptyDir loses data when the pod is deleted.

What to say in the interview

"Without a volume, data lives in the container's writable layer and disappears with the container. We mount a named volume or PVC at /var/lib/mysql. Also, the init env vars only apply on first start with an empty data directory."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL