2020-03-06 18:25:16 +00:00
## Compose sample application
2021-03-22 08:58:41 +00:00
### React application with a Spring backend and a MySQL database
2020-03-06 18:25:16 +00:00
Project structure:
```
.
├── backend
│ ├── Dockerfile
│ ...
├── db
│ └── password.txt
├── docker-compose.yaml
├── frontend
│ ├── ...
│ └── Dockerfile
└── README.md
```
[_docker-compose.yaml_ ](docker-compose.yaml )
```
services:
backend:
build: backend
...
db:
2020-03-17 15:28:17 +00:00
image: mysql:8.0.19
2020-03-06 18:25:16 +00:00
...
frontend:
build: frontend
ports:
2020-03-23 10:18:04 +00:00
- 3000:3000
2020-03-06 18:25:16 +00:00
...
```
The compose file defines an application with three services `frontend` , `backend` and `db` .
2020-03-24 14:03:44 +00:00
When deploying the application, docker-compose maps port 3000 of the frontend service container to port 3000 of the host as specified in the file.
Make sure port 3000 on the host is not already being in use.
2020-03-06 18:25:16 +00:00
## Deploy with docker-compose
```
$ docker-compose up -d
Creating network "react-java-mysql_default" with the default driver
Building backend
2020-03-17 15:28:17 +00:00
Step 1/17 : FROM maven:3.6.3-jdk-11 AS builder
2020-03-06 18:25:16 +00:00
...
Successfully tagged react-java-mysql_frontend:latest
WARNING: Image for service frontend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build` .
Creating react-java-mysql_frontend_1 ... done
Creating react-java-mysql_db_1 ... done
Creating react-java-mysql_backend_1 ... done
```
## Expected result
2020-03-09 13:10:22 +00:00
Listing containers must show three containers running and the port mapping as below:
2020-03-06 18:25:16 +00:00
```
$ docker ps
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a63dee74d79e react-java-mysql_backend "java -Djava.securit…" 39 seconds ago Up 37 seconds react-java-mysql_backend_1
2020-03-23 10:18:04 +00:00
6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:3000->3000/tcp react-java-mysql_frontend_1
2020-03-17 15:28:17 +00:00
b176b18fbec4 mysql:8.0.19 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1
2020-03-06 18:25:16 +00:00
```
2020-03-23 10:18:04 +00:00
After the application starts, navigate to `http://localhost:3000` in your web browser to get a colorful message.
2020-03-24 16:00:41 +00:00
![page ](./output.jpg )
2020-03-06 18:25:16 +00:00
Stop and remove the containers
```
$ docker-compose down
Stopping react-java-mysql_backend_1 ... done
Stopping react-java-mysql_frontend_1 ... done
Stopping react-java-mysql_db_1 ... done
Removing react-java-mysql_backend_1 ... done
Removing react-java-mysql_frontend_1 ... done
Removing react-java-mysql_db_1 ... done
Removing network react-java-mysql_default
```