Docker Postgresql connection refused
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 |
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 '18 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 '18 at 19:11
add a comment |
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 '18 at 18:10
j-moneyj-money
218114
218114
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 19:16
Ralf StubnerRalf Stubner
14.2k21537
14.2k21537
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.
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 '18 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 '18 at 19:11