Docker container for Postgres 9.1 not exposing port 5432 to host

前端 未结 7 1427
醉酒成梦
醉酒成梦 2020-12-23 20:18

I\'m trying to use a Docker container to run a PostgreSQL server, and connect with it from my host machine.

My configuration is:

  • Host machine: Mac OS X
相关标签:
7条回答
  • 2020-12-23 20:28

    It's 2018 and I just had a similar problem. The solution for me seemed to be with the order of props to docker. e.g. this resulted in no port being exposed;

    docker run -d --name posttest postgres:alpine -e POSTGRES_PASSWORD=fred -p 5432:5432

    while this worked fine (image exposed port 5432 as expected);

    docker run --name posttest -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine

    0 讨论(0)
  • 2020-12-23 20:34

    Your docker host is a virtual machine, which has it's own IP affffdres. You can detect this IP address by entering the following command:

    docker-machine ip
    

    The answer will be something like 192.168.99.100

    When you have mapped the ports using the -p 5432:5432 switch, you will be able to connect to postgres with any tool from your dev machine using the IP address mentioned.

    0 讨论(0)
  • 2020-12-23 20:34

    I had a similar issue. My problem was simply 0.0.0.0 not mapping to localhost so I just had to add to psql

    psql --host=0.0.0.0
    

    This is presuming

    docker port <container name>
    

    outputs

    5432/tcp -> 0.0.0.0:5432
    
    0 讨论(0)
  • 2020-12-23 20:34

    Other answers work, but don't explain why they work.

    Given the command:

    psql -h localhost -p 5432:5432 -U postgres
    

    localhost is actually a special value that tells psql to look for a unix socket connection, instead of going over TCP. We can't use unix sockets to connect to docker services.

    Changing the command like so fixes it, by forcing TCP:

    psql -h 127.0.0.1 -p 5432:5432 -U postgres
    

    That will work as long as you docker run ... -p 5432:5432 .... Specifying the IP returned by docker-machine ip also forces TCP, so that also works.

    0 讨论(0)
  • 2020-12-23 20:36

    I was able to connect using container IP or host IP, except localhost (127.0.0.1).

    To get container id run

    docker ps
    

    Find required container id and run

    docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>
    

    Port must be exposed.

    Here is an example of docker-compose.yml which starts two containers postgres and adminer, which is database management tool you can use to connect to postgres:

    version: '3'
    services:
      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
      postgres:
        image: postgres:11.4-alpine
        restart: always
        ports:
          - "5432:5432"
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
    
    0 讨论(0)
  • 2020-12-23 20:39

    I had a similar problem working in a VMWare virtual machine with Lubuntu. The VM had been paused and then was restarted. The PostgreSQL Docker container was correctly mapped and listening on localhost:5432, but I always got:

    psql: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
    

    Restarting the VM solved the problem in my case.

    0 讨论(0)
提交回复
热议问题