When I run Curl to localhost:8080 it cannot communicate

Asked

Viewed 1,334 times

0

When I run Curl to localhost:8080 it cannot communicate

        $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);

The code above belongs to: localhost:8000

Where I’m trying to communicate with: localhost:8080, For he gives the following error:

Failed to connect to localhost port 8080: Connection refused

I’m using Docker to generate my containers (I don’t know if this information will help)

What is going on? What is the solution to this problem?

  • 1

    Are you sure you are using the server on this port, 8080? The standard for web applications tends to be 80 or 443, for example. Try using 127.0.0.1 instead of localhost. That doesn’t sound like a CURL mistake. If you are trying to connect with another external server also see if you have the door open, for example in Iptables, but this is not the case!

  • I’ll take a look, I think it was lack of attention because the Docker you open a door in your machine and one in the container. I’ll check this question better.

  • Hi, try adding curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

  • I think I found a possible problem. Because I’m using Docker and making PHP connect with another application, therefore PHP is inside Docker, right. But since I didn’t find the container I want to communicate with, it’s like it doesn’t exist. 'Cause I’m accessing from inside the container, but I’ll take a closer look. I just thought of it for now

1 answer

0

From what you have said, you run 2 separate containers and need one to access the other via the Docker network. I will try to help you with this.

Ambience

  • Container: PHP stack
  • Container: 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) php script. (will run in the container)

<?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 resource made available by container)

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 the container was able to access the container.

Explanation

The Docker-Compose is a Docker command that describes multi-container services and then facilitates your orchestration and management.

In the archive Docker-Compose.yml we are declaring the 2 services as you requested. These services will run using the images php:cli and httpd that are distributed through Docker’s public record http://hub.docker.com. They have been configured to run PHP scripts on php:cli and to serve files through the HTTP protocol using apache2 on httpd. When httpd image was created, it was configured to serve /usr/local/apache2/htdocs through door 80.

We include the other 2 files in their respective containers through the volumes clause: In the container will appear mounted in the folder /app the file php script., while in container the file index.html appear in the folder /usr/local/apache2/htdocs which is the apache2 pattern in this image.

We also defined that the two containers will be in the same network default and define the initial container command as php /app/script.php. This way the script will be executed and the output will be exposed in the standard container output.

Moreover, in the Docker-Compose.yml We expose the container to the host computer. We’ve connected port 80 of the container to port 8080 of the host so you can make sure apache is ok. For this just access through your browser the localhost:8080 path. The data will be redirected to conteinerB:80. Even if this port exposure was not done, direct container accessThe container by PHP would still work, because the two containers already have a network for them and do not depend on the host for anything!

Let’s magic

When executing the command Docker Compose up Docker creates a network for containers, assigns container-specific IP network access (which can be verified by the apache log that displays the 192.168.16.3 ip trying to access it) and runs each of them separately. This means that after a few seconds the container starts serving the index.html file from port 80, and in container A, the script runs until it gets the data from the container through the PHP Curl library.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.