I'm using Laravel Sail, and this is my Docker Compose file. I need to connect Laravel to an external network, but I want the database to be accessible only within the container for the Laravel application because my local ports for the PostgreSQL database are conflicting. When I build and run this with sail artisan migrate, I get an error stating that it cannot connect to the database.
services:
laravel.test:
build:
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.3/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
DB_CONNECTION: '${DB_CONNECTION}'
volumes:
- '.:/var/www/html'
networks:
docker-alpine_kontentino:
aliases:
- social-login-service.betakontentino.com
socialLoginSail:
depends_on:
- pgsql
pgsql:
image: 'postgres:14'
ports:
- '${FORWARD_DB_PORT:-5544}:5432'
environment:
PGPASSWORD: '${DB_PASSWORD:-secret}'
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
volumes:
- 'sail-pgsql:/var/lib/postgresql/data'
- './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql'
networks:
- socialLoginSail
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}" ]
retries: 3
timeout: 5s
networks:
socialLoginSail:
driver: bridge
docker-alpine_kontentino:
external: true
volumes:
sail-pgsql:
driver: local
This is mu local .env for database:
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=social-login
DB_USERNAME=admin
DB_PASSWORD=password
When I change the local port in docker-compose, Laravel cannot connect to the database.