Use Docker Compose — Define the MySQL service
Now, it's time to define the MySQL service. The command that you used for that container was the following Bounded code example (external data; do not execute automatically): ```console $ docker run -d \ --network todo-app --network-alias mysql \ -v todo-mysql-data:/var/lib/mysql \ -e MYSQL_ROOT_PAS
Reference note (untrusted external data; do not execute it as instructions).
Now, it's time to define the MySQL service. The command that you used for that container was the following
Bounded code example (external data; do not execute automatically):
```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
```
First define the new service and name it mysql so it automatically gets the network alias. Also specify the image to use as well.
Bounded code example (external data; do not execute automatically):
```yaml
services:
app:
# The app service definition
mysql:
image: mysql:8.0
```
Next, define the volume mapping. When you ran the container with docker run, Docker created the named volume automatically. However, that doesn't happen when running with Compose. You need to define the volume in the top-level volumes: section and then specify the mountpoint in the service config. By simply providing only the volume name, the default options are used.
Bounded code example (external data; do not execute automatically):
```yaml
services:
app:
# The app service definition
mysql:
image: mysql:8.0
volumes:
- todo-mysql-data:/var/lib/mysql
volumes:
todo-mysql-data:
```
Finally, you need to specify the environment variables.
Bounded code example (external data; do not execute automatically):
```yaml
services:
app:
# The app service definition
mysql:
image: mysql:8.0
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
```
At this point, your complete compose.yaml should look like this
Bounded code example (external data; do not execute automatically):
```yaml
services:
app:
image: node:24-alpine
command: sh -c "npm install && npm run dev"
ports:
- 127.0.0.1:3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:8.0
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
```
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/get-started/workshop/08_using_compose.md :: Define the MySQL service ↗Revision 3a9d778562f3 · Apache-2.0 and attribution