1

My site has a separate admin project. Both connect to the same database.

How do I get this working locally so when I run sail up in the admin project it does not try to create a new container for the database. I want to connect to the same database that my main application connects to. I do not need both projects to run at the same time locally.

Adding both docker.compose.yml to make it clearer. Main:

version: '3'
services:
    laravel.test:
        build:
            context: ./docker/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
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - selenium
    mysql:
        image: 'mysql:8.0'
        cap_add:
            - SYS_NICE 
        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:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
    selenium:
        image: 'selenium/standalone-chrome'
        volumes:
            - '/dev/shm:/dev/shm'
        ports:
            - '${FORWARD_SELENIUM_PORT:-4444}:4444'
            - '${FORWARD_SELENIUM_HEADFUL_PORT:-7900}:7900'
        networks:
            - sail

networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local

Second:

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'
        cap_add:
            - SYS_NICE
        ports:
            - 81:80 #'${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    # mysql:
    #     image: 'mysql:8.0'
    #     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:
    #         - 'sailmysql:/var/lib/mysql'
    #     networks:
    #         - sail
    #     healthcheck:
    #       test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
    #       retries: 3
    #       timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
BobB
  • 750
  • 1
  • 9
  • 22
  • The first `docker-compose.yml` leave it as default sail. On the second you remove the mysql service, but keep the networks and volumes. On second project put the `DB_HOST=host.docker.internal` in `.env`. – francisco May 20 '22 at 14:41
  • Connection refused so looks like the first mysql is not running. I also had to remove the depends_on in the second project. Both projects default to Laravel.test as the service name which I think may be the issue. @francisco could you share the second docker.compose so i can compare? – BobB May 21 '22 at 23:09

2 Answers2

2

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.

Manal Liaquat
  • 229
  • 2
  • 7
0

To set up 2 projects using same local DB, the 1º project, it's sail default with some custom changes, explained in comments. My .envhas DB_HOST=mysql.

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/7.4 #change here for php 7.4
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-7.4/app #change here for php 7.4
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - 81:80 #'${APP_PORT:-80}:80' # I use the port 82 for the 1º project
        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
        depends_on:
            - mysql
    mysql:
        image: 'mariadb:10.5.15' #I use this version, but you can use other.
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: "%"
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" ]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

(This creates container with DB and PHP).

On my 2ºproject, I did this docker-compose.yml.

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/7.4
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-7.4/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - 82:80 #'${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
        depends_on:
            - mysql
    #mysql:
    #    image: 'mariadb:10.5.15'
    #    ports:
    #        - '${FORWARD_DB_PORT:-3306}:3306'
    #    environment:
    #        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
    #        MYSQL_ROOT_HOST: "%"
    #        MYSQL_DATABASE: '${DB_DATABASE}'
    #        MYSQL_USER: '${DB_USERNAME}'
    #        MYSQL_PASSWORD: '${DB_PASSWORD}'
    #        MYSQL_ALLOW_EMPTY_PASSWORD: 1
    #    volumes:
    #        - 'sail-mysql:/var/lib/mysql'
    #        - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
    #    networks:
    #        - sail
    #    healthcheck:
    #        test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" ]
    #        retries: 3
    #        timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

In here, I just commented the service for db. The .env is DB_HOST=host.docker.internal.

francisco
  • 1,387
  • 2
  • 12
  • 23
  • Thanks for the details. I copied what you have include the extra_hosts which was not in either of my original docker.compose but I still get 'ERROR: Service 'laravel.test' depends on service 'mysql' which is undefined' when I 'sail up' in the second project – BobB May 23 '22 at 20:34
  • @BobB on docker desktop, the container of service "mysql" is green? – francisco May 24 '22 at 22:15
  • yes when I start the main project mysql service from docker desktop it is green but still sail up says the mysql dependency cannot be found. Updated my original question with my docker.compose for both projects in case you see something I am not. Second project .env also has your modification.Thanks for looking at this. – BobB May 25 '22 at 19:48
  • have you tried `docker compose up` instead of `sail up`? but delete the volumes previous created – francisco May 25 '22 at 21:09