How to Use Docker Compose For Springboot App
Background
I need to deploy my springboot application onto AWS cloud. My app is connected to a MySQL
container brought up by docker. I use Flyway to do database migration. In my own laptop,
my first step is to use docker to run a MySQL container. Then type mvn flyay:migrate
in
IDEA and run my application. Now all these three things must happen on cloud so I choose
to use docker compose to finsih this task.
How to do it step by step
-
look up office website for docker compose After readng through the page, I gained my very simple understanding of docker compose. Bascially, there are two steps; the first step is to prepare all the docker images which my whole project relies on, the second step is to write the
dokcer-compose.yml
to config docker compose. -
google some examples on docker compose with springboot I found two useful links {1, 2} saying the database config info of springboot app needs to be written in docker-compose.yml
-
google some examples on docker compose flyway Also I found two useful links {1, 2}. I just followed the code in the second link to see if it works and it does! That is a good news because I do not need to config healthcheck of MySQL and tell flyway to monitor it.
Final Result
My docker-compose.yml
looks like below,
version: '2.1'
services:
docker-mysql:
container_name: my-mysql
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=blog
ports:
- 3306:3306
app:
container_name: my-app
image: blog:latest
ports:
- 8080:8080
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/blog?allowPublicKeyRetrieval=true&useSSL=false
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: my-secret-pw
depends_on:
- docker-mysql
links:
- docker-mysql:docker-mysql
flyway:
container_name: my-flyway
environment:
- FLYWAY_USER=root
- FLYWAY_PASSWORD=my-secret-pw
- FLYWAY_URL=jdbc:mysql://docker-mysql:3306/blog?allowPublicKeyRetrieval=true&useSSL=false
- FLYWAY_TARGET=2
image: flyway/flyway:latest
command: -locations=filesystem:/flyway/sql -connectRetries=10 migrate
volumes:
- ./src/main/resources/db/migration/:/flyway/sql
depends_on:
- docker-mysql
I got this error Public Key Retrieval is not allowed
and the answer is
here.
The script is very simple to understand. If you have any questions, feel free to ask me.