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

MPesic's avatar
Level 10

Two Laravel projects with sail on hitting the same db problem

So I managed to set the docker compose file on both projects to read/write in the same db by configuring docker-compose.yml files like this.

project1

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
            - shared
    mariadb:
        image: 'mariadb:10'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmariadb:/var/lib/mysql'
        networks:
            - shared
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
networks:
    sail:
        driver: bridge
    shared:
        external:
            name: shared
volumes:
    sailmariadb:
        driver: local

project2:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
            - shared
networks:
    sail:
        driver: bridge
    shared:
        external:
            name: shared

and I created network called shared in docker networks. Bare in mind that on a project2 I added APP_PORT=81 so it doesn't get any conflicts

Started migration and seeds. added dd('project1') and dd('project2') on both project api.php routes file and when I test in postman or browser by sending request to localhost:80 or localhost:81 it displays correctly. Also I compared in both projects via tinker are they reading from the same database, and results were the same, so they are reading from the same db.

I remove the dd's and run sail artisan passport:install command on project2 and copy the secrets to .env it went well, still can open the localhost:80 and 81. And now here comes the problem.

When I send a request on postman after that to login endpoint it runs to infinity. I stop the request and when I try to again test localhost:81 I can't open it anymore, it runs to infinity. Even when I do sail down on both projects and try to up it again, same problem.

And I know that projects code isn't a problem since I run it on remote server. Any ideas what should I do next to make it work.

0 likes
9 replies
bugsysha's avatar

Don't use the same database in development. I don't see any good reason why would you.

MPesic's avatar
Level 10

@bugsysha I need because I have 3 (third project not connected yet) services that read and manipulating with same data in different way.

bugsysha's avatar

@MPesic best advice I can give you develop your code based on testing and not development database usage.

bugsysha's avatar

@franciscocaldeira where did you get that stupid idea that I'm judging? I did the same mistakes so I'm not all of a sudden the smartest person in the world just because I wrote a simple comment that nobody understands.

MPesic's avatar
Level 10

@bugsysha Because that two or more projects will be hosted on different servers and will have completely different logic for everything. But thats not the subject of this thread.

bugsysha's avatar

@MPesic I'm trying to understand what is the point of what you wrote. Can you clarify?

Patryyyck's avatar

Hi @MPesic,

Maybe you can try this!

In your project1 docker-compose file, you can replace the network section by:

networks:
    sail:
        driver: bridge
        name: project1_network

In your project2 docker-compose file, you can replace the network section by:

networks:
    sail:
        driver: bridge
    shared:
        external:
            name: project1_network

Regards!

ManalLiaquat's avatar

I tried the given solution but didn't work for me, after spending a couple of hours.

I found out that we need to run both projects under one network of docker, and to do this I just add COMPOSE_PROJECT_NAME=myprettyproject in both projects' env and remove the DB service from the docker-compose.yml file from project-2.

DB ENVs will be the same for both projects.

Please or to participate in this conversation.