Reputation: 1705
I have one running docker which is for postgres running as below.
$ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
505ab3ffc4b1 postgres "docker-entrypoint..." 26 minutes ago Up 26 minutes 0.0.0.0:5432->5432/tcp posttest
I have another docker, in which a python file is running and trying to connect to postgres which is connecting to localhost:5432 and it failed by following error:
$sudo docker run test
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?
Inside my test Dockerfile, it is connecting to localhost:5432, How can I connect it to another dockers ip and port?
I am new to docker,so please help
Upvotes: 0
Views: 329
Reputation: 30841
The recommended approach to make containers interact with each other on one machine (not clustered) is by using a user defined bridge network. Here you can read about the possible network drivers. Here you can read about the difference of the default bridge network and the user defined bridge network.
So create your user defined docker bridge network.
$ docker network create my-bridge-net
We will deploy our 2 containers (python and DB) inside this network. First deploy the postgres container. I don't need to map the port on localhost (= I don't need to make the container available from outside my server)
$ docker run -d --net my-bridge-net --name my-postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password postgres
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96acfe130efa postgres "docker-entrypoint.s…" 2 seconds ago Up 1 second 5432/tcp my-postgres
The postgres container is running. I'll just check the IP of the container:
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-postgres
172.20.0.2
Now your python container needs to run inside the same network:
$ docker run -d --net my-bridge-net ... image
This container can communicate with the postgres container using the container name + port. (not localhost because localhost will point to the localhost inside the container instead of the localhost of your server).
I will replace your python app in this example with a basic ubuntu
container.
$ docker run -d --net my-bridge-net -it ubuntu /bin/bash
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5610fe02d847 ubuntu "/bin/bash" 22 seconds ago Up 21 seconds thirsty_neumann
96acfe130efa postgres "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 5432/tcp my-postgres
Now I have 2 containers running in the same user defined bridge network.
I'm going to exec
inside the ubuntu container to prove I can access the my-postgres container.
I'm going inside the ubuntu container and install curl and network tools to prove I can access the my-postgres container.
$ docker exec -it 5610fe02d847 /bin/bash
root@5610fe02d847:/# apt-get update
root@5610fe02d847:/# apt-get install iputils-ping
root@5610fe02d847:/# apt-get install curl
root@5610fe02d847:/# ping my-postgres
PING my-postgres (172.20.0.2) 56(84) bytes of data.
64 bytes from my-postgres.my-bridge-net (172.20.0.2): icmp_seq=1 ttl=64 time=0.102 ms
64 bytes from my-postgres.my-bridge-net (172.20.0.2): icmp_seq=2 ttl=64 time=0.115 ms
64 bytes from my-postgres.my-bridge-net (172.20.0.2): icmp_seq=3 ttl=64 time=0.122 ms
root@5610fe02d847:/# curl my-postgres:80
curl: (7) Failed to connect to my-postgres port 80: Connection refused
root@5610fe02d847:/# curl my-postgres:5432
curl: (52) Empty reply from server
As you can see I'm able to ping the container of postgres (you'll see the same IP in the output as the IP of our postgres container). I'm also able to curl the container but port 80 is refused. container port 80 is closed for our postgres container. We can curl on port 5432 of the postgres container. This port is open. This port is open because of the EXPOSE 5432
command in the Dockerfile of postgres. There is no need to map port 5432 on the localhost of our server.
Upvotes: 1