Linux : Docker configuration - accessing PostgreSQL server on the host

This page last changed on May 01, 2015 by admin.

Context

Docker version 1.5.0

Get docker container info

docker inspect <container-name>

Docker delivers a huge chunk of config. For us now the relevant part is the networking:

 "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.7",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "fe80::42:acff:fe11:7",
        "LinkLocalIPv6PrefixLen": 64,
        "MacAddress": "02:42:ac:11:00:07",
        "PortMapping": null,
        "Ports": {
            "8080/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8091"
                }
            ]
        }
    }

Access PostgreSQL server on host from container

Listen to all IP addresses - in postgresql.conf:

listen_addresses = '*'

Allow connections from docker container - in pg_hba.conf add

host   all    all 172.17.0.0/16   trust

Make docker play nice with ufw, in /etc/default/ufw adjust the forward policy (see http://docs.docker.com/installation/ubuntulinux/#enable-ufw-forwarding)

DEFAULT_FORWARD_POLICY="ACCEPT"

And open up a port for the docker container and allow the bridge:

sudo ufw allow from 172.17.42.1 to any port 5432
sudo ufw allow in on docker0