Docker Postgresql connection refused












-1















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









share|improve this question























  • 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
















-1















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









share|improve this question























  • 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














-1












-1








-1








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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

















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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 19:16









        Ralf StubnerRalf Stubner

        14.2k21537




        14.2k21537
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)