2020-03-06 18:25:16 +00:00
## Compose sample application
### Java Spark application with 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
└── 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
ports:
2020-03-17 14:30:49 +00:00
- 8080:8080
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
...
```
The compose file defines an application with two services `backend` and `db` .
2022-05-10 09:59:25 +00:00
When deploying the application, docker compose maps port 8080 of the backend service container to port 80 of the host as specified in the file.
2020-03-17 14:30:49 +00:00
Make sure port 8080 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 "sparkjava-mysql_default" with the default driver
Building backend
...
Successfully tagged sparkjava-mysql_backend:latest
WARNING: Image for service backend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build` .
Creating sparkjava-mysql_db_1 ... done
Creating sparkjava-mysql_backend_1 ... done
```
## Expected result
Listing containers must show two containers running and the port mapping as below:
```
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2020-03-17 14:30:49 +00:00
ee1e4f05d9f6 sparkjava-mysql_backend "/bin/sh -c 'java -j…" 44 seconds ago Up 43 seconds 0.0.0.0:8080->8080/tcp sparkjava-mysql_backend_1
716025ddf65b mysql:8.0.19 "docker-entrypoint.s…" 44 seconds ago Up 43 seconds 3306/tcp, 33060/tcp sparkjava-mysql_db_1
2020-03-06 18:25:16 +00:00
```
After the application starts, run:
```
2020-03-17 14:30:49 +00:00
$ curl localhost:8080
2020-03-06 18:25:16 +00:00
["Blog post #0 ","Blog post #1 ","Blog post #2 ","Blog post #3 ","Blog post #4 "]
```
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 sparkjava-mysql_backend_1 ... done
Stopping sparkjava-mysql_db_1 ... done
Removing sparkjava-mysql_backend_1 ... done
Removing sparkjava-mysql_db_1 ... done
Removing network sparkjava-mysql_default
```