- 127.0.0.1:3000:3000
working_dir: /app
volumes:
- ./:/app
```
5. Finally, you need to migrate the environment variable definitions using the `environment` key.
```yaml
services:
app:
image: node:18-alpine
command: sh -c "yarn install && yarn 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
```
### Define the MySQL service
Now, it's time to define the MySQL service. The command that you used for that container was the following:
```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
```
1. First define the new service and name it `mysql` so it automatically gets the network alias. Also specify the image to use as well.
```yaml
services:
app:
# The app service definition
mysql:
image: mysql:8.0
```
2. 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.
```yaml
services:
app:
# The app service definition
mysql:
image: mysql:8.0
volumes:
- todo-mysql-data:/var/lib/mysql
volumes:
todo-mysql-data:
```
3. Finally, you need to specify the environment variables.
```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:
```yaml
services:
app:
image: node:18-alpine
command: sh -c "yarn install && yarn 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:
```
## Run the application stack
Now that you have your `compose.yaml` file, you can start your application.
1. Make sure no other copies of the containers are running first. Use `docker ps` to list the containers and `docker rm -f <ids>` to remove them.
2. Start up the application stack using the `docker compose up` command. Add the
`-d` flag to run everything in the background.
```console
$ docker compose up -d
```
When you run the previous command, you should see output like the following:
```plaintext
Creating network "app_default" with the default driver