postgresqldocker

PostgreSQL in Docker - access from host


I've been trying to get access to PostgreSQL running in a docker container. I'm able to connect to the PostgreSQL from within the adminer on localhost:8080, which is another container, but I'm not able to connect from my host machine, although I'm mapping the port 5432 to the host machine.

As a container's IP to connect to I've been using an IP address fetched from "docker inspect <postgre_container>" - usually 172.21.0.2/3.

This is what my docker-compose.yml looks like:

version: '3.1'

services:
  db:
    image: postgres:9.6.0
    restart: always
    environment:
      POSTGRES_PASSWORD: root
      POSTGRES_USER: root
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

volumes:
  pgdata:

I'll be glad for any hints, thanks


Solution

  • The IP address shown using docker inspect is the internal IP address within the container. Since you are using port mapping with external port 5432, you should access Postgres via localhost:5432 (e.g. 127.0.0.1:5432).