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
2022-05-10 09:59:25 +00:00
├── compose.yaml
2020-03-06 18:25:16 +00:00
├── frontend
│ ├── ...
│ └── Dockerfile
└── README.md
```
2022-05-10 09:59:25 +00:00
[_compose.yaml_ ](compose.yaml )
2020-03-06 18:25:16 +00:00
```
services:
backend:
build: backend
...
db:
2021-11-08 10:41:35 +00:00
# We use a mariadb image which supports both amd64 & arm64 architecture
image: mariadb:10.6.4-focal
# If you really want to use MySQL, uncomment the following line
#image: mysql:8.0.27
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` .
2022-05-10 09:59:25 +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.
2020-03-24 14:03:44 +00:00
Make sure port 3000 on the host is not already being in use.
2020-03-06 18:25:16 +00:00
2021-11-08 10:41:35 +00:00
> ℹ ️ **_INFO_**
> For compatibility purpose between `AMD64` and `ARM64` architecture, we use a MariaDB as database instead of MySQL.
> You still can use the MySQL image by uncommenting the following line in the Compose file
> `#image: mysql:8.0.27`
2022-05-10 09:59:25 +00:00
## Deploy with docker compose
2020-03-06 18:25:16 +00:00
```
2022-05-10 09:59:25 +00:00
$ docker compose up -d
2020-03-06 18:25:16 +00:00
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
```
2022-05-10 09:59:25 +00:00
$ docker compose down
2020-03-06 18:25:16 +00:00
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
```