I can't access a container from another container

Expected behavior

Connect to postgres container by providing the host=localhost

Actual behavior

It does not connect:
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host “localhost” (::1) and accepting
TCP/IP connections on port 54320?
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 54320?

Information

  • the output of:
    • pinata diagnose -u on OSX
      -bash: pinata: command not found

Steps to reproduce the behavior

Just try to access a db container from another container providing host=localhost

What shall I put in host instead of localhost? Notice that I can connect to the DB container from the host it self, but not from within a container

localhost (127.0.0.1, ::1) always refers to the current container, and will never refer to anything in a different container (without unusual setup).

Have you read Legacy container links | Docker Docs ? That discusses the issue in some detail, and while it prominently uses the word “legacy”, it does describe the default out-of-the-box Docker setup.

1 Like

Thank you, by luck, I have tried to use the internal IP address using iconfig, en1 entry, and it worked :slight_smile:

@hopewise, how did you exactly manage it to run?

Can’t speak for him exactly (and you’ve probably already solved it, but might be interesting for someone else), but personally I got it to run by trying to connect to the name of the service I was connecting to. I have postgres running in a service called “db”, so I connect to “db” rather than “localhost”.

@vakz we need enter only service name or need enter servicename:port ?

If everything runs on the default ports you do not need to specify the port. However make sure that the port is exposed by the container you want to connect to