0

I am trying to use JDBC from first container to connect to Postgresql database on secend container .the error happens while trying to make a database connection.

conn = DriverManager.getConnection(url, user, password);

I deploy war file into wildfly that is docker container. when I call the connect() method the error occur.
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.corsan</groupId>
    <artifactId>corsan</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.16</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

docker file:

FROM jboss/wildfly
ADD corsan.war /opt/jboss/wildfly/standalone/deployments/

the Java code for JDBC connection:


private final String url = "jdbc:postgresql://localhost:5432/postgres";
private final String user = "postgres";
private final String password = "1234";


    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (SQLException e) {
            System.out.println("problem while making connection");
            System.out.println(e.getMessage());
        }

        return conn;
    }

and while the wildfly is lunching there is always this warning that I guess is relevant to the connection refused Error:

Could not index class org/postgresql/jdbc/PgConnection$1.class at /content/corsan.war/WEB-INF/lib/postgresql-42.2.16.jar: java.lang.IllegalStateException: Required class information is missing

these two picture are intellij project structure and artifact that has postgres driver library in its lib.
Project structure
Project structure Artifact

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
22:51:20,921 ERROR [io.undertow.request] (default task-1) 
  UT005023: Exception handling request to /corsan/: java.lang.NullPointerException  

when I deploy the war directly to the same application server in local it works fine. But when I use docker container this issue occure. I appreciate for any hint or help.

Postgresql and application server are running in separate container in local machine. docker ps :

app server port: 0.0.0.0:8080->8080/tcp
PG: 0.0.0.0:5432->5432/tcp

  • try 'docker ps' make sure your port is being forwarded from your host machine to the docker container, you need to see something like 5432->5432 – ScottFree Oct 12 '20 at 23:26
  • In a docker container `localhost` is the container itself (not the host). See [this question](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) for info on referencing the host (If Postgres is also a container then you can [connect to it directly](https://docs.docker.com/network/)) – Brits Oct 12 '20 at 23:37
  • Please post formatted code rather than screenshots. Screenshots break search, copy and paste, and are less accessible. – BMitch Oct 13 '20 at 14:06
  • @ScottFree the docker ps command show this app server port: 0.0.0.0:8080->8080/tcp PG: 0.0.0.0:5432->5432/tcp that looks correct. – hadiroudsari Oct 13 '20 at 14:21

1 Answers1

3

The application server and Postgresql database are in two separate containers so they need to be connected together. This link help to make network between two separate container.Also Portainer can be used as GUI tools for connecting different containers together.
After these two containers have a same network we can check it with this command to ensure that the both containers are using the same network(corsannet is the name of network that I used).

docker network inspect corsannet

To ensure that the both containers are using the same network.

The other issue is for JDBC url we can not use localhost or domain name instead we should use the name of the database container, in this sample is pg.

private final String url = "jdbc:postgresql://pg:5432/postgres";