This README provides a step-by-step guide to running a Java-based application with a PostgreSQL database using Docker containers. Follow these steps to set up the database, the Java application, and verify everything works correctly.
- Docker installed and running on your system.
- Basic knowledge of using the command line.
- PostgreSQL installed and running (if testing without Docker).
Before starting your containers, check if the ports 5432 (PostgreSQL) and 9090 (Java app) are already in use.
Run the following commands:
# Check if PostgreSQL port 5432 is in use
sudo lsof -i :5432
# Check if Java application port 9090 is in use
sudo lsof -i :9090If the ports are in use by another process, you can kill the process using the following commands:
# Kill the process using the PostgreSQL port
sudo kill -9 <PID>
# Kill the process using the Java app port
sudo kill -9 <PID>Create a custom Docker network to allow communication between the Java application and PostgreSQL containers.
docker network create java-postgres-networkRun a PostgreSQL container and attach it to the java-postgres-network network. This will make the database accessible to the Java app.
docker run -d --name postgres-container --network java-postgres-network -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=hitcahi-db -p 5432:5432 postgresPOSTGRES_USER: The database username.POSTGRES_PASSWORD: The database password.POSTGRES_DB: The name of the database.
Verify that you can access the PostgreSQL database using the following command:
psql -h localhost -p 5432 -U postgres -d hitcahi-dbYou can interact with the database using basic SQL commands:
-
List all databases:
\l
-
List all tables in the connected database:
\dt
-
Query a specific table:
SELECT * FROM your_table;
Now, build the Docker image for your Java-based application.
docker build -t developers-base:latest .After building the image, you can tag it for use with a local Docker registry:
docker tag developers-base:latest localhost:5000/developers-base:latestPush the image to the local registry:
docker push localhost:5000/developers-base:latestVerify the image is successfully pushed by running:
curl http://localhost:5000/v2/_catalogIf you're testing without Docker containers and running the Java application directly on your machine, you need to export the following environment variables:
export DB_URL="jdbc:postgresql://localhost:5432/hitcahi-db"
export DB_USERNAME="postgres"
export DB_PASSWORD="postgres"
export SERVER_PORT="9090"These variables configure the connection between your Java app and the PostgreSQL database.
Now, run the Java application container and ensure it's connected to the java-postgres-network network and PostgreSQL container.
docker run -d --name java-app-container --network java-postgres-network -e DB_URL=jdbc:postgresql://postgres-container:5432/hitcahi-db -e DB_USERNAME=postgres -e DB_PASSWORD=postgres -p 9090:9090 java-developers:0.0.1This command does the following:
- Attaches the Java application container to the custom Docker network (
java-postgres-network). - Passes the necessary environment variables (
DB_URL,DB_USERNAME,DB_PASSWORD) to the container. - Exposes port
9090for the Java application.
Once the Java application container is running, open your browser and navigate to:
http://localhost:9090
This should display the response from the Java application if everything is configured correctly.
- Connection Refused Error: If your Java application cannot connect to PostgreSQL, verify that the PostgreSQL container is running and accessible. You may need to ensure the Java app is using the correct hostname (
postgres-container) and notlocalhost. - Port Binding Issues: If port conflicts occur, ensure that you are not running other services using the same ports (
5432and9090).
You have successfully set up a Java-based application with PostgreSQL using Docker containers. This guide covers all the necessary steps from setting up the database to running the application and troubleshooting any issues that may arise.