2020-03-06 18:25:16 +00:00
## Compose sample application
### React application with a NodeJS backend and a MySQL database
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
2020-03-23 23:36:46 +00:00
ports:
- 80:80
- 9229:9229
- 9230:9230
2020-03-06 18:25:16 +00:00
...
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 23:36:46 +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-23 23:36:46 +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-express-mysql_default" with the default driver
Building backend
Step 1/16 : FROM node:10
---> aa6432763c11
...
Successfully tagged react-express-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-express-mysql_db_1 ... done
Creating react-express-mysql_backend_1 ... done
Creating react-express-mysql_frontend_1 ... done
```
## Expected result
2020-03-09 13:06:04 +00:00
Listing containers must show containers running and the port mapping as below:
2020-03-06 18:25:16 +00:00
```
$ docker ps
2020-03-23 23:36:46 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3e1183e709e react-express-mysql_frontend "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:3000->3000/tcp react-express-mysql_frontend_1
9422da53da76 react-express-mysql_backend "docker-entrypoint.s…" 8 minutes ago Up 8 minutes (healthy) 0.0.0.0:80->80/tcp, 0.0.0.0:9229-9230->9229-9230/tcp react-express-mysql_backend_1
a434bce6d2be mysql:8.0.19 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 3306/tcp, 33060/tcp react-express-mysql_db_1
2020-03-06 18:25:16 +00:00
```
2020-03-23 23:36:46 +00:00
After the application starts, navigate to `http://localhost:3000` in your web browser.
2020-03-24 16:00:41 +00:00
![page ](./output.png )
2020-03-23 23:36:46 +00:00
2020-03-06 18:25:16 +00:00
2020-03-23 23:36:46 +00:00
The backend service container has the port 80 mapped to 80 on the host.
2020-03-06 18:25:16 +00:00
```
2020-03-23 23:36:46 +00:00
$ curl localhost:80
2020-03-24 16:00:41 +00:00
{"message":"Hello from MySQL 8.0.19"}
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-express-mysql_frontend_1 ... done
Stopping react-express-mysql_backend_1 ... done
Stopping react-express-mysql_db_1 ... done
Removing react-express-mysql_frontend_1 ... done
Removing react-express-mysql_backend_1 ... done
Removing react-express-mysql_db_1 ... done
Removing network react-express-mysql_default
```
2022-07-13 08:15:53 +00:00
## Use with Docker Development Environments
You can use this sample with the Dev Environments feature of Docker Desktop.
![Screenshot of creating a Dev Environment in Docker Desktop ](../dev-envs.png )
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
```
2022-07-13 08:35:12 +00:00
https://github.com/docker/awesome-compose/tree/master/react-express-mysql
2022-07-13 08:15:53 +00:00
```