```yaml
services:
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
MYSQL_DATABASE: mydb
ports:
- 3307:3306
volumes:
- my-db-volume:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: my-secret-pw
ports:
- 8080:80
depends_on:
- db
volumes:
my-db-volume:
```
For the database service:
- `db` is the name of the service.
- `image: mysql:latest` specifies that the service uses the latest MySQL
image from Docker Hub.
- `environment` lists the environment variables used by MySQL to
initialize the database, such as the root password and the database
name.
- `ports` maps port 3307 on the host to port 3306 in the container,
allowing you to connect to the database from your host machine.
- `volumes` mounts `my-db-volume` to `/var/lib/mysql` inside the container
to persist database data.
In addition to the database service, there is a phpMyAdmin service. By
default Compose sets up a single network for your app. Each container for
a service joins the default network and is both reachable by other
containers on that network, and discoverable by the service's name.
Therefore, in the `PMA_HOST` environment variable, you can specify the
service name, `db`, in order to connect to the database service. For more details about Compose, see the [Compose file reference](/reference/compose-file/).
2. Run Docker Compose.
1. Open a terminal and change directory to the directory where your
`compose.yaml` file is located.
2. Run Docker Compose using the following command.
```console
$ docker compose up
```
You can now access phpMyAdmin at
[http://localhost:8080](http://localhost:8080) and connect to your
database using `root` as the username and `my-secret-pw` as the password.
3. To stop the containers, press `ctrl`+`c` in the terminal.
Now, with Docker Compose you can start your database and app, mount volumes,
configure networking, and more, all with a single command.
## Summary
This guide introduced you to the essentials of using containerized databases,
specifically focusing on MySQL, to enhance flexibility, ease of setup, and
consistency across your development environments. The use-cases covered in
this guide not only streamline your development workflows but also prepare you
for more advanced database management and deployment scenarios, ensuring your
data-driven applications remain robust and scalable.
Related information:
- [Docker Hub database images](https://hub.docker.com/search?q=database&type=image)
- [Dockerfile reference](/reference/dockerfile/)
- [Compose file reference](/reference/compose-file/)
- [CLI reference](/reference/cli/docker/)
- [Database samples](../../reference/samples/_index.md#databases)