简体   繁体   中英

Accessing PostgreSQL on docker container from pgAdmin4 in another docker container

I have a PostgreSQL instance running on a docker with these commands:

mkdir -p $HOME/vols/postgres

docker pull postgres:12.0

docker run --rm   --name pg-docker -e POSTGRES_PASSWORD=docker \
    -v $HOME/vols/postgres:/var/lib/postgresql/data \
    -d -p 5432:5432  postgres

It's up and running and I can access it from DBeaver which is installed on my local-machine. Also, I've installed pgAdmin4 by these commands:

mkdir -p $HOME/vols/pgadmin4

docker pull dpage/pgadmin4

docker run --rm --name pgadmin4 -p 5050:80 \    
    -v $HOME/vols/pgadmin4:/var/lib/pgadmin \
    -e 'PGADMIN_DEFAULT_EMAIL=amiry@manexapp.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=12345678' \
    -d dpage/pgadmin4

The pgAdmin is also up and running well and I can easily access it and login to it through http://localhost:5050 .

But when I want to connect to my postgre-container instance via pgAdmin4-container instance, I get this error:

Unable to connect to server:

could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Address not available Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

Does anybody has an idea what's going wrong here please? Thanks in advance.

NOTE: My host machine is Fedora 31.

Inside a container, the loopback address ( localhost or 127.0.0.1) refers to "this container". When you try to connect to 127.0.0.1 inside the pgAdmin4 container, it fails because your Postgres service is not running inside the pgAdmin4 container.

The easiest way to make this work is to put both of your containers on a user defined network, in which case they can simply refer to each other by name.

Start by creating a network:

docker network create dbnet

Then launch the postgres container on that network:

docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker \
    --net dbnet \
    -v $HOME/vols/postgres:/var/lib/postgresql/data \
    -d -p 5432:5432  postgres

And finally launch the pgAdmin4 container on that network:

docker run --rm --name pgadmin4 -p 5050:80 \    
    --net dbnet \
    -v $HOME/vols/pgadmin4:/var/lib/pgadmin \
    -e 'PGADMIN_DEFAULT_EMAIL=amiry@manexapp.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=12345678' \
    -d dpage/pgadmin4

Now when you access your pgadmin ui, you can connect to the host pg-docker instead of localhost .

UPDATE: OK. larsks added a full detailed answer with creating a custom network approach, which makes more sense. I'll try it and let you know.


OK. I recognized there is a network problem and pgAdmin4-docker cannot see pg12-docker. So, I did these steps to solve the problem:

A. Stop pgadmin4 container by:

docker stop pgadmin4

This will also delete the container, because I have ran it by --rm flag.

B. Find the network-name and IP associated to pg12-docker by running:

docker inspect pg12-docker

This will echo a large JSON-file. Find the Networks node:

"Networks": {
    "bridge": {
        "Gateway": "172.17.0.1",
        "IPAddress": "172.17.0.2",
        // other lines omitted  
    }
}

You will see the network-name as the first child of JSON ( bridge in this example) and also the IPAddress . Note theme somewhere.

C. Re-run the pgAdmin4-docker again with --network [NetworkName] parameter; replace [NetworkName] with the name you got in previous step ( bridge in this example).

docker run --rm --name pgadmin4 -p 5050:80 --network bridge \    
    -v $HOME/vols/pgadmin4:/var/lib/pgadmin \
    -e 'PGADMIN_DEFAULT_EMAIL=amiry@manexapp.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=12345678' \
    -d dpage/pgadmin4

D. Access (or refresh) http://localhost:5050 and login to it. In Add server section, use the IPAddress you got in step B. and you are good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM