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
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)
@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.
@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.
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).
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.
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.
@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.