Allow docker container to connect to a local/host postgres databaseConnecting to postgres from a docker containerCommunicate to Docker host from Docker containerHow to connect a Docker container to PostgreSQL running on the localhost using a DATABASE_URL?Unable to connect containerized app to containerized postgres, receiving dial tcp connection refused errorDocker container to local osx postgresImport PostgreSQL with Sqoop in DockerDocker localhost connection blocked by UFWConnect to Postgresql on Windows host machine with DockerConnect containerized .NET Core MVC/WebAPI app to LOCALLY installed PostgresAccess database that is outside the docker environmentHow to list containers in DockerHow to get a Docker container's IP address from the host?How to remove old Docker containersHow to deal with persistent storage (e.g. databases) in DockerCopying files from Docker container to hostCopying files from host to Docker containerHow to copy Docker images from one host to another without using a repositoryFrom inside of a Docker container, how do I connect to the localhost of the machine?How to create User/Database in script for Docker PostgresPostgres networking with Docker containers and host LAN

Why did David Cameron offer a referendum on the European Union?

Teacher help me explain this to my students

A Riley Respite

Who will lead the country until there is a new Tory leader?

using Leibniz rule to solve definite integral

C++ forcing function parameter evalution order

Why didn't Thanos use the Time Stone to stop the Avengers' plan?

Make 24 using exactly three 3s

number headings

How strong are Wi-Fi signals?

Is the field of q-series 'dead'?

What is this opening/position called, and what is white’s best move?

I unknowingly submitted plagarised work

Website returning plaintext password

Can a person survive on blood in place of water?

What is the object moving across the ceiling in this stock footage?

Where can I find visible/radio telescopic observations of the center of the Milky Way galaxy?

How to patch glass cuts in a bicycle tire?

Did the UK Government ask for the Irish backstop?

Why did the person in charge of a principality not just declare themself king?

How do I partition a matrx into blocks and replace zeros with dots?

Boss wants me to falsify a report. How should I document this unethical demand?

What are these arcade games in Ghostbusters 1984?

Is real public IP Address hidden when using a system wide proxy in Windows 10?



Allow docker container to connect to a local/host postgres database


Connecting to postgres from a docker containerCommunicate to Docker host from Docker containerHow to connect a Docker container to PostgreSQL running on the localhost using a DATABASE_URL?Unable to connect containerized app to containerized postgres, receiving dial tcp connection refused errorDocker container to local osx postgresImport PostgreSQL with Sqoop in DockerDocker localhost connection blocked by UFWConnect to Postgresql on Windows host machine with DockerConnect containerized .NET Core MVC/WebAPI app to LOCALLY installed PostgresAccess database that is outside the docker environmentHow to list containers in DockerHow to get a Docker container's IP address from the host?How to remove old Docker containersHow to deal with persistent storage (e.g. databases) in DockerCopying files from Docker container to hostCopying files from host to Docker containerHow to copy Docker images from one host to another without using a repositoryFrom inside of a Docker container, how do I connect to the localhost of the machine?How to create User/Database in script for Docker PostgresPostgres networking with Docker containers and host LAN






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








89















I've recently been playing around with Docker and QGIS and have installed a container following the instructions in this tutorial.



Everything works great, although I am unable to connect to a localhost postgres database that contains all my GIS data. I figure this is because my postgres database is not configured to accept remote connections and have been editing the postgres conf files to allow remote connections using the instructions in this article.



I'm still getting an error message when I try and connect to my database running QGIS in Docker: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections to port 5433?
The postgres server is running, and I've edited my pg_hba.conf file to allow connections from a range of IP addresses (172.17.0.0/32). I had previously queried the IP address of the docker container using docker ps and although the IP address changes, it has so far always been in the range 172.17.0.x



Any ideas why I can't connect to this database? Probably something very simple I imagine!



I'm running Ubuntu 14.04; Postgres 9.3










share|improve this question






























    89















    I've recently been playing around with Docker and QGIS and have installed a container following the instructions in this tutorial.



    Everything works great, although I am unable to connect to a localhost postgres database that contains all my GIS data. I figure this is because my postgres database is not configured to accept remote connections and have been editing the postgres conf files to allow remote connections using the instructions in this article.



    I'm still getting an error message when I try and connect to my database running QGIS in Docker: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections to port 5433?
    The postgres server is running, and I've edited my pg_hba.conf file to allow connections from a range of IP addresses (172.17.0.0/32). I had previously queried the IP address of the docker container using docker ps and although the IP address changes, it has so far always been in the range 172.17.0.x



    Any ideas why I can't connect to this database? Probably something very simple I imagine!



    I'm running Ubuntu 14.04; Postgres 9.3










    share|improve this question


























      89












      89








      89


      39






      I've recently been playing around with Docker and QGIS and have installed a container following the instructions in this tutorial.



      Everything works great, although I am unable to connect to a localhost postgres database that contains all my GIS data. I figure this is because my postgres database is not configured to accept remote connections and have been editing the postgres conf files to allow remote connections using the instructions in this article.



      I'm still getting an error message when I try and connect to my database running QGIS in Docker: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections to port 5433?
      The postgres server is running, and I've edited my pg_hba.conf file to allow connections from a range of IP addresses (172.17.0.0/32). I had previously queried the IP address of the docker container using docker ps and although the IP address changes, it has so far always been in the range 172.17.0.x



      Any ideas why I can't connect to this database? Probably something very simple I imagine!



      I'm running Ubuntu 14.04; Postgres 9.3










      share|improve this question
















      I've recently been playing around with Docker and QGIS and have installed a container following the instructions in this tutorial.



      Everything works great, although I am unable to connect to a localhost postgres database that contains all my GIS data. I figure this is because my postgres database is not configured to accept remote connections and have been editing the postgres conf files to allow remote connections using the instructions in this article.



      I'm still getting an error message when I try and connect to my database running QGIS in Docker: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections to port 5433?
      The postgres server is running, and I've edited my pg_hba.conf file to allow connections from a range of IP addresses (172.17.0.0/32). I had previously queried the IP address of the docker container using docker ps and although the IP address changes, it has so far always been in the range 172.17.0.x



      Any ideas why I can't connect to this database? Probably something very simple I imagine!



      I'm running Ubuntu 14.04; Postgres 9.3







      ubuntu docker postgresql-9.3 qgis






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 26 '18 at 14:52









      Cepr0

      11.4k22154




      11.4k22154










      asked Jul 6 '15 at 14:57









      marty_cmarty_c

      1,20131721




      1,20131721






















          7 Answers
          7






          active

          oldest

          votes


















          104














          TL;DR



          1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32.

          2. Don't use localhost to connect to the PostgreSQL database on your host, but the host's IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL.

          3. Make sure PostreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL's configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

          Long version



          172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0). No Docker container will ever get that address assigned, because it's the network address of the Docker bridge (docker0) interface.



          When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a:



          $ ip a
          ...
          3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
          link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
          inet 172.17.42.1/16 scope global docker0
          valid_lft forever preferred_lft forever


          As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0). This means that the network address is 172.17.0.0/16.



          The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.



          This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16.






          share|improve this answer




















          • 1





            hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

            – marty_c
            Jul 7 '15 at 9:00












          • Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

            – helmbert
            Jul 7 '15 at 9:22











          • ah, that did it! thanks very much

            – marty_c
            Jul 7 '15 at 10:36






          • 5





            I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

            – Dzamo Norton
            Nov 7 '15 at 10:32











          • @DzamoNorton, thanks for the hint! I updated my answer accordingly.

            – helmbert
            Nov 7 '15 at 12:19


















          43














          Docker for Mac solution



          17.06 onwards



          Thanks to @Birchlabs' comment, now it is tons easier with this special Mac-only DNS name available:



          docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal


          From 17.12.0-cd-mac46, docker.for.mac.host.internal should be used instead of docker.for.mac.localhost. See release note for details.



          Older version



          @helmbert's answer well explains the issue. But Docker for Mac does not expose the bridge network, so I had to do this trick to workaround the limitation:



          $ sudo ifconfig lo0 alias 10.200.10.1/24


          Open /usr/local/var/postgres/pg_hba.conf and add this line:



          host all all 10.200.10.1/24 trust


          Open /usr/local/var/postgres/postgresql.conf and edit change listen_addresses:



          listen_addresses = '*'


          Reload service and launch your container:



          $ PGDATA=/usr/local/var/postgres pg_ctl reload
          $ docker run -e DB_PORT=5432 -e DB_HOST=10.200.10.1 my_app


          What this workaround does is basically same with @helmbert's answer, but uses an IP address that is attached to lo0 instead of docker0 network interface.






          share|improve this answer




















          • 3





            Is this still current as of 4 April 2017?

            – Petrus Theron
            Apr 4 '17 at 9:37











          • Yep, worked a treat for me! Thanks @baxang!

            – VirtualWolf
            Jun 1 '17 at 11:46











          • I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

            – Tsung Goh
            Jun 14 '17 at 17:01







          • 6





            There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

            – Birchlabs
            Jul 28 '17 at 17:18






          • 2





            Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

            – gseva
            Apr 26 '18 at 20:12


















          15














          Simple Solution for mac:



          The newest version of docker (18.03) offers a built in port forwarding solution. Inside your docker container simply have the db host set to host.docker.internal. This will be forwarded to the host the docker container is running on.



          Documentation for this is here: https://docs.docker.com/docker-for-mac/networking/#per-container-ip-addressing-is-not-possible






          share|improve this answer


















          • 1





            This is by far the best answer now! way easier and how it should be.

            – bjm88
            Jul 7 '18 at 4:58











          • is host.docker.internal limited to macs only?

            – Dragas
            Nov 12 '18 at 11:22


















          2














          Simple solution



          Just add --network=host to docker run. That's all!



          This way container will use the host's network, so localhost and 127.0.0.1 will point to the host (by default they point to a container). Example:



          docker run -d --network=host 
          -e "DB_DBNAME=your_db"
          -e "DB_PORT=5432"
          -e "DB_USER=your_db_user"
          -e "DB_PASS=your_db_password"
          -e "DB_HOST=127.0.0.1"
          --name foobar foo/bar





          share|improve this answer























          • Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

            – jfcorugedo
            Mar 27 at 16:01


















          1














          One more thing needed for my setup was to add



          172.17.0.1 localhost


          to /etc/hosts



          so that Docker would point to 172.17.0.1 as the DB hostname, and not rely on a changing outer ip to find the DB. Hope this helps someone else with this issue!






          share|improve this answer




















          • 9





            This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

            – Alex
            Oct 22 '17 at 2:17






          • 1





            A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

            – jaredsk
            Dec 1 '17 at 16:59











          • the --add-host=database:172.17.0.1 is preferable

            – Luis Martins
            Apr 19 '18 at 19:50


















          1














          In Ubuntu:



          First You have to check that is the Docker Database port is Available in your system by following command -



          sudo iptables -L -n


          Sample OUTPUT:



          Chain DOCKER (1 references)
          target prot opt source destination
          ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:3306
          ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
          ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:22


          Here 3306 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



          sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT


          Now, You can easily access the Docker Database from your local system by following configuration



           host: 172.17.0.2 
          adapter: mysql
          database: DATABASE_NAME
          port: 3307
          username: DATABASE_USER
          password: DATABASE_PASSWORD
          encoding: utf8


          In CentOS:



          First You have to check that is the Docker Database port is Available in your firewall by following command -



          sudo firewall-cmd --list-all


          Sample OUTPUT:



           target: default
          icmp-block-inversion: no
          interfaces: eno79841677
          sources:
          services: dhcpv6-client ssh
          **ports: 3307/tcp**
          protocols:
          masquerade: no
          forward-ports:
          sourceports:
          icmp-blocks:
          rich rules:


          Here 3307 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



          sudo firewall-cmd --zone=public --add-port=3307/tcp


          In server, You can add the port permanently



          sudo firewall-cmd --permanent --add-port=3307/tcp
          sudo firewall-cmd --reload


          Now, You can easily access the Docker Database from your local system by the above configuration.






          share|improve this answer
































            -1














            The another solution is service volume, You can define a service volume and mount host's PostgreSQL Data directory in that volume. Check out the given compose file for details.



            version: '2'
            services:
            db:
            image: postgres:9.6.1
            volumes:
            - "/var/lib/postgresql/data:/var/lib/postgresql/data"
            ports:
            - "5432:5432"


            By doing this, another PostgreSQL service will run under container but uses same data directory which host PostgreSQL service is using.






            share|improve this answer


















            • 1





              This will probably cause write conflicts with a running host PostgreSQL service

              – Petrus Theron
              Apr 4 '17 at 9:37











            • I think stopping host service will solve the problem in that case.

              – Hrishi
              Apr 6 '17 at 6:19











            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%2f31249112%2fallow-docker-container-to-connect-to-a-local-host-postgres-database%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            104














            TL;DR



            1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32.

            2. Don't use localhost to connect to the PostgreSQL database on your host, but the host's IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL.

            3. Make sure PostreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL's configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

            Long version



            172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0). No Docker container will ever get that address assigned, because it's the network address of the Docker bridge (docker0) interface.



            When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a:



            $ ip a
            ...
            3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
            link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
            inet 172.17.42.1/16 scope global docker0
            valid_lft forever preferred_lft forever


            As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0). This means that the network address is 172.17.0.0/16.



            The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.



            This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16.






            share|improve this answer




















            • 1





              hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

              – marty_c
              Jul 7 '15 at 9:00












            • Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

              – helmbert
              Jul 7 '15 at 9:22











            • ah, that did it! thanks very much

              – marty_c
              Jul 7 '15 at 10:36






            • 5





              I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

              – Dzamo Norton
              Nov 7 '15 at 10:32











            • @DzamoNorton, thanks for the hint! I updated my answer accordingly.

              – helmbert
              Nov 7 '15 at 12:19















            104














            TL;DR



            1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32.

            2. Don't use localhost to connect to the PostgreSQL database on your host, but the host's IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL.

            3. Make sure PostreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL's configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

            Long version



            172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0). No Docker container will ever get that address assigned, because it's the network address of the Docker bridge (docker0) interface.



            When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a:



            $ ip a
            ...
            3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
            link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
            inet 172.17.42.1/16 scope global docker0
            valid_lft forever preferred_lft forever


            As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0). This means that the network address is 172.17.0.0/16.



            The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.



            This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16.






            share|improve this answer




















            • 1





              hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

              – marty_c
              Jul 7 '15 at 9:00












            • Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

              – helmbert
              Jul 7 '15 at 9:22











            • ah, that did it! thanks very much

              – marty_c
              Jul 7 '15 at 10:36






            • 5





              I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

              – Dzamo Norton
              Nov 7 '15 at 10:32











            • @DzamoNorton, thanks for the hint! I updated my answer accordingly.

              – helmbert
              Nov 7 '15 at 12:19













            104












            104








            104







            TL;DR



            1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32.

            2. Don't use localhost to connect to the PostgreSQL database on your host, but the host's IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL.

            3. Make sure PostreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL's configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

            Long version



            172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0). No Docker container will ever get that address assigned, because it's the network address of the Docker bridge (docker0) interface.



            When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a:



            $ ip a
            ...
            3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
            link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
            inet 172.17.42.1/16 scope global docker0
            valid_lft forever preferred_lft forever


            As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0). This means that the network address is 172.17.0.0/16.



            The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.



            This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16.






            share|improve this answer















            TL;DR



            1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32.

            2. Don't use localhost to connect to the PostgreSQL database on your host, but the host's IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL.

            3. Make sure PostreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL's configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

            Long version



            172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0). No Docker container will ever get that address assigned, because it's the network address of the Docker bridge (docker0) interface.



            When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a:



            $ ip a
            ...
            3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
            link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
            inet 172.17.42.1/16 scope global docker0
            valid_lft forever preferred_lft forever


            As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0). This means that the network address is 172.17.0.0/16.



            The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.



            This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 7 '15 at 12:17

























            answered Jul 6 '15 at 15:05









            helmberthelmbert

            19.3k64957




            19.3k64957







            • 1





              hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

              – marty_c
              Jul 7 '15 at 9:00












            • Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

              – helmbert
              Jul 7 '15 at 9:22











            • ah, that did it! thanks very much

              – marty_c
              Jul 7 '15 at 10:36






            • 5





              I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

              – Dzamo Norton
              Nov 7 '15 at 10:32











            • @DzamoNorton, thanks for the hint! I updated my answer accordingly.

              – helmbert
              Nov 7 '15 at 12:19












            • 1





              hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

              – marty_c
              Jul 7 '15 at 9:00












            • Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

              – helmbert
              Jul 7 '15 at 9:22











            • ah, that did it! thanks very much

              – marty_c
              Jul 7 '15 at 10:36






            • 5





              I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

              – Dzamo Norton
              Nov 7 '15 at 10:32











            • @DzamoNorton, thanks for the hint! I updated my answer accordingly.

              – helmbert
              Nov 7 '15 at 12:19







            1




            1





            hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

            – marty_c
            Jul 7 '15 at 9:00






            hey thanks for your comments. I've changed my pg_hba.conf to the address you suggested, but still get the same connection error message after stopping and restarting the postgres service. I've added the line under my ipv4 connections - is there somewhere else I'm supposed to add the address you suggest? Alternatively in my QGIS app running in Docker do I need to change the postgres connection info? For example, if I'm connecting from within a docker container is the host still 'localhost'?

            – marty_c
            Jul 7 '15 at 9:00














            Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

            – helmbert
            Jul 7 '15 at 9:22





            Ah, that's an important point. No, localhost is not the host system inside your Docker container. Try connecting to the host system's public IP address. To keep the container portable, you can also start the container with the --add-host=database:<host-ip> and simply use database as hostname to connect to your PostgreSQL host from within the Docker container.

            – helmbert
            Jul 7 '15 at 9:22













            ah, that did it! thanks very much

            – marty_c
            Jul 7 '15 at 10:36





            ah, that did it! thanks very much

            – marty_c
            Jul 7 '15 at 10:36




            5




            5





            I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

            – Dzamo Norton
            Nov 7 '15 at 10:32





            I needed one more piece. I also had to edit /etc/postgresql/9.3/main/postgresql.conf and add my server's eth0 IP address to listen_addresses. By default listen_addresses has postgres bind to localhost only.

            – Dzamo Norton
            Nov 7 '15 at 10:32













            @DzamoNorton, thanks for the hint! I updated my answer accordingly.

            – helmbert
            Nov 7 '15 at 12:19





            @DzamoNorton, thanks for the hint! I updated my answer accordingly.

            – helmbert
            Nov 7 '15 at 12:19













            43














            Docker for Mac solution



            17.06 onwards



            Thanks to @Birchlabs' comment, now it is tons easier with this special Mac-only DNS name available:



            docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal


            From 17.12.0-cd-mac46, docker.for.mac.host.internal should be used instead of docker.for.mac.localhost. See release note for details.



            Older version



            @helmbert's answer well explains the issue. But Docker for Mac does not expose the bridge network, so I had to do this trick to workaround the limitation:



            $ sudo ifconfig lo0 alias 10.200.10.1/24


            Open /usr/local/var/postgres/pg_hba.conf and add this line:



            host all all 10.200.10.1/24 trust


            Open /usr/local/var/postgres/postgresql.conf and edit change listen_addresses:



            listen_addresses = '*'


            Reload service and launch your container:



            $ PGDATA=/usr/local/var/postgres pg_ctl reload
            $ docker run -e DB_PORT=5432 -e DB_HOST=10.200.10.1 my_app


            What this workaround does is basically same with @helmbert's answer, but uses an IP address that is attached to lo0 instead of docker0 network interface.






            share|improve this answer




















            • 3





              Is this still current as of 4 April 2017?

              – Petrus Theron
              Apr 4 '17 at 9:37











            • Yep, worked a treat for me! Thanks @baxang!

              – VirtualWolf
              Jun 1 '17 at 11:46











            • I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

              – Tsung Goh
              Jun 14 '17 at 17:01







            • 6





              There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

              – Birchlabs
              Jul 28 '17 at 17:18






            • 2





              Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

              – gseva
              Apr 26 '18 at 20:12















            43














            Docker for Mac solution



            17.06 onwards



            Thanks to @Birchlabs' comment, now it is tons easier with this special Mac-only DNS name available:



            docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal


            From 17.12.0-cd-mac46, docker.for.mac.host.internal should be used instead of docker.for.mac.localhost. See release note for details.



            Older version



            @helmbert's answer well explains the issue. But Docker for Mac does not expose the bridge network, so I had to do this trick to workaround the limitation:



            $ sudo ifconfig lo0 alias 10.200.10.1/24


            Open /usr/local/var/postgres/pg_hba.conf and add this line:



            host all all 10.200.10.1/24 trust


            Open /usr/local/var/postgres/postgresql.conf and edit change listen_addresses:



            listen_addresses = '*'


            Reload service and launch your container:



            $ PGDATA=/usr/local/var/postgres pg_ctl reload
            $ docker run -e DB_PORT=5432 -e DB_HOST=10.200.10.1 my_app


            What this workaround does is basically same with @helmbert's answer, but uses an IP address that is attached to lo0 instead of docker0 network interface.






            share|improve this answer




















            • 3





              Is this still current as of 4 April 2017?

              – Petrus Theron
              Apr 4 '17 at 9:37











            • Yep, worked a treat for me! Thanks @baxang!

              – VirtualWolf
              Jun 1 '17 at 11:46











            • I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

              – Tsung Goh
              Jun 14 '17 at 17:01







            • 6





              There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

              – Birchlabs
              Jul 28 '17 at 17:18






            • 2





              Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

              – gseva
              Apr 26 '18 at 20:12













            43












            43








            43







            Docker for Mac solution



            17.06 onwards



            Thanks to @Birchlabs' comment, now it is tons easier with this special Mac-only DNS name available:



            docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal


            From 17.12.0-cd-mac46, docker.for.mac.host.internal should be used instead of docker.for.mac.localhost. See release note for details.



            Older version



            @helmbert's answer well explains the issue. But Docker for Mac does not expose the bridge network, so I had to do this trick to workaround the limitation:



            $ sudo ifconfig lo0 alias 10.200.10.1/24


            Open /usr/local/var/postgres/pg_hba.conf and add this line:



            host all all 10.200.10.1/24 trust


            Open /usr/local/var/postgres/postgresql.conf and edit change listen_addresses:



            listen_addresses = '*'


            Reload service and launch your container:



            $ PGDATA=/usr/local/var/postgres pg_ctl reload
            $ docker run -e DB_PORT=5432 -e DB_HOST=10.200.10.1 my_app


            What this workaround does is basically same with @helmbert's answer, but uses an IP address that is attached to lo0 instead of docker0 network interface.






            share|improve this answer















            Docker for Mac solution



            17.06 onwards



            Thanks to @Birchlabs' comment, now it is tons easier with this special Mac-only DNS name available:



            docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal


            From 17.12.0-cd-mac46, docker.for.mac.host.internal should be used instead of docker.for.mac.localhost. See release note for details.



            Older version



            @helmbert's answer well explains the issue. But Docker for Mac does not expose the bridge network, so I had to do this trick to workaround the limitation:



            $ sudo ifconfig lo0 alias 10.200.10.1/24


            Open /usr/local/var/postgres/pg_hba.conf and add this line:



            host all all 10.200.10.1/24 trust


            Open /usr/local/var/postgres/postgresql.conf and edit change listen_addresses:



            listen_addresses = '*'


            Reload service and launch your container:



            $ PGDATA=/usr/local/var/postgres pg_ctl reload
            $ docker run -e DB_PORT=5432 -e DB_HOST=10.200.10.1 my_app


            What this workaround does is basically same with @helmbert's answer, but uses an IP address that is attached to lo0 instead of docker0 network interface.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 10 '18 at 22:23

























            answered Jan 10 '17 at 5:57









            baxangbaxang

            2,94912322




            2,94912322







            • 3





              Is this still current as of 4 April 2017?

              – Petrus Theron
              Apr 4 '17 at 9:37











            • Yep, worked a treat for me! Thanks @baxang!

              – VirtualWolf
              Jun 1 '17 at 11:46











            • I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

              – Tsung Goh
              Jun 14 '17 at 17:01







            • 6





              There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

              – Birchlabs
              Jul 28 '17 at 17:18






            • 2





              Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

              – gseva
              Apr 26 '18 at 20:12












            • 3





              Is this still current as of 4 April 2017?

              – Petrus Theron
              Apr 4 '17 at 9:37











            • Yep, worked a treat for me! Thanks @baxang!

              – VirtualWolf
              Jun 1 '17 at 11:46











            • I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

              – Tsung Goh
              Jun 14 '17 at 17:01







            • 6





              There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

              – Birchlabs
              Jul 28 '17 at 17:18






            • 2





              Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

              – gseva
              Apr 26 '18 at 20:12







            3




            3





            Is this still current as of 4 April 2017?

            – Petrus Theron
            Apr 4 '17 at 9:37





            Is this still current as of 4 April 2017?

            – Petrus Theron
            Apr 4 '17 at 9:37













            Yep, worked a treat for me! Thanks @baxang!

            – VirtualWolf
            Jun 1 '17 at 11:46





            Yep, worked a treat for me! Thanks @baxang!

            – VirtualWolf
            Jun 1 '17 at 11:46













            I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

            – Tsung Goh
            Jun 14 '17 at 17:01






            I like this way which will not expose database. BTW, can I use this on CentOS? I got the error: alias: Unknown host when I tried to use the alias command you provide.

            – Tsung Goh
            Jun 14 '17 at 17:01





            6




            6





            There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

            – Birchlabs
            Jul 28 '17 at 17:18





            There is a better way on macOS, as of Docker 17.06.0-rc1-ce-mac13 (June 1st 2017). containers recognise the host docker.for.mac.localhost. this is the IP of your host machine. lookup its entry in the container's hosts database like so: docker run alpine /bin/sh -c 'getent hosts docker.for.mac.localhost'

            – Birchlabs
            Jul 28 '17 at 17:18




            2




            2





            Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

            – gseva
            Apr 26 '18 at 20:12





            Seems like it changed to host.docker.internal since 18.03, other options are still available but deprecated (Source).

            – gseva
            Apr 26 '18 at 20:12











            15














            Simple Solution for mac:



            The newest version of docker (18.03) offers a built in port forwarding solution. Inside your docker container simply have the db host set to host.docker.internal. This will be forwarded to the host the docker container is running on.



            Documentation for this is here: https://docs.docker.com/docker-for-mac/networking/#per-container-ip-addressing-is-not-possible






            share|improve this answer


















            • 1





              This is by far the best answer now! way easier and how it should be.

              – bjm88
              Jul 7 '18 at 4:58











            • is host.docker.internal limited to macs only?

              – Dragas
              Nov 12 '18 at 11:22















            15














            Simple Solution for mac:



            The newest version of docker (18.03) offers a built in port forwarding solution. Inside your docker container simply have the db host set to host.docker.internal. This will be forwarded to the host the docker container is running on.



            Documentation for this is here: https://docs.docker.com/docker-for-mac/networking/#per-container-ip-addressing-is-not-possible






            share|improve this answer


















            • 1





              This is by far the best answer now! way easier and how it should be.

              – bjm88
              Jul 7 '18 at 4:58











            • is host.docker.internal limited to macs only?

              – Dragas
              Nov 12 '18 at 11:22













            15












            15








            15







            Simple Solution for mac:



            The newest version of docker (18.03) offers a built in port forwarding solution. Inside your docker container simply have the db host set to host.docker.internal. This will be forwarded to the host the docker container is running on.



            Documentation for this is here: https://docs.docker.com/docker-for-mac/networking/#per-container-ip-addressing-is-not-possible






            share|improve this answer













            Simple Solution for mac:



            The newest version of docker (18.03) offers a built in port forwarding solution. Inside your docker container simply have the db host set to host.docker.internal. This will be forwarded to the host the docker container is running on.



            Documentation for this is here: https://docs.docker.com/docker-for-mac/networking/#per-container-ip-addressing-is-not-possible







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 17 '18 at 14:49









            ChrisChris

            19218




            19218







            • 1





              This is by far the best answer now! way easier and how it should be.

              – bjm88
              Jul 7 '18 at 4:58











            • is host.docker.internal limited to macs only?

              – Dragas
              Nov 12 '18 at 11:22












            • 1





              This is by far the best answer now! way easier and how it should be.

              – bjm88
              Jul 7 '18 at 4:58











            • is host.docker.internal limited to macs only?

              – Dragas
              Nov 12 '18 at 11:22







            1




            1





            This is by far the best answer now! way easier and how it should be.

            – bjm88
            Jul 7 '18 at 4:58





            This is by far the best answer now! way easier and how it should be.

            – bjm88
            Jul 7 '18 at 4:58













            is host.docker.internal limited to macs only?

            – Dragas
            Nov 12 '18 at 11:22





            is host.docker.internal limited to macs only?

            – Dragas
            Nov 12 '18 at 11:22











            2














            Simple solution



            Just add --network=host to docker run. That's all!



            This way container will use the host's network, so localhost and 127.0.0.1 will point to the host (by default they point to a container). Example:



            docker run -d --network=host 
            -e "DB_DBNAME=your_db"
            -e "DB_PORT=5432"
            -e "DB_USER=your_db_user"
            -e "DB_PASS=your_db_password"
            -e "DB_HOST=127.0.0.1"
            --name foobar foo/bar





            share|improve this answer























            • Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

              – jfcorugedo
              Mar 27 at 16:01















            2














            Simple solution



            Just add --network=host to docker run. That's all!



            This way container will use the host's network, so localhost and 127.0.0.1 will point to the host (by default they point to a container). Example:



            docker run -d --network=host 
            -e "DB_DBNAME=your_db"
            -e "DB_PORT=5432"
            -e "DB_USER=your_db_user"
            -e "DB_PASS=your_db_password"
            -e "DB_HOST=127.0.0.1"
            --name foobar foo/bar





            share|improve this answer























            • Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

              – jfcorugedo
              Mar 27 at 16:01













            2












            2








            2







            Simple solution



            Just add --network=host to docker run. That's all!



            This way container will use the host's network, so localhost and 127.0.0.1 will point to the host (by default they point to a container). Example:



            docker run -d --network=host 
            -e "DB_DBNAME=your_db"
            -e "DB_PORT=5432"
            -e "DB_USER=your_db_user"
            -e "DB_PASS=your_db_password"
            -e "DB_HOST=127.0.0.1"
            --name foobar foo/bar





            share|improve this answer













            Simple solution



            Just add --network=host to docker run. That's all!



            This way container will use the host's network, so localhost and 127.0.0.1 will point to the host (by default they point to a container). Example:



            docker run -d --network=host 
            -e "DB_DBNAME=your_db"
            -e "DB_PORT=5432"
            -e "DB_USER=your_db_user"
            -e "DB_PASS=your_db_password"
            -e "DB_HOST=127.0.0.1"
            --name foobar foo/bar






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 20 at 21:33









            Max MalyshMax Malysh

            9,02285469




            9,02285469












            • Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

              – jfcorugedo
              Mar 27 at 16:01

















            • Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

              – jfcorugedo
              Mar 27 at 16:01
















            Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

            – jfcorugedo
            Mar 27 at 16:01





            Be careful! This solution, that is the right one in my opinion, does not work for macOS. Don't waste your time trying to figure out why it is not working. Take a look at: github.com/docker/for-mac/issues/2716

            – jfcorugedo
            Mar 27 at 16:01











            1














            One more thing needed for my setup was to add



            172.17.0.1 localhost


            to /etc/hosts



            so that Docker would point to 172.17.0.1 as the DB hostname, and not rely on a changing outer ip to find the DB. Hope this helps someone else with this issue!






            share|improve this answer




















            • 9





              This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

              – Alex
              Oct 22 '17 at 2:17






            • 1





              A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

              – jaredsk
              Dec 1 '17 at 16:59











            • the --add-host=database:172.17.0.1 is preferable

              – Luis Martins
              Apr 19 '18 at 19:50















            1














            One more thing needed for my setup was to add



            172.17.0.1 localhost


            to /etc/hosts



            so that Docker would point to 172.17.0.1 as the DB hostname, and not rely on a changing outer ip to find the DB. Hope this helps someone else with this issue!






            share|improve this answer




















            • 9





              This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

              – Alex
              Oct 22 '17 at 2:17






            • 1





              A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

              – jaredsk
              Dec 1 '17 at 16:59











            • the --add-host=database:172.17.0.1 is preferable

              – Luis Martins
              Apr 19 '18 at 19:50













            1












            1








            1







            One more thing needed for my setup was to add



            172.17.0.1 localhost


            to /etc/hosts



            so that Docker would point to 172.17.0.1 as the DB hostname, and not rely on a changing outer ip to find the DB. Hope this helps someone else with this issue!






            share|improve this answer















            One more thing needed for my setup was to add



            172.17.0.1 localhost


            to /etc/hosts



            so that Docker would point to 172.17.0.1 as the DB hostname, and not rely on a changing outer ip to find the DB. Hope this helps someone else with this issue!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 2 '17 at 7:36









            goto

            5,496102843




            5,496102843










            answered Mar 2 '17 at 7:02









            Mikko PöriMikko Pöri

            16024




            16024







            • 9





              This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

              – Alex
              Oct 22 '17 at 2:17






            • 1





              A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

              – jaredsk
              Dec 1 '17 at 16:59











            • the --add-host=database:172.17.0.1 is preferable

              – Luis Martins
              Apr 19 '18 at 19:50












            • 9





              This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

              – Alex
              Oct 22 '17 at 2:17






            • 1





              A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

              – jaredsk
              Dec 1 '17 at 16:59











            • the --add-host=database:172.17.0.1 is preferable

              – Luis Martins
              Apr 19 '18 at 19:50







            9




            9





            This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

            – Alex
            Oct 22 '17 at 2:17





            This is a bad solution. Localhost should typically point to 127.0.0.1. Changing it might have undesired consequences, even if in this particular case it works.

            – Alex
            Oct 22 '17 at 2:17




            1




            1





            A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

            – jaredsk
            Dec 1 '17 at 16:59





            A better way is to set up a database host with --add-host=database:172.17.0.1 when running the container. Then point your app to that host. This avoids hard-coding an IP address inside a container.

            – jaredsk
            Dec 1 '17 at 16:59













            the --add-host=database:172.17.0.1 is preferable

            – Luis Martins
            Apr 19 '18 at 19:50





            the --add-host=database:172.17.0.1 is preferable

            – Luis Martins
            Apr 19 '18 at 19:50











            1














            In Ubuntu:



            First You have to check that is the Docker Database port is Available in your system by following command -



            sudo iptables -L -n


            Sample OUTPUT:



            Chain DOCKER (1 references)
            target prot opt source destination
            ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:3306
            ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
            ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:22


            Here 3306 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



            sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT


            Now, You can easily access the Docker Database from your local system by following configuration



             host: 172.17.0.2 
            adapter: mysql
            database: DATABASE_NAME
            port: 3307
            username: DATABASE_USER
            password: DATABASE_PASSWORD
            encoding: utf8


            In CentOS:



            First You have to check that is the Docker Database port is Available in your firewall by following command -



            sudo firewall-cmd --list-all


            Sample OUTPUT:



             target: default
            icmp-block-inversion: no
            interfaces: eno79841677
            sources:
            services: dhcpv6-client ssh
            **ports: 3307/tcp**
            protocols:
            masquerade: no
            forward-ports:
            sourceports:
            icmp-blocks:
            rich rules:


            Here 3307 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



            sudo firewall-cmd --zone=public --add-port=3307/tcp


            In server, You can add the port permanently



            sudo firewall-cmd --permanent --add-port=3307/tcp
            sudo firewall-cmd --reload


            Now, You can easily access the Docker Database from your local system by the above configuration.






            share|improve this answer





























              1














              In Ubuntu:



              First You have to check that is the Docker Database port is Available in your system by following command -



              sudo iptables -L -n


              Sample OUTPUT:



              Chain DOCKER (1 references)
              target prot opt source destination
              ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:3306
              ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
              ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:22


              Here 3306 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



              sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT


              Now, You can easily access the Docker Database from your local system by following configuration



               host: 172.17.0.2 
              adapter: mysql
              database: DATABASE_NAME
              port: 3307
              username: DATABASE_USER
              password: DATABASE_PASSWORD
              encoding: utf8


              In CentOS:



              First You have to check that is the Docker Database port is Available in your firewall by following command -



              sudo firewall-cmd --list-all


              Sample OUTPUT:



               target: default
              icmp-block-inversion: no
              interfaces: eno79841677
              sources:
              services: dhcpv6-client ssh
              **ports: 3307/tcp**
              protocols:
              masquerade: no
              forward-ports:
              sourceports:
              icmp-blocks:
              rich rules:


              Here 3307 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



              sudo firewall-cmd --zone=public --add-port=3307/tcp


              In server, You can add the port permanently



              sudo firewall-cmd --permanent --add-port=3307/tcp
              sudo firewall-cmd --reload


              Now, You can easily access the Docker Database from your local system by the above configuration.






              share|improve this answer



























                1












                1








                1







                In Ubuntu:



                First You have to check that is the Docker Database port is Available in your system by following command -



                sudo iptables -L -n


                Sample OUTPUT:



                Chain DOCKER (1 references)
                target prot opt source destination
                ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:3306
                ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
                ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:22


                Here 3306 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



                sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT


                Now, You can easily access the Docker Database from your local system by following configuration



                 host: 172.17.0.2 
                adapter: mysql
                database: DATABASE_NAME
                port: 3307
                username: DATABASE_USER
                password: DATABASE_PASSWORD
                encoding: utf8


                In CentOS:



                First You have to check that is the Docker Database port is Available in your firewall by following command -



                sudo firewall-cmd --list-all


                Sample OUTPUT:



                 target: default
                icmp-block-inversion: no
                interfaces: eno79841677
                sources:
                services: dhcpv6-client ssh
                **ports: 3307/tcp**
                protocols:
                masquerade: no
                forward-ports:
                sourceports:
                icmp-blocks:
                rich rules:


                Here 3307 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



                sudo firewall-cmd --zone=public --add-port=3307/tcp


                In server, You can add the port permanently



                sudo firewall-cmd --permanent --add-port=3307/tcp
                sudo firewall-cmd --reload


                Now, You can easily access the Docker Database from your local system by the above configuration.






                share|improve this answer















                In Ubuntu:



                First You have to check that is the Docker Database port is Available in your system by following command -



                sudo iptables -L -n


                Sample OUTPUT:



                Chain DOCKER (1 references)
                target prot opt source destination
                ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:3306
                ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:80
                ACCEPT tcp -- 0.0.0.0/0 172.17.0.3 tcp dpt:22


                Here 3306 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



                sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT


                Now, You can easily access the Docker Database from your local system by following configuration



                 host: 172.17.0.2 
                adapter: mysql
                database: DATABASE_NAME
                port: 3307
                username: DATABASE_USER
                password: DATABASE_PASSWORD
                encoding: utf8


                In CentOS:



                First You have to check that is the Docker Database port is Available in your firewall by following command -



                sudo firewall-cmd --list-all


                Sample OUTPUT:



                 target: default
                icmp-block-inversion: no
                interfaces: eno79841677
                sources:
                services: dhcpv6-client ssh
                **ports: 3307/tcp**
                protocols:
                masquerade: no
                forward-ports:
                sourceports:
                icmp-blocks:
                rich rules:


                Here 3307 is used as Docker Database Port on 172.17.0.2 IP, If this port is not available Run the following command -



                sudo firewall-cmd --zone=public --add-port=3307/tcp


                In server, You can add the port permanently



                sudo firewall-cmd --permanent --add-port=3307/tcp
                sudo firewall-cmd --reload


                Now, You can easily access the Docker Database from your local system by the above configuration.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 24 at 4:03

























                answered May 23 '18 at 7:06









                SanaullaSanaulla

                18125




                18125





















                    -1














                    The another solution is service volume, You can define a service volume and mount host's PostgreSQL Data directory in that volume. Check out the given compose file for details.



                    version: '2'
                    services:
                    db:
                    image: postgres:9.6.1
                    volumes:
                    - "/var/lib/postgresql/data:/var/lib/postgresql/data"
                    ports:
                    - "5432:5432"


                    By doing this, another PostgreSQL service will run under container but uses same data directory which host PostgreSQL service is using.






                    share|improve this answer


















                    • 1





                      This will probably cause write conflicts with a running host PostgreSQL service

                      – Petrus Theron
                      Apr 4 '17 at 9:37











                    • I think stopping host service will solve the problem in that case.

                      – Hrishi
                      Apr 6 '17 at 6:19















                    -1














                    The another solution is service volume, You can define a service volume and mount host's PostgreSQL Data directory in that volume. Check out the given compose file for details.



                    version: '2'
                    services:
                    db:
                    image: postgres:9.6.1
                    volumes:
                    - "/var/lib/postgresql/data:/var/lib/postgresql/data"
                    ports:
                    - "5432:5432"


                    By doing this, another PostgreSQL service will run under container but uses same data directory which host PostgreSQL service is using.






                    share|improve this answer


















                    • 1





                      This will probably cause write conflicts with a running host PostgreSQL service

                      – Petrus Theron
                      Apr 4 '17 at 9:37











                    • I think stopping host service will solve the problem in that case.

                      – Hrishi
                      Apr 6 '17 at 6:19













                    -1












                    -1








                    -1







                    The another solution is service volume, You can define a service volume and mount host's PostgreSQL Data directory in that volume. Check out the given compose file for details.



                    version: '2'
                    services:
                    db:
                    image: postgres:9.6.1
                    volumes:
                    - "/var/lib/postgresql/data:/var/lib/postgresql/data"
                    ports:
                    - "5432:5432"


                    By doing this, another PostgreSQL service will run under container but uses same data directory which host PostgreSQL service is using.






                    share|improve this answer













                    The another solution is service volume, You can define a service volume and mount host's PostgreSQL Data directory in that volume. Check out the given compose file for details.



                    version: '2'
                    services:
                    db:
                    image: postgres:9.6.1
                    volumes:
                    - "/var/lib/postgresql/data:/var/lib/postgresql/data"
                    ports:
                    - "5432:5432"


                    By doing this, another PostgreSQL service will run under container but uses same data directory which host PostgreSQL service is using.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 2 '17 at 7:59









                    HrishiHrishi

                    1777




                    1777







                    • 1





                      This will probably cause write conflicts with a running host PostgreSQL service

                      – Petrus Theron
                      Apr 4 '17 at 9:37











                    • I think stopping host service will solve the problem in that case.

                      – Hrishi
                      Apr 6 '17 at 6:19












                    • 1





                      This will probably cause write conflicts with a running host PostgreSQL service

                      – Petrus Theron
                      Apr 4 '17 at 9:37











                    • I think stopping host service will solve the problem in that case.

                      – Hrishi
                      Apr 6 '17 at 6:19







                    1




                    1





                    This will probably cause write conflicts with a running host PostgreSQL service

                    – Petrus Theron
                    Apr 4 '17 at 9:37





                    This will probably cause write conflicts with a running host PostgreSQL service

                    – Petrus Theron
                    Apr 4 '17 at 9:37













                    I think stopping host service will solve the problem in that case.

                    – Hrishi
                    Apr 6 '17 at 6:19





                    I think stopping host service will solve the problem in that case.

                    – Hrishi
                    Apr 6 '17 at 6:19

















                    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%2f31249112%2fallow-docker-container-to-connect-to-a-local-host-postgres-database%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript