0

I have two docker containers running:

Postgres 9.4: 0.0.0.0:5431->5432/tcp
Postgres 11: 0.0.0.0:5432->5432/tcp

I can connect to both of them via clients on my computer, but I am unable to access one container from within another. I would like to use Postgres's dblink feature - i.e. (in case it matters), I am running a query on one server that looks like this:

select
dp.*
from dblink(
    'dbname=mydb
    port=5431
    host=localhost
    user=postgres
    password=password', 'select 1'
) as dp (i int)

This results in an error along that says:

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 5431?

And I guess this is because localhost is the loopback address inside the container. What is the way around this? (I'm super new to Docker by the way)

1

You are correct in that using localhost within the container is referencing the container, and not your network. You could use inspect, find the ip, and hardcode the ip. However, a simpler and cleaner solution would be to utilize Docker's network features. When you run the containers you can put the containers inside a defined network. Containers in this network can resolve requests to other containers using their container name instead of an ip (e.g. Server=container2).

Docker Network Documentation

0

Ah. Well a high level answer that allows for me to just 'get by' for now is this:

Docker will automatically create a network when running one or more containers. You can check what a container's IP address is on this virtual network by running:

docker container inspect <container name>

I was able to find the gateway address of the postgres container that I want to query from and use it that way.

0

you can use

"docker network create "

to create your own network. Once you do that when you are creating a container use the

"--network "

to create the 2 containers in the same network.The containers in the same network can interact with each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.