Example how to combine SpringBoot with Flyway and Postgres DB using Docker.
- Develop a simple Spring Boot application with database
- Easy manage database schema using Flyway
- Simple usage of Postgres database run as a Docker container
- Use Docker-Compose to bind Spring Boot application with Postgres database that can be deploy on production
- Run Postgres database as Docker container:
- Create and start container:
docker run -d --name sb_flyway_postgres_docker_db --env 'DB_USER=postgres' --env 'DB_PASS=postgres' --env 'DB_NAME=sb-flyway-postgres-docker-db' --env 'PG_TRUST_LOCALNET=true' -p 5433:5432 sameersbn/postgresql:9.6-1
- Stop running container:
docker stop sb_flyway_postgres_docker_db
- Start container:
docker start sb_flyway_postgres_docker_db
- Remove container:
docker rm sb_flyway_postgres_docker_db
- Create and start container:
- Go to location of
pom.xml
and run Spring Boot application:mvn spring-boot:run
- Test API:
curl http://localhost:8081/api/vehicles
- Go to
docker
location - Run Bash script to build Docker-Compose images:
./build-docker.sh
- Go to
docker/app
location (wheredocker-compose.yml
is located) - Run Docker-Compose command to create and start application and database:
docker-compose up -d
- When application starts, test API:
curl http://localhost:8080/api/vehicles
- Run Docker-Compose command to stop and destory application and database:
docker-compose down
docker-compose up
- create and start Docker containersdocker-compose up -d
- create and start Docker cotainers in the backgrounddocker-compose down
- stop and destroy Docker cotainersdocker-compose start
- start Docker containersdocker-compose stop
- stop Docker containersdocker-compose logs -f
- tailing logs of Docker cotainersdocker-compose ps
- check status of Docker cotainers
For PostgreSQL to preserve its state across container destroy and create, Docker mount a volume located in docker/storage
(defined in a Dockerfile)