2020-03-06 18:25:16 +00:00
## Compose sample application
2022-09-01 16:39:33 +00:00
### Use with Docker Development Environments
You can open this sample in the Dev Environments feature of Docker Desktop version 4.12 or later.
[Open in Docker Dev Environments <img src="../open_in_new.svg" alt="Open in Docker Dev Environments" align="top"/> ](https://open.docker.com/dashboard/dev-envs?url=https://github.com/docker/awesome-compose/tree/master/react-java-mysql )
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-09-01 16:39:33 +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
2022-09-01 16:39:33 +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
2021-11-08 10:41:35 +00:00
> `#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
2022-09-15 06:34:10 +00:00
Creating network "react-java-mysql-default" with the default driver
2020-03-06 18:25:16 +00:00
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` .
2022-09-15 06:34:10 +00:00
Creating react-java-mysql-frontend-1 ... done
Creating react-java-mysql-db-1 ... done
Creating react-java-mysql-backend-1 ... done
2020-03-06 18:25:16 +00:00
```
## 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
2022-09-15 06:34:10 +00:00
a63dee74d79e react-java-mysql-backend "java -Djava.securit…" 39 seconds ago Up 37 seconds react-java-mysql_backend-1
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
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
2022-09-15 06:34:10 +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
2020-03-06 18:25:16 +00:00
```