I am used to having just one Laravel Sail project on my machine which has been fine and I am used to using DDEV for Craft CMS project environments where each container feels more self-contained. I am now at a point where I have tried to set up a second Laravel project locally that I can run with Sail but it seems like I can't have two different database environments or something? Maybe Sail environments share the same database connection if they both use MySQL 8?
I am not trying to run the two projects simultaneously but it seems like when MySQL starts up, it loads the database for the wrong project and doesn't create the database for the new project and I don't have permission to create it manually.
I am not a Docker config expert so if someone could help me to understand how I need to set things up so that I can have two projects that each have their own database with Sail, I'd appreciate the help.
I am used to having just one Laravel Sail project on my machine which has been fine and I am used to using DDEV for Craft CMS project environments where each container feels more self-contained. I am now at a point where I have tried to set up a second Laravel project locally that I can run with Sail but it seems like I can't have two different database environments or something? Maybe Sail environments share the same database connection if they both use MySQL 8?
I am not trying to run the two projects simultaneously but it seems like when MySQL starts up, it loads the database for the wrong project and doesn't create the database for the new project and I don't have permission to create it manually.
I am not a Docker config expert so if someone could help me to understand how I need to set things up so that I can have two projects that each have their own database with Sail, I'd appreciate the help.
Share Improve this question asked Feb 17 at 16:36 Benjamin KohlBenjamin Kohl 5325 silver badges6 bronze badges1 Answer
Reset to default 0It sounds like both are hitting the same ports.
In you first project, try setting the docker-compose.yml
vars like;
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.3
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
NODE_VERSION: '22'
image: sail-8.3/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-8080}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
- '${XDEBUG_PORT:-9003}:${XDEBUG_PORT:-9003}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
IGNITION_LOCAL_SITES_PATH: '${PWD}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- minio
- mailpit
mysql:
image: 'mysql/mysql-server:8.0'
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
And in the other project, the dockerfile;
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.3
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.3/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '8010:80'
- '${VITE_PORT:-5174}:${VITE_PORT:-5174}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
IGNITION_LOCAL_SITES_PATH: '${PWD}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- mailpit
- minio
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3307}: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
You can control this via the .env
by setting the repective values of (any any other clashing ports), e.g in first project
APP_PORT=8080
FORWARD_DB_PORT=3306
so access the first on http://localhost:8080
and second
APP_PORT=8081
FORWARD_DB_PORT=3307
so access the first on http://localhost:8081