This project is a Spring Boot application that uses PostgreSQL as its database. The application and database are containerized using Docker and managed via Docker Compose. The containers share the same network and use a volume for persistent database storage.
- Spring Boot backend
- PostgreSQL database
- Docker Compose for easy setup
- Shared network and volume management
.dockerignorefile to optimize builds
Ensure you have the following installed:
git clone https://github.com/kyawyea92/springboot_postgres_docker.git
cd springboot_postgres_dockerCreate a .env file in the root directory with the following variables:
POSTGRES_USER=root
POSTGRES_PASSWORD=password
POSTGRES_DB=dockerVersion
SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/dockerVersion
SPRING_DATASOURCE_USERNAME=root
SPRING_DATASOURCE_PASSWORD=passwordmvn clean packagedocker-compose up --build -ddocker psThe docker-compose.yml file defines two services:
app: The Spring Boot applicationdb: The PostgreSQL database
Example docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:latest
container_name: postgres_container
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
ports:
- "5432:5432"
networks:
- backend
volumes:
- ./postgres_data:/var/lib/postgresql/data
app:
build: .
container_name: springboot_app
restart: always
depends_on:
- db
environment:
SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL}
SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME}
SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD}
ports:
- "8080:8080"
networks:
- backend
networks:
backend:
driver: bridge
volumes:
postgres_data:
driver: localTo optimize the build, a .dockerignore file is included:
target/
*.jar
*.war
*.class
*.log
.idea/
*.iml
.DS_Store
node_modules/
.env
To stop and remove the containers, run:
docker-compose down -vYou can connect to the PostgreSQL database using:
docker exec -it postgres_container psql -U admin -d mydatabaseView logs for the application:
docker logs -f springboot_appView logs for the database:
docker logs -f postgres_containerThis setup provides a robust way to develop and run a Spring Boot application with PostgreSQL using Docker. Happy coding! 🚀
This project is licensed under the GNU GENERAL PUBLIC LICENSE V.3 - see the LICENSE file for details.