This is a Springboot application running on docker
- Contains a GET & POST APIs to create and read employees with first & last name fields
- POST API can be used to create employee with fields "firstName" & "lastName"
- GET API can be used to get all employees stored
- This application connects to PostgreSQL database that will be running as a containerized service on docker
- This application also builds and deploys pgadmin as a container on docker that will help us in browsing our postgres database
- pgAdmin is a commonly used database management tool in the PostgresDB community. It simplifies the creation, maintenance, and use of database objects by offering a clean and intuitive user interface.
- Java 8
- PostgreSQL, latest version as per https://hub.docker.com/_/postgres
- pgAdming, latest version as per https://hub.docker.com/r/dpage/pgadmin4/
- Springboot 2.4.5
- Download and install Docker Desktop
- Download and install IntelliJ IDEA Community (Free, open-source) here
- Clone the main branch of the project on to your working environment, clone url
- Open cloned project in Intellij
- Open docker-compose.yml file
- Execute all services by clicking on two green arrows at line 3 in docker-compose.yml file - aka
docker-compose up
- Verify if all the containers are up & running on you docker desktop, you see something like this
- If all containers are up & running, you can go ahead and create some data by calling the APIs below
- POST API - http://localhost:18080/createEmployee
- POST API Request Body
{ "firstName": "Docker", "lastName": "Desktop" }
- GET API - http://localhost:18080/getallemployees
- GET API Response body
{ "id" : "1", "firstName": "Docker", "lastName": "Desktop" }
- Rest APIs will be running on port 18080
- PostgreSQL database will be running on port 5432
- pgAdmin will be running on port 5050
pgAdmin is a commonly used database management tool in the PostgresDB community. It simplifies the creation, maintenance, and use of database objects by offering a clean and intuitive user interface.
- pgAdming takes time to boot up
- You monitor and confirm it's readiness by looking at the pgadming-container logs as below
- We have preloaded the servers on to pgAdmin by using environment variable PGADMIN_SERVER_JSON_FILE and supplied servers here
- We are using default password as "changeme" to login to postgres database
- You can login to pgadming at http://localhost:5050/browser/ and browse as below
- Username: postgres
- Password: changeme
We have two docker files in this application
- Dockerfile-restapi
- Dockerfile-pgadmin
This file is used to run the maven build by copying the src directory containing all the java code and pom.xml file The next section in this file loads JDK and copies the generated jar file as application.jar and then it is launched
This file is used to load pgadmin docker image and copy the postgres-servers.json on to container at /pgadmin4/servers.json
The docker compose file contains 3 services
- restapi
- pgadmin
- postgres
This service will invoke the Dockerfile-restapi and is dependent on postgres We are supplying enviornment variables required for loading postgres database
This service will invoke the Dockerfile-pgadmin and is dependent on postgres We are supplying enviornment variables required for preloading servers
This service will load postgre image and start the container running postgres database
- springboot-postgres-pgadmin-docker
- pgadmin4
- postgres
- https://docs.docker.com/develop/develop-images/multistage-build/
- https://stackoverflow.com/questions/27767264/how-to-dockerize-maven-project-and-how-many-ways-to-accomplish-it
- https://dzone.com/articles/a-dockerfile-for-maven-based-github-projects
- https://medium.com/@millet.david.julien/part-4-manage-database-with-pgadmin4-on-a-docker-image-95179b7f595f
- https://www.baeldung.com/dockerizing-spring-boot-application