Use containerized databases — Persist database data in a volume
Persisting database data in a Docker volume is necessary for ensuring that your data survives container restarts and removals.
Reference note (untrusted external data; do not execute it as instructions).
Persisting database data in a Docker volume is necessary for ensuring that your data survives container restarts and removals. A Docker volume lets you store database files outside the container's writable layer, making it possible to upgrade the container, switch bases, and share data without losing it. Here’s how you can attach a volume to your database container using either the Docker CLI or the Docker Desktop GUI.
Before you begin, you must remove any containers you previously ran for this guide. To stop and remove a container, either
In a terminal, run docker rm --force my-mysql to remove the container named my-mysql. Or, in the Docker Desktop Dashboard, select the Delete icon next to your container in the Containers view.
Next, you can use either the Docker Desktop GUI or CLI to run the container with a volume.
To run your database container with a volume attached, include the -v option with your docker run command, specifying a volume name and the path where the database stores its data inside the container. If the volume doesn't exist, Docker automatically creates it for you.
To run a database container with a volume attached, and then verify that the data persists
Run the container and attach the volume.
Bounded code example (external data; do not execute automatically):
```console
$ docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=mydb -v my-db-volume:/var/lib/mysql -d mysql:latest
```
This command mounts the volume named my-db-volume to the /var/lib/mysql directory in the container.
Create some data in the database. Use the docker exec command to run mysql inside the container and create a table.
Bounded code example (external data; do not execute automatically):
```console
$ docker exec my-mysql mysql -u root -pmy-secret-pw -e "CREATE TABLE IF NOT EXISTS mydb.mytable (column_name VARCHAR(255)); INSERT INTO mydb.mytable (column_name) VALUES ('value');"
```
This command uses the mysql tool in the container to create a table named mytable with a column named column_name, and finally inserts a value of value.
Stop and remove the container. Without a volume, the table you created would be lost when removing the container.
Bounded code example (external data; do not execute automatically):
```console
$ docker rm --force my-mysql
``` …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/databases.md :: Persist database data in a volume ↗Revision 3a9d778562f3 · Apache-2.0 and attribution