-e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todos `
mysql:8.0
```
{{< /tab >}}
{{< tab name="Command Prompt" >}}
```console
$ docker run -d ^
--network todo-app --network-alias mysql ^
-v todo-mysql-data:/var/lib/mysql ^
-e MYSQL_ROOT_PASSWORD=secret ^
-e MYSQL_DATABASE=todos ^
mysql:8.0
```
{{< /tab >}}
{{< /tabs >}}
In the previous command, you can see the `--network-alias` flag. In a later section, you'll learn more about this flag.
> [!TIP]
>
> You'll notice a volume named `todo-mysql-data` in the above command that is mounted at `/var/lib/mysql`, which is where MySQL stores its data. However, you never ran a `docker volume create` command. Docker recognizes you want to use a named volume and creates one automatically for you.
3. To confirm you have the database up and running, connect to the database and verify that it connects.
```console
$ docker exec -it <mysql-container-id> mysql -u root -p
```
When the password prompt comes up, type in `secret`. In the MySQL shell, list the databases and verify
you see the `todos` database.
```console
mysql> SHOW DATABASES;
```
You should see output that looks like this:
```plaintext
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| todos |
+--------------------+
5 rows in set (0.00 sec)
```
4. Exit the MySQL shell to return to the shell on your machine.
```console
mysql> exit
```
You now have a `todos` database and it's ready for you to use.
## Connect to MySQL
Now that you know MySQL is up and running, you can use it. But, how do you use it? If you run
another container on the same network, how do you find the container? Remember that each container has its own IP address.
To answer the questions above and better understand container networking, you're going to make use of the [nicolaka/netshoot](https://github.com/nicolaka/netshoot) container,
which ships with a lot of tools that are useful for troubleshooting or debugging networking issues.
1. Start a new container using the nicolaka/netshoot image. Make sure to connect it to the same network.
```console
$ docker run -it --network todo-app nicolaka/netshoot
```
2. Inside the container, you're going to use the `dig` command, which is a useful DNS tool. You're going to look up
the IP address for the hostname `mysql`.
```console
$ dig mysql
```
You should get output like the following.
```text
; <<>> DiG 9.18.8 <<>> mysql
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32162
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;mysql. IN A
;; ANSWER SECTION:
mysql. 600 IN A 172.23.0.2
;; Query time: 0 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Tue Oct 01 23:47:24 UTC 2019
;; MSG SIZE rcvd: 44
```
In the "ANSWER SECTION", you will see an `A` record for `mysql` that resolves to `172.23.0.2`
(your IP address will most likely have a different value). While `mysql` isn't normally a valid hostname,
Docker was able to resolve it to the IP address of the container that had that network alias. Remember, you used the
`--network-alias` earlier.
What this means is that your app only simply needs to connect to a host named `mysql` and it'll talk to the
database.
## Run your app with MySQL
The todo app supports the setting of a few environment variables to specify MySQL connection settings. They are:
- `MYSQL_HOST` - the hostname for the running MySQL server
- `MYSQL_USER` - the username to use for the connection
- `MYSQL_PASSWORD` - the password to use for the connection
- `MYSQL_DB` - the database to use once connected
> [!NOTE]
>
> While using env vars to set connection settings is generally accepted for development, it's highly discouraged