连接postgreSQL+;来自我的本地网络的postGIS docker容器

连接postgreSQL+;来自我的本地网络的postGIS docker容器,postgresql,docker,ip,postgis,remote-access,Postgresql,Docker,Ip,Postgis,Remote Access,我已使用以下内容构建了docker容器: 检查: $ docker inspect b89c7f54e76a [ { "Id": "b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f", "Created": "2017-08-08T14:34:02.375576229Z", "Path": "docker-entrypoint.sh", "Arg

我已使用以下内容构建了docker容器:

检查:

$ docker inspect b89c7f54e76a
[
    {
        "Id": "b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f",
        "Created": "2017-08-08T14:34:02.375576229Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "postgres"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 8293,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-08-09T10:08:11.317833478Z",
            "FinishedAt": "2017-08-08T18:18:17.110547652Z"
        },
        "Image": "sha256:1c5da6633b90bfb50486b543dd11c7efb800e6d27d338d51aaf82e929d83bde2",
        "ResolvConfPath": "/var/lib/docker/containers/b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f/hostname",
        "HostsPath": "/var/lib/docker/containers/b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f/hosts",
        "LogPath": "/var/lib/docker/containers/b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f/b89c7f54e76aa82bd142e1eb45a1f12008bf2ee2f94e392b08b386493b58891f-json.log",
        "Name": "/psql-futurSahel",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": null,
            "Name": "aufs"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "bb59bb5d17ad6b3ca59d7acf5c21936ce19fb15ec9fa13388ce63c01ed559f37",
                "Source": "/var/lib/docker/volumes/bb59bb5d17ad6b3ca59d7acf5c21936ce19fb15ec9fa13388ce63c01ed559f37/_data",
                "Destination": "/var/lib/postgresql/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "b89c7f54e76a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5432/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "POSTGRES_PASSWORD=postgres",
                "PATH=/usr/lib/postgresql/9.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.7",
                "LANG=en_US.utf8",
                "PG_MAJOR=9.6",
                "PG_VERSION=9.6.3-1.pgdg80+1",
                "PGDATA=/var/lib/postgresql/data",
                "POSTGIS_MAJOR=2.3",
                "POSTGIS_VERSION=2.3.2+dfsg-1~exp2.pgdg80+1"
            ],
            "Cmd": [
                "postgres"
            ],
            "ArgsEscaped": true,
            "Image": "mdillon/postgis",
            "Volumes": {
                "/var/lib/postgresql/data": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "07536f68477b1f345cff2caa3118d7ee9251b0a84a2ef6a1f641c24aecd1ae53",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5432/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/07536f68477b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "98ca66036295516d5d653b865f3bf4de976b1f8fc704269ef5d7d067817a8c4d",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1f95d91f247bb8cb99e8eb5c1d3911b82812947880c9e2ba6ec4db3cdc8f131b",
                    "EndpointID": "98ca66036295516d5d653b865f3bf4de976b1f8fc704269ef5d7d067817a8c4d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
它就像我桌面上的一个魔咒,我可以使用psql与服务器交互。它工作得很好

psql -h 172.17.0.2 -p 5432 -U postgres
但我想让我的同事连接数据库,我不太了解它是如何工作的

此时(如果我们假设我的本地IP是
192.168.0.14

不起作用

我发现了一些链接,如:


但它仍然清晰可见

您需要做的是在运行容器时进行端口映射

docker run -p 5432:5432 --name some-postgis -e POSTGRES_PASSWORD=mysecretpassword -d mdillon/postgis

这将把主机上的5432(左侧)映射到容器上的5432(右侧)。只要您的IP可以从他的计算机访问,您的同事就可以访问DB

您的同事无法连接的原因是您正在使用主机对接内部IP地址,而您的同事计算机没有意识到172.17.0.2地址托管在您的计算机上

因此,正确的机制如@Tarun Lalwani所述


现在,您的同事将使用您的机器进行连接,即dockers公共ip地址的主机

我已经编辑了我的问题<代码>-p 5432:5432不等同于
-p“$POSTGRES\u PORT\u 5432\u TCP\u PORT”
?如果我执行
docker run-d-p5432:5432 postgres
操作,它不会创建另一个docker?该命令只是一个示例。根据您发布的命令更新了答案<代码>--link现在不推荐使用。您可能应该使用
docker compose
如何使用
docker compose
psql -h 192.168.0.14 -p 5432 -U postgres
docker run -p 5432:5432 --name some-postgis -e POSTGRES_PASSWORD=mysecretpassword -d mdillon/postgis