From what you said, you run 2 distinct containers and need one to access the other through the docker network. I'll try to help you with that.
Environment
- ConteinerA: PHP stack
- ConteinerB: WEB server
Desired Action: Container A requests in Container B through PHP's cURL library to access http content.
Solution
As an example, take the following files created in the same empty folder:
1) docker-compose.yml
version: '3'
services:
conteinerA:
image: php:cli
command: php /app/script.php
volumes:
- ./script.php:/app/script.php
networks:
- default
conteinerB:
image: httpd
ports:
- 8080:80
volumes:
- ./index.html:/usr/local/apache2/htdocs/index.html
networks:
- default
2) script.php (will run on containerA)
<?php
sleep(10); #aguardar o outro conteiner iniciar
$url = "http://conteinerB/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
echo $res;
?>
3) index.html (will be the feature made available by containerB)
Informacao vinda do ConteinerB
Navigate to the folder where the 3 files are and run the command:
docker-compose up
At the end of the terminal output you will see something like:
conteinerA_1 | Informacao vinda do ConteinerB
conteinerB_1 | 192.168.16.3 - - [30/Jan/2018:19:23:40 +0000] "POST / HTTP/1.1" 200 33
This means that containerA has been able to access container B.
Explanation
docker-compose is a docker command that describes multi-container services and then makes it easier to orchestrate and manage.
In the file docker-compose.yml we are declaring the 2 services as you requested. These services will run using the php: cli and link images that are distributed through the public Docker registry link . They have been configured to run PHP scripts in php: cli and to serve files over the HTTP protocol using apache2 on link . When the httpd image was created, it was configured to serve the / usr / local / apache2 / htdocs files via port 80.
We include the other 2 files in their respective containers through the volumes clause: In the containerA will appear mounted in the / app folder the script.php file, whereas in container B file index.html will appear in the / usr / local / apache2 / htdocs folder which is the default for apache2 in this image.
We have also defined that the two containers will be on the same default network and set the initial containerA command to php /app/script.php . This way the script will run and the output will be exposed to the standard output of the container.
In addition, in docker-compose.yml we expose the B container to the host computer. We connect port 80 from the B container to port 8080 of the host so you can make sure apache is ok. To do this, simply access your localhost: 8080 through your browser. Data will be redirected to container B: 80. Even if this port exposure were not made, direct access from containerA to containerB by PHP would continue to work because the two containers already have a network for them and do not depend on the host at all!
Let the magic
When executing the docker compose up command, docker creates a network for the containers, assigns network access with specific IPs to the containers (which can be seen by the apache log that displays ip 192.168 .16.3 trying to access it) and execute each separately. This means that after a few seconds the container B begins serving the index.html file over port 80, and in container A, the script runs until it gets the data from the container B through the PHP cURL library.