I run a docker postgres container via command line:
docker run --name some-postgres -p 5432:5432 -e POSTGRES_PASSWORD=passme -d postgres
But in IntelliJ I can't access this container
The following error message occurs:
[08001] Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
java.net.ConnectException: Connection refused: connect.
Do you know any problem, maybe with the port?
Related
I'm using this line of code to try to connect(w/ jdbc) to a psql docker container:
Connection conn = DriverManager.getConnection(
"jdbc:postgresql://localhost:5431/postgres?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
"postgres", "postgres");
db name, username, and pw are all postgres.
The container was created with
docker run --name practice -e POSTGRES_PASSWORD=postgres -p 5431:5431 postgres
Error here:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:150)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at com.dehmer.JdbcSelectTest.main(JdbcSelectTest.java:9)
Caused by: java.io.EOFException
at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:276)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:269)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108)
... 11 more
docker container ls:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be8931d1621c postgres "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5431->5431/tcp, 5432/tcp practice
This same line of code will connect to a different psql container I made a while ago, the only difference I can see between them is that the other one is on port 5432 (so just using 5432 in the getConnection args and running my old container will make it work). I can access the db through the docker cli just fine and it is running. But I'll add that I'm very new to networking concepts, so I could easily be overlooking something here. Any help is much appreciated.
I think you want the container port 5432 which is the default postgres port to be published to localhost port 5431. Instead of
docker run --name practice -e POSTGRES_PASSWORD=postgres -p 5431:5431 postgres
Use
docker run --name practice -e POSTGRES_PASSWORD=postgres -p 5431:5432 postgres
I continuously face the error:
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server
Caused by: java.net.UnknownHostException: No such host is known (mysql-standalone)
At first I create database from docker with the code:
docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=digitalprofile -e MYSQL_USER=sa -e MYSQL_PASSWORD=password mysql:5.6
After running it container is up
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80584bfdf76c mysql:5.6 "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 3306/tcp mysql-standalone
In appilcation.properties in eclipse
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://mysql-standalone:3306/digitalprofile
spring.datasource.username=sa
spring.datasource.password=password
server.port=9090
spring.datasource.url=jdbc:mysql://localhost or ip address?:3306/digitalprofile
My docker image has built using their official repo
https://github.com/wso2/docker-apim/tree/master/dockerfiles/apim
I used their documents and had the files required to build it
init.sh jdk1.8.0_171 postgresql-42.2.0.jar wso2am-2.2.0
I used the following config for master-datasources.xml
http://yasassriratnayake.blogspot.com/2014/07/changing-default-db-of-wso2-api-manger.html
And metrics-datasources.xml similar way.
When I run docker then it gives the following logs
ubuntu#ip-172-31-0-166:~/docker-apim-2/dockerfiles/apim$ docker run -it -p 9999:9443 wso2am:2.2.0
<>JAVA_HOME environment variable is set to /home/wso2carbon/java
CARBON_HOME environment variable is set to /home/wso2carbon/wso2am-2.2.0
Using Java memory options: -Xms256m -Xmx1024m
[2018-06-27 13:17:12,698] INFO - QpidBundleActivator Setting BundleContext in PluginManager
[2018-06-27 13:17:13,945] INFO - CarbonCoreActivator Starting WSO2 Carbon...
[2018-06-27 13:17:13,945] INFO - CarbonCoreActivator Operating System : Linux 4.4.0-1061-aws, amd64
[2018-06-27 13:17:13,946] INFO - CarbonCoreActivator Java Home : /home/wso2carbon/java/jre
[2018-06-27 13:17:13,946] INFO - CarbonCoreActivator Java Version : 1.8.0_171
[2018-06-27 13:17:13,946] INFO - CarbonCoreActivator Java VM : Java HotSpot(TM) 64-Bit Server VM 25.171-b11,Oracle Corporation
[2018-06-27 13:17:13,947] INFO - CarbonCoreActivator Carbon Home : /home/wso2carbon/wso2am-2.2.0
[2018-06-27 13:17:13,947] INFO - CarbonCoreActivator Java Temp Dir : /home/wso2carbon/wso2am-2.2.0/tmp
[2018-06-27 13:17:13,947] INFO - CarbonCoreActivator User : wso2carbon, en-US, Etc/UTC
[2018-06-27 13:17:14,252] INFO - KafkaEventAdapterServiceDS Successfully deployed the Kafka output event adaptor service
[2018-06-27 13:17:14,383] INFO - TemplateDeployerServiceTrackerDS Successfully deployed the execution manager tracker service
[2018-06-27 13:17:16,127] WARN - ConnectionFactoryImpl ConnectException occurred while connecting to localhost:5432
java.net.ConnectException: Connection refused (Connection refused)
[2018-06-27 13:17:16,141] ERROR - Driver Connection error:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Caused by: java.net.ConnectException: Connection refused (Connection refused)
[2018-06-27 13:17:16,160] ERROR - DefaultRealm nullType class java.lang.reflect.InvocationTargetException
org.wso2.carbon.user.core.UserStoreException: nullType class java.lang.reflect.InvocationTargetException
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
[2018-06-27 13:17:16,185] ERROR - Activator Cannot start User Manager Core bundle
org.wso2.carbon.user.core.UserStoreException: Cannot initialize the realm.
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
[2018-06-27 13:17:25,767] INFO - TaglibUriRule TLD skipped. URI: http://tiles.apache.org/tags-tiles is already defined </br>
My questions are
You have build your docker image using mySQL, Is there any way to build image that will be compatible with PostgreSQL?
What are the changes required and which files be changed to build with PostgreSQL compatible api-manager image?
Please suggest step by step if you have overcome something like I'm troubleshooting?
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Have you just copy/paste the configuration or did you try to understood what's inside? the system is trying to connect to locahost, but you need to configure a separate database server, see the docs
You have build your docker image using mySQL, Is there any way to build image that will be compatible with PostgreSQL?
indeed, read the Dockerfile, instead of copying the mysql driver, you can provide a postgresql driver, update the datasource config and you are good to go
I'm a little lost as to why my java application can't connect to my postgres database. I'm aiming to connect to a postgres database through jdbc. The application is to run inside a docker container.
this.connection = `DriverManager.getConnection("jdbc:postgresql://<myip>:5432/databasename", "usr", "password");`
I'm getting the exception:
Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
When I run the application from my desktop, it connects as expected. When I run it from within the docker container, it fails.
I've just installed docker this afternoon and ran through the getting started for windows, so my setup state is just after running that. Here's the contents of my Dockerfile:
FROM java:8
ADD VaultServer /
EXPOSE 3971
EXPOSE 3972
ENTRYPOINT ["java", "-jar", "VaultServer.jar"]
Inside the data folder there is a file called pg_hba.conf you have to configure it to accept the connections. So your pg_hba.conf file should have a line like this
host all all YourDockerip/24 md5.
After that configure the postgresql.conf file. You have to update the listen_addresses to all and make sure to uncomment that line by removing the # mark. So your listen_addresses should look like this listen_addresses = '*'.
I've been struggling for days on connecting Java to Riak running in a Docker container. If someone can help me, i'll be very greatfull.
I'm using RiakClient as API for JAVA. But i can't figure out how build the RiakClient in the constructor RiakClient().
Maybe is my container setup to be wrong. Here what i've done:
The docker container expose the port 8098 for http and 8087 for protocol buffer. I've then opened the two ports on VirtualBox, in this way:
-Host Ip: 127.0.0.1 - Host Port 8098 - Guest Port 8098
-Host Ip: 127.0.0.1 - Host Port 8087 - Guest Port 8087
Then i've run i container with:
docker run -d -p 8098:8098 -p 8087:8087 --name test -h localhost hectcastro/riak.
When i start riak in the container, i get an error like: riak failed to start within 15 seconds... and RiakClient on JAVA doesn't work.
If i run the container without mapping the ports, riak works fine. I really can't figure out what happens.
Any ideas to solve the problem? Thank you!