You run sail up -d, type php artisan migrate, and get a red Connection refused wall of error. Even though Docker Desktop shows your database container is running and healthy, the application inside refuses to talk to it. This connection breakdown highlights a fundamental rule of containerized development: in Docker Compose, 127.0.0.1 refers to the container itself, not the network.
Configuring external services (databases, caches, message brokers) for a Laravel project can quickly lead to environment mismatch issues across teams. Sail solves this by treating your services as independent, reproducible containers, but you must configure their network connectivity, build requirements, and startup ordering correctly.
In this guide, we show you how to swap and configure databases (Postgres, MongoDB), wire up caching and brokers (Redis, RabbitMQ), and handle healthchecks and volume resets safely.
Navigation: All tools · Sail overview · Queues · Env & deploy · Troubleshooting
Table of contents
- Network and hostname rules
- Redis service (full example)
- Replace MySQL with PostgreSQL
- MongoDB + PHP extension in Sail
- RabbitMQ as a container
- Mailpit (SMTP dev)
- Meilisearch / Typesense (optional search)
- Healthchecks and startup order
- Named volumes and data reset
- Common Mistakes
- Self-Check Questions
Network and hostname rules
All application-side hosts in .env must be Compose service names (mysql, pgsql, redis, mongo), not 127.0.0.1. This is because PHP runs inside the laravel.test container and communicates via an isolated Docker network.
Note
Host Machine vs. Container Networking
From your host machine (IDE, terminal, DBeaver), you connect to databases via 127.0.0.1 using the forwarded port (e.g., FORWARD_DB_PORT). But inside the containers, services must address each other using their Compose service names.
Redis service (full example)
To add Redis to your Sail environment, define the service and mount a named volume.
services:
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
Register sail-redis under the top-level volumes: section and declare the dependency inside laravel.test:
services:
laravel.test:
depends_on:
redis:
condition: service_healthy
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
Run sail exec redis redis-cli ping to verify the connection.
Replace MySQL with PostgreSQL
If your production server uses PostgreSQL, running MySQL locally is an unnecessary risk.
- Comment out or delete the
mysqlservice and its associated volume indocker-compose.yml. - Add the
pgsqlservice block using the standard Sail stub:
services:
pgsql:
image: 'postgres:15-alpine'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
environment:
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD}'
volumes:
- 'sail-pgsql:/var/lib/postgresql/data'
networks:
- sail
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME} -d ${DB_DATABASE}"]
retries: 3
timeout: 5s
- Update the
depends_onarray oflaravel.testto point topgsql. - Update
.env:
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=sail
DB_PASSWORD=password
Rebuild and start the containers using sail build --no-cache && sail up -d.
MongoDB + PHP extension in Sail
MongoDB is not supported out-of-the-box by Sail's default runtimes and requires installing the PHP MongoDB extension inside the application container.
- Add the MongoDB service to your Compose file:
services:
mongo:
image: 'mongo:7'
ports:
- '${FORWARD_MONGO_PORT:-27017}:27017'
volumes:
- 'sail-mongo:/data/db'
networks:
- sail
- Register the volume
sail-mongoundervolumes:. - Publish the Sail Dockerfiles if you haven't already:
bash
php artisan sail:publish - Edit the runtime Dockerfile (e.g.,
docker/8.3/Dockerfile) to install the PECL extension:
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
- Rebuild your Sail images and start:
bash
sail build --no-cache sail up -d
RabbitMQ as a container
For projects requiring advanced AMQP features instead of Redis or database-backed queues, run RabbitMQ as a sidecar container.
services:
rabbitmq:
image: 'rabbitmq:3-management-alpine'
hostname: rabbitmq
ports:
- '${FORWARD_RABBITMQ_PORT:-5672}:5672'
- '${FORWARD_RABBITMQ_MANAGEMENT:-15672}:15672'
volumes:
- 'sail-rabbitmq:/var/lib/rabbitmq'
networks:
- sail
environment:
RABBITMQ_DEFAULT_USER: '${RABBITMQ_USER:-sail}'
RABBITMQ_DEFAULT_PASS: '${RABBITMQ_PASSWORD:-password}'
Note
Make sure to declare sail-rabbitmq under your top-level volumes: list. Access the management dashboard locally at http://localhost:15672 using your custom credentials (e.g., sail / password).
Mailpit (SMTP dev)
Sail includes Mailpit to trap outgoing emails. This prevents real messages from accidentally being sent to users during local testing.
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
All emails sent by the application can be read via the web interface at http://localhost:8025.
Meilisearch / Typesense (optional search)
To enable lightning-fast local search with Laravel Scout:
php artisan sail:install --with=meilisearch
Or copy the Meilisearch service stub into your Compose configuration. Remember to point your environment variables to the service name:
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:7700
Healthchecks and startup order
By default, Docker Compose starts containers in parallel. A standard depends_on: [mysql] only ensures that the MySQL container starts, not that the database engine is fully ready to receive connections.
Implementing healthcheck on the database service and setting condition: service_healthy on the application service ensures migrations run cleanly during initialization.
Named volumes and data reset
Named volumes act as persistent virtual drives.
- Running
sail downstops containers but leaves your database volumes untouched. - Running
sail down -vdestroys all volumes, resetting your local environment completely. - To reset only one database: locate the target volume with
docker volume lsand delete it usingdocker volume rm <volume_name>.
⚠️ Common Mistakes
1. Using 127.0.0.1 or localhost inside .env for container connections
Setting DB_HOST=127.0.0.1 causes Laravel to search for the database inside the PHP container itself, leading to a connection failure.
Reality: Use DB_HOST=mysql or DB_HOST=pgsql.
2. Destructive volume resets with sail down -v
Running sail down -v when you only want to stop your containers will wipe your databases, seeding records, and Redis keys.
Reality: Use sail down to safely stop containers. Only use -v if you intend to wipe the state completely.
3. Modifying custom Dockerfiles without rebuilding the image
Editing the published runtime Dockerfiles to install PHP extensions (like mongodb or gd) has no effect until you force a rebuild.
Reality: Always run sail build --no-cache after editing Dockerfiles.
🧠 Self-Check Questions
- Why does setting
DB_HOST=127.0.0.1fail when running Laravel migrations inside Sail? - How does
depends_onwithcondition: service_healthydiffer from a plaindepends_onarray? - What happens to your local PostgreSQL data if you run
sail down -v? - Why do you need to publish Sail's Dockerfiles (via
sail:publish) before you can use the MongoDB extension?
Reveal Answers
- Inside Docker Compose, each container has its own loopback network interface.
127.0.0.1refers to thelaravel.testcontainer itself, which does not run the database. You must use the database container's service name as the hostname. - A plain
depends_ononly checks if the container has started (in arunningstate). Thecondition: service_healthymodifier blocks the dependent container from starting until the database passes its internal readiness healthcheck (e.g. it is ready to accept socket connections). - All local PostgreSQL data will be permanently destroyed because the volume storing
/var/lib/postgresql/datais deleted. - Sail uses pre-built generic Docker images. To install custom PECL extensions, you must access and modify the underlying Dockerfile configuration and build a local image.