0

I have created a docker container. This database container is run successfully. I can connect to this container and see all details of database. Here is some configuration:

services:
  db:
    container_name: db
    image: postgres:latest
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: abc
      POSTGRES_PASSWORD: abc
      POSTGRES_DB: database_dev
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
    - postres_volume:/var/lib/postgresql/data/

Here is information I inspect from this running container:

"NetworkSettings": {
            "Bridge": "",
            "Ports": {
                "5432/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5432"
                    }
                ]
            },
            "Networks": {
                "medusa_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "74f7f48596e3",
                        "db"
                    ],
                    "NetworkID": "d48bbf5453a7c42facda69a8c2c741a20bc18d35238854c00f068bd19119afc4",
                    "EndpointID": "5587658ae084eba71e65fb4712bc75a0ea7c240b600d77d4b4722f5a91818d73",
                    "Gateway": "172.19.0.1",
                    "IPAddress": "172.19.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                }
            }

Based on those information, I try some postgres clients such as PgAdmin4 or Adminer and using those information:

host: 172.19.0.5 (get from inspect)
port: 5432          (get from inspect)
user name: abc (get from docker-compose config)
password: abc  (get from docker-compose config)
database name: abc_dev (get from docker-compose config)

But all clients cannot connect. I don't know what is it wrong here ? Please tell me.

1 Answer 1

1

The IP addresses you get from inspect are the addresses in Docker's internal network, not addresses you can see from outside of containers.

Since you're exposing the port in your compose file (the 5432:5432 setting under ports), you can simply connect to port 5432 on the host Docker is running the container. If you're trying to connect from the host Docker is on, localhost:5432 will work.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried with localhost or 0.0.0.0 before. But I received error something like "connection refuse". If I input ip in inspect, there will be a big waiting time (then fail). That is the main difference.
Just a note that the .Ports."5432/tcp".HostIp value is an external address, it's just normally 0.0.0.0 unless otherwise specified.
@TrầnKimDự immediate connection refused implies postgres or the proxy is not listening on the port. Check the logs for the db container

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.