Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davorminchorov's avatar

GuzzleHttp \ Exception \ ConnectException cURL error 7: Failed to connect to localhost port 8087: Connection refused

Hey, I am learning how to use the Guzzlephp/guzzle package and query a local API on localhost:8087. I can use curl on the command line and it returns the result normally but when I try it with Guzzle, it says

GuzzleHttp \ Exception \ ConnectException
cURL error 7: Failed to connect to localhost port 8087: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

My application is hosted using nginx (Docker container) and I am using Windows (virtualbox port forwarding, localhost:8893) The API is locally (host machine) on Windows (localhost: 8087)

Here's a code example:

$guzzle = new GuzzleHttp\Client([
        'base_uri' => 'http://localhost:8087',
        'headers' => [
            'Accept' => 'application/json; charset=utf-8'
        ]
    ]);

Anyone has any idea what's the problem?

0 likes
14 replies
renedekat's avatar

@Ruffles The problem is that you can connect to port 8087 on localhost from your host machine, but when you use Guzzle running in a VM, or this case Docker, localhost is the localhost of the Docker container, not your localhost of the host machine.

3 likes
davorminchorov's avatar

@bashy No, I cURL that URL from my windows terminal and it works fine but I am having problems hitting it using guzzle, because my API is locally on my Windows PC and not on the Linux VM.

Edit: I just tried your example from within my docker container and I got the same error: Connection Refused

@renedekat yeah I thought about that so now I have to figure out how to cURL the windows version of localhost from the VM / Docker container.

bashy's avatar

Yup so you need to forward the port into the VM I think. Won't work with localhost since that resolves to whatever is default in /etc/hosts (normally 127.0.0.1 pointing to the loopback interface).

1 like
davorminchorov's avatar

OK, so I tried port forwarding but I am guessing that's only one way (Linux -> Windows) and it didn't work. I read about bridged adapter option in Virtualbox but I failed with that option, so I ended up installing the whole software with the API on my Linux VM. I opened the api in the browser (using port forwarding) and it seems to work but when I tried to cURL the url above from the VM's terminal, it returned the same problem: Connection refused.

Edit: I tried from the Linux VM's console to use curl and it worked but what I said before this edit was that I tried using curl from the docker container and it didn't work.

davorminchorov's avatar
davorminchorov
OP
Best Answer
Level 53

I figured out a way how to query the API on Windows directly from the container using Guzzle. I just changed the IP address from localhost:8087 to my windows IP address 192.168.x.x:8087 and it worked! There was no need to do anything with linux ports / docker ports etc.

Thanks for the help anyway!

19 likes
dwoodard's avatar

I've been on this for hours++ now!!! Thank you so much!!!

The gotcha here was even though I can call the localhost in the browser https://localhost/api/whatever

if I tried to curl that call it would throw a curl 7

localhost to [local IP] worked for me

So many thanks!

elude's avatar

@Ruffles I fucking love you dude, fk me I was killing myself over this for 2 days, I'm so dumb! It's so obvious now in hindsight... lol Thanks for reporting your solution for this.

felipemeddeiros's avatar

After some time I fixed my problem adding a name alias to my nginx container on docker

#Nginx Service
  nginx:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../:/var/www
      - ./nginx/sites/:/etc/nginx/conf.d/
      - ./nginx/log/:/var/log/nginx/
    depends_on:
      - php-fpm
      - mysql
    networks:
      app-network:
        aliases:
          - api.local

my api url is "http://api.local"

2 likes
Tumtum's avatar

Instead of your machines IP, from Docker 18.03+ you can use host.docker.internal instead of localhost to not depend an a specific IP

1 like

Please or to participate in this conversation.