Docker Community Forums

Share and learn in the Docker community.

I can't access a container from another container

beta

(Samir Sabri) #1

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


(David Maze) #2

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 https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/ ? 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.


(Samir Sabri) #3

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


(2strokemaster) #4

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


(Vakz) #5

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”.


(Somashekar) #6

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


(Cmonspqr) #7

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