1

I am attempting to run both PostgreSQL and pgAdmin in Docker containers. The idea is that the PostgreSQL database should be accessible to any applications I have running on the host machine, and also to pgAdmin.

I am using this command to run PostgreSQL:

docker run -d -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password --name postgres -p 5432:5432 postgres

And to run pgAdmin:

docker run -d -p 1111:1111 --name pgadmin -e "PGADMIN_LISTEN_PORT=1111" -e "PGADMIN_DEFAULT_EMAIL=admin@test.com" -e "PGADMIN_DEFAULT_PASSWORD=test" dpage/pgadmin4

If I go to localhost:1111, I can connect to pgAdmin and login. However, when I try to connect to my local PostgreSQL instance, it gets no response.

Therefore, I tried to run pgAdmin with access to the host internet using --net=host instead of -p 1111:1111:

docker run -d --net=host --name pgadmin -e "PGADMIN_LISTEN_PORT=1111" -e "PGADMIN_DEFAULT_EMAIL=admin@test.com" -e "PGADMIN_DEFAULT_PASSWORD=test" dpage/pgadmin4

Now, when I try to go to localhost:1111 to connect to pgAdmin, I get no response in my browser.

Docker Compose is a possible solution, as I could link the two containers together so they could access each other without having to worry about ports, but I also need pgAdmin to be able to access PostgreSQL instances on other machines, as well as my local one.

I feel like --net=host is broken in Docker. There's a whole thread here with a lot of confusion.

My setup:

Host: Windows 10
Docker: Docker Desktop Community v2.0.0.3 (31259)

Update

I have now tried using --link postgres on the pgAdmin container and it allows me to connect to my local instance of PostgreSQL but not non-local ones, the full command is:

docker run -d -p 1111:1111 --link postgres --name pgadmin -e "PGADMIN_LISTEN_PORT=1111" -e "PGADMIN_DEFAULT_EMAIL=admin@test.com" -e "PGADMIN_DEFAULT_PASSWORD=test" dpage/pgadmin4
harvzor
  • 2,832
  • 1
  • 22
  • 40
  • I have the "same" problem last week https://stackoverflow.com/questions/55928586/problem-with-postgresql-and-pgadmin-docker-containers , Hope this help you. – Schwarz54 May 10 '19 at 08:53
  • @Schwarz54 The accepted answer on your post is a little bit useful, I have now found that using `--link postgres` will at least let pgAdmin access just my local instance of PostgreSQL but I also need it to be able to access any non-local ones too. – harvzor May 13 '19 at 07:47

2 Answers2

2

The commands were not wrong, only the connection in pgAdmin, so the full list of commands are:


For PostgreSQL:

docker run -d -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password --name postgres -p 5432:5432 postgres

And pgAdmin:

docker run -d -p 1111:1111 --name pgadmin -e "PGADMIN_LISTEN_PORT=1111" -e "PGADMIN_DEFAULT_EMAIL=admin@test.com" -e "PGADMIN_DEFAULT_PASSWORD=test" dpage/pgadmin4

Now the pgAdmin container won't connect to localhost but needs the IP of the PostgreSQL container. Run:

docker inspect postgres

Inspect result:

[
    {
        ...
        "NetworkSettings": {
            ...
            "Networks": {
                ...
                "IPAddress": "172.17.0.3",
                ...
            }
        }
    }
]

We're only interested in the IPAddress from the inspect command. This is the IP which pgAdmin should connect to.

pgAdmin is also capable of access external IPs from your machine.

harvzor
  • 2,832
  • 1
  • 22
  • 40
  • But the ips would change when we restart the container correct? How do we refer to the container in a more persistent manner? – hakish Aug 14 '20 at 10:23
  • @hakish I'd recommend using Docker Compose. Containers in the same yaml file can connect to each other using the name of the container (such as `postgres:5432`). I have a blogpost which is half finished you might want to look at: https://github.com/Harvzor/portfolio/blob/docker-blogpost/data/blog/docker.md#adding-pgadmin – harvzor Aug 18 '20 at 12:04
0

When you create docker containers it creates a bridge network. First find the network range for the bridge network. You can use ifconfig to find it. Let's say 172.17.0.5 is ip of pgAdmin, Then create a user 'root'@'172.17.0.5' for PostgreSQL and give database permissions for that user. Then you can connect to the database. Also check if port 3306 is accessible using telnet.