data persists:
1. Run the container and attach the volume.
```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.
2. Create some data in the database. Use the `docker exec` command to run
`mysql` inside the container and create a table.
```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`.
3. Stop and remove the container. Without a volume, the table you created would
be lost when removing the container.
```console
$ docker remove --force my-mysql
```
4. Start a new container with the volume attached. This time, you don't need to
specify any environment variables as the configuration is saved in the
volume.
```console
$ docker run --name my-mysql -v my-db-volume:/var/lib/mysql -d mysql:latest
```
5. Verify that the table you created still exists. Use the `docker exec` command
again to run `mysql` inside the container.
```console
$ docker exec my-mysql mysql -u root -pmy-secret-pw -e "SELECT * FROM mydb.mytable;"
```
This command uses the `mysql` tool in the container to select all the
records from the `mytable` table.
You should see output like the following.
```console
column_name
value
```
{{< /tab >}}
{{< tab name="GUI" >}}
To run a database container with a volume attached, and then verify that the
data persists:
1. Run a container with a volume attached.
1. In the Docker Desktop Dashboard, select the global search at the top of the window.
2. Specify `mysql` in the search box, and select the **Images** tab if not
already selected.
3. Hover over the **mysql** image and select **Run**.
The **Run a new container** modal appears.
4. Expand **Optional settings**.
5. In the optional settings, specify the following:
- **Container name**: `my-mysql`
- **Environment variables**:
- `MYSQL_ROOT_PASSWORD`:`my-secret-pw`
- `MYSQL_DATABASE`:`mydb`
- **Volumes**:
- `my-db-volume`:`/var/lib/mysql`