Docker container for Postgres 9.1 not exposing port 5432 to host

前端 未结 7 1469
醉酒成梦
醉酒成梦 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: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}}' 
    

    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
    

提交回复
热议问题