Connecting to postgres instance that is running via docker on my ubuntu server

How can I connect to my postgresql instance when I am running it via docker.

In my docker-compose I have the image defined as follows:

    image: postgres:9.4
    #container_name: db
      - "/home/data/pgdata:/var/lib/postgresql/data"
    restart: always

I installed the client only:

sudo apt-get install -y postgresql-client

I installed the psql client on ubuntu, and I try to connect to it but can’t seem to connect successfuly.

psql -h db -p 5432 -U postgres

psql: could not translate host name
“db” to address: Temporary failure in name resolution

I tried different hosts like localhost, and docker_db_1 and they all didn’t work.

Source: StackOverflow