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

uniqueginun's avatar

Docker run two Laravel Projects

Hello everyone,

I'm new to docker and I set up docker for two projects in my local machine. the first project is a website that consumes the second project which is Laravel API.

docker-compose.yml nginx setup for the website project

services:
    nginx:
        image: nginx:stable-alpine
        ports:
            - "80:80"

docker-compose.yml nginx setup for the API project

services:
    nginx:
        image: nginx:stable-alpine
        ports:
            - "8001:80"

I perform HTTP request from website to the API but it gives me this error:

cURL error 7: Failed to connect to localhost port 8001: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://localhost:8001/api/users/3453

But the weird part is when I copy this url http://localhost:8001/api/users/3453 and paste it in the browser it works fine. which means the port not working only inside my docker container.

0 likes
3 replies
neilstee's avatar

@uniqueginun that's the nature of docker, it automatically creates a new network for every docker-composer.yml (at least from what I understand)

If you want 2 separate docker setup to see each other, you need to configure a network like this:

Website:

network:
	mynetwork:

services:
    nginx:
        image: nginx:stable-alpine
        ports:
            - "80:80"
		network:
			mynetwork:

API:


services:
    nginx:
        image: nginx:stable-alpine
        ports:
            - "80:80"
		network:
			mynetwork:

Something like that, bear in mind that you need to run your Website docker first so the mynetwork is created. and by the time you run your API, it will share to that network.

Another thing is after running your Website docker, docker somehow appends your project name to mynetwork network name so it will become website-mynetwork (I can't remember exactly). Maybe after running your Website docker, run docker network ls on the terminal to see what network name is generated.

Please or to participate in this conversation.