Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Java-Based Application with PostgreSQL Database Setup

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.


Prerequisites

  • Docker installed and running on your system.
  • Basic knowledge of using the command line.
  • PostgreSQL installed and running (if testing without Docker).

Steps

1. Check if the Ports are in Use

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 :9090

If 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>

2. Create a Custom Docker Network

Create a custom Docker network to allow communication between the Java application and PostgreSQL containers.

docker network create java-postgres-network

3. Run PostgreSQL Container

Run 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   postgres
  • POSTGRES_USER: The database username.
  • POSTGRES_PASSWORD: The database password.
  • POSTGRES_DB: The name of the database.

4. Access PostgreSQL Database

Verify that you can access the PostgreSQL database using the following command:

psql -h localhost -p 5432 -U postgres -d hitcahi-db

You 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;

5. Build Docker Image for the Java Application

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:latest

Push the image to the local registry:

docker push localhost:5000/developers-base:latest

Verify the image is successfully pushed by running:

curl http://localhost:5000/v2/_catalog

6. Set Up Environment Variables (If Not Using Docker)

If 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.


7. Run the Java Application Container

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.1

This 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 9090 for the Java application.

8. Verify the 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.


Troubleshooting

  • 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 not localhost.
  • Port Binding Issues: If port conflicts occur, ensure that you are not running other services using the same ports (5432 and 9090).

Conclusion

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages