- Or, in the Docker Desktop Dashboard, select the **Delete** icon next to your
container in the **Containers** view.
To create a network and run containers on it:
1. Run the following command to create a Docker network named my-network.
```console
$ docker network create my-network
```
2. Run your database container and specify the network using the `--network`
option. This runs the container on the my-network network.
```console
$ docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=mydb --network my-network -d mysql:latest
```
3. Run your other containers and specify the network using the `--network`
option. For this example, you'll run a phpMyAdmin container that can connect
to your database.
1. Run a phpMyAdmin container. Use the `--network` option to specify the
network, the `-p` option to let you access the container from your host
machine, and the `-e` option to specify a required environment variable
for this image.
```console
$ docker run --name my-phpmyadmin -d --network my-network -p 8080:80 -e PMA_HOST=my-mysql phpmyadmin
```
4. Verify that the containers can communicate. For this example, you'll access
phpMyAdmin and verify that it connects to the database.
1. Open [http://localhost:8080](http://localhost:8080) to access your phpMyAdmin container.
2. Log in using `root` as the username and `my-secret-pw` as the password.
You should connect to the MySQL server and see your database listed.
At this point, any application running on your `my-network` container network
can access the MySQL service in the container at `my-mysql:3306`.
## 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. 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 remove --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.
{{< tabs group="ui" >}}
{{< tab name="CLI" >}}
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:
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
```