Reputation: 1
I'm working with docker on differents projects. I've a nginx conatainer and two php container : one for my projets in php5.6 and another for php7.0 project's.
I need to to make a cURL request between my two php container (from 5.6 to 7) but I have no idea of how make it. I've try somthing with the different host file but I only get a Connection timed out or a Connection refused.
curl -X GET 'hostUrl'
When the hostUrl is the php7 docker ip I get the Connection timed out and when hostUrl is 127.0.0.1 I get the Connection refused error.
They are in the same networks in my docker-compose.
What can I do to solve my problem ?
Thanks
Upvotes: 0
Views: 1888
Reputation: 1044
The name of your container is the host name of the container.
If you have containers named webphp5
and webphp7
. You can do something like this:
$ docker exec -it webphp5 bash #login into the container via console
$ curl -X GET 'webphp7'
This is curl the you webphp7
container provided that you have your ports that you are trying to access open.
Upvotes: 0
Reputation: 24116
If the two containers are under the same docker network; i.e. you have specified two containers on one docker-composer.yml file, then you should be able to talk to the other container simply using the hostname of the container.
If the two containers are not on the same docker network, you can have both container expose their private port and communicate via the host machine.
For e.g. if you have a container "A" running on port 9000 and container "B" running on port 8000; and you want to curl into container "A" from "B", then you can do this:
<?php
// Get the docker host machine's ip:
$host_ip = exec("/sbin/ip route|awk '/default/ { print $3 }'");
// Now curl
// $container_a_url = "http://{$host_ip}:9000"
Upvotes: 1