Docker Postgresql connection refused
up vote
-1
down vote
favorite
I have been trying to dockerize my web-app and everything has gone (mostly) well so far! Except now I am trying to wire in (sorry if the verbage is incorrect) in postgreql as the application connects to a psql DB for logging in as well as storing things.
I read in a post here on SO that I need to just map the db to my local machine by adding ports: -"5432:5432"
to docker-compose.yml. (here is the referenced question Docker Compose + Postgres: Expose port)
However now I am getting a connection refused error whenever I try to do any action that requires db access.
Here is docker-compose.yml
version: '3.1'
services:
drools-average-docker-app:
image: drools-average-docker-image
build:
context: ./
dockerfile: Dockerfile
ports:
- 8080:8080
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: 'droolsTestDB'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
ports:
- "5432:5432"
I also read somewhere that it might be iptables
blocking the connection? So just in case, here is iptables.rules
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [8:411]
:TCP - [0:0]
:UDP - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
COMMIT
postgresql docker docker-compose iptables
add a comment |
up vote
-1
down vote
favorite
I have been trying to dockerize my web-app and everything has gone (mostly) well so far! Except now I am trying to wire in (sorry if the verbage is incorrect) in postgreql as the application connects to a psql DB for logging in as well as storing things.
I read in a post here on SO that I need to just map the db to my local machine by adding ports: -"5432:5432"
to docker-compose.yml. (here is the referenced question Docker Compose + Postgres: Expose port)
However now I am getting a connection refused error whenever I try to do any action that requires db access.
Here is docker-compose.yml
version: '3.1'
services:
drools-average-docker-app:
image: drools-average-docker-image
build:
context: ./
dockerfile: Dockerfile
ports:
- 8080:8080
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: 'droolsTestDB'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
ports:
- "5432:5432"
I also read somewhere that it might be iptables
blocking the connection? So just in case, here is iptables.rules
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [8:411]
:TCP - [0:0]
:UDP - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
COMMIT
postgresql docker docker-compose iptables
What is the DB connection used in the app? Normally there is no need to expose port 5432 to the host. Instead connect to port 5432 on the hostdb
instead.
– Ralf Stubner
Nov 19 at 18:47
I use jdbc to connectspring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
with postgres as the user and password
– j-money
Nov 19 at 19:11
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have been trying to dockerize my web-app and everything has gone (mostly) well so far! Except now I am trying to wire in (sorry if the verbage is incorrect) in postgreql as the application connects to a psql DB for logging in as well as storing things.
I read in a post here on SO that I need to just map the db to my local machine by adding ports: -"5432:5432"
to docker-compose.yml. (here is the referenced question Docker Compose + Postgres: Expose port)
However now I am getting a connection refused error whenever I try to do any action that requires db access.
Here is docker-compose.yml
version: '3.1'
services:
drools-average-docker-app:
image: drools-average-docker-image
build:
context: ./
dockerfile: Dockerfile
ports:
- 8080:8080
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: 'droolsTestDB'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
ports:
- "5432:5432"
I also read somewhere that it might be iptables
blocking the connection? So just in case, here is iptables.rules
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [8:411]
:TCP - [0:0]
:UDP - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
COMMIT
postgresql docker docker-compose iptables
I have been trying to dockerize my web-app and everything has gone (mostly) well so far! Except now I am trying to wire in (sorry if the verbage is incorrect) in postgreql as the application connects to a psql DB for logging in as well as storing things.
I read in a post here on SO that I need to just map the db to my local machine by adding ports: -"5432:5432"
to docker-compose.yml. (here is the referenced question Docker Compose + Postgres: Expose port)
However now I am getting a connection refused error whenever I try to do any action that requires db access.
Here is docker-compose.yml
version: '3.1'
services:
drools-average-docker-app:
image: drools-average-docker-image
build:
context: ./
dockerfile: Dockerfile
ports:
- 8080:8080
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: 'droolsTestDB'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
ports:
- "5432:5432"
I also read somewhere that it might be iptables
blocking the connection? So just in case, here is iptables.rules
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [8:411]
:TCP - [0:0]
:UDP - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
COMMIT
postgresql docker docker-compose iptables
postgresql docker docker-compose iptables
asked Nov 19 at 18:10
j-money
209113
209113
What is the DB connection used in the app? Normally there is no need to expose port 5432 to the host. Instead connect to port 5432 on the hostdb
instead.
– Ralf Stubner
Nov 19 at 18:47
I use jdbc to connectspring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
with postgres as the user and password
– j-money
Nov 19 at 19:11
add a comment |
What is the DB connection used in the app? Normally there is no need to expose port 5432 to the host. Instead connect to port 5432 on the hostdb
instead.
– Ralf Stubner
Nov 19 at 18:47
I use jdbc to connectspring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
with postgres as the user and password
– j-money
Nov 19 at 19:11
What is the DB connection used in the app? Normally there is no need to expose port 5432 to the host. Instead connect to port 5432 on the host
db
instead.– Ralf Stubner
Nov 19 at 18:47
What is the DB connection used in the app? Normally there is no need to expose port 5432 to the host. Instead connect to port 5432 on the host
db
instead.– Ralf Stubner
Nov 19 at 18:47
I use jdbc to connect
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
with postgres as the user and password– j-money
Nov 19 at 19:11
I use jdbc to connect
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
with postgres as the user and password– j-money
Nov 19 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
In the comments you write that you use
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
to connect to the DB. Change this to
spring.datasource.url=jdbc:postgresql://db/droolsTestDB
and it should work even without exposing port 5432 to the host.
Background: Each docker container has it's own network interface that corresponds to localhost
for that particular container. Connecting to localhost
from within a container will therefore not connect to the host or some other container. Since docker-compose
offers automatic DNS resolution, it is easy to refer to other services by their names instead.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In the comments you write that you use
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
to connect to the DB. Change this to
spring.datasource.url=jdbc:postgresql://db/droolsTestDB
and it should work even without exposing port 5432 to the host.
Background: Each docker container has it's own network interface that corresponds to localhost
for that particular container. Connecting to localhost
from within a container will therefore not connect to the host or some other container. Since docker-compose
offers automatic DNS resolution, it is easy to refer to other services by their names instead.
add a comment |
up vote
1
down vote
accepted
In the comments you write that you use
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
to connect to the DB. Change this to
spring.datasource.url=jdbc:postgresql://db/droolsTestDB
and it should work even without exposing port 5432 to the host.
Background: Each docker container has it's own network interface that corresponds to localhost
for that particular container. Connecting to localhost
from within a container will therefore not connect to the host or some other container. Since docker-compose
offers automatic DNS resolution, it is easy to refer to other services by their names instead.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In the comments you write that you use
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
to connect to the DB. Change this to
spring.datasource.url=jdbc:postgresql://db/droolsTestDB
and it should work even without exposing port 5432 to the host.
Background: Each docker container has it's own network interface that corresponds to localhost
for that particular container. Connecting to localhost
from within a container will therefore not connect to the host or some other container. Since docker-compose
offers automatic DNS resolution, it is easy to refer to other services by their names instead.
In the comments you write that you use
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
to connect to the DB. Change this to
spring.datasource.url=jdbc:postgresql://db/droolsTestDB
and it should work even without exposing port 5432 to the host.
Background: Each docker container has it's own network interface that corresponds to localhost
for that particular container. Connecting to localhost
from within a container will therefore not connect to the host or some other container. Since docker-compose
offers automatic DNS resolution, it is easy to refer to other services by their names instead.
answered Nov 19 at 19:16
Ralf Stubner
13.4k21437
13.4k21437
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380398%2fdocker-postgresql-connection-refused%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the DB connection used in the app? Normally there is no need to expose port 5432 to the host. Instead connect to port 5432 on the host
db
instead.– Ralf Stubner
Nov 19 at 18:47
I use jdbc to connect
spring.datasource.url=jdbc:postgresql://localhost/droolsTestDB
with postgres as the user and password– j-money
Nov 19 at 19:11