You commit a local .env file containing database passwords to GitHub, and your security scanners immediately trigger an alert. Or a teammate pulls your repository, runs sail up, and watches the process crash because their local MySQL port 3306 is already occupied by another project. Environment management isn't just about defining key-value pairs; it is about drawing a strict boundary between local ergonomics, CI/CD testing, and hardened production deployments.
Containerized development changes how environment variables behave. Since PHP runs inside laravel.test while databases bind to host ports, paths, URLs, and port mappings must adjust depending on whether they are accessed from the host machine or from within the Docker Compose network.
In this guide, we show you how to structure .env files, avoid host port collisions, set up Docker-based CI pipelines, and audit your configuration before going live on production.
Navigation: All tools · Sail overview · Databases · Queues · Troubleshooting
Table of contents
.env,.env.example, and secretsFORWARD_*ports and collisionsAPP_URLand trusted proxies- Optional
env_filein Compose - CI: GitHub Actions pattern
- Sail vs dev/staging/prod servers
- Checklist before go-live
- Common Mistakes
- Self-Check Questions
.env, .env.example, and secrets
.env: Stores local secrets and environmental details. Never commit this file to source control..env.example: Committed to Git. Default keys should represent safe local defaults so a new developer can copy it to.envand runsail upimmediately.
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
# Sail specific overrides
FORWARD_DB_PORT=3306
FORWARD_REDIS_PORT=6379
FORWARD_* ports and collisions
By default, Sail binds database and application ports to localhost. If you run multiple projects, ports like 3306 (MySQL) or 80 (HTTP) will collide.
To fix this, change the forwarded ports in .env:
APP_PORT=8080
FORWARD_DB_PORT=3307
FORWARD_REDIS_PORT=6380
Note
Internal vs. External Ports
Modifying FORWARD_DB_PORT changes the port exposed on your host machine. Inside the Docker network, MySQL still communicates on the default port 3306. Your Laravel config does not need to change its DB_PORT.
APP_URL and trusted proxies
Laravel uses APP_URL to generate signed URLs and route redirects.
APP_URL=http://localhost:8080
Ensure this matches the port mapped to your host. If you put a load balancer or reverse proxy (like Nginx or Traefik) in front of your production application, configure TrustProxies in Laravel to read the client's original IP and protocol (HTTPS) correctly.
Optional env_file in Compose
If you have environment variables that only apply to the Docker environment, load them in docker-compose.yml:
services:
laravel.test:
env_file:
- .env
- .env.docker.local
This prevents polluting the shared .env file with environment variables that only matter inside containers.
CI: GitHub Actions pattern
You can use Sail's Docker Compose services inside your CI/CD pipeline to run automated tests in a clean environment.
name: Run Tests
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Copy CI Env
run: cp .env.ci .env
- name: Start Sail
run: docker compose up -d
- name: Run Pest/PHPUnit
run: docker compose exec -T laravel.test php artisan test
Sail vs dev/staging/prod servers
- Sail is optimized for local developer speed (includes tools like Mailpit, Meilisearch, and file-watching runtimes).
- Staging/Production environments require security hardening, log aggregation, automated backup routines, SSL/TLS certificates, and separate database servers (e.g., AWS RDS).
Note
Do not attempt to use sail up on a public-facing cloud server. It exposes development services and uses configuration stubs that are highly insecure.
Checklist before go-live
- [ ]
APP_ENV=productionandAPP_DEBUG=falsein the production environment. - [ ] Generate a secure
APP_KEYusingphp artisan key:generate. - [ ] Connect to managed database instances, not containerized local databases.
- [ ] Set
QUEUE_CONNECTION=redisorsqswith supervised worker systems. - [ ] Enable TLS/HTTPS termination.
- [ ] Configure centralized log handlers (e.g., Bugsnag, Sentry, or AWS CloudWatch).
⚠️ Common Mistakes
1. Committing the .env file to Git
Exposing API keys, database credentials, or application passwords in repository histories.
Reality: Verify that .env is listed in your .gitignore file before committing.
2. Changing DB_PORT instead of FORWARD_DB_PORT for local conflicts
Changing DB_PORT=3307 in your .env without modifying Sail's compose config will break the internal connection because the container database is still listening on 3306.
Reality: Keep DB_PORT=3306 (for internal container connection) and set FORWARD_DB_PORT=3307 (to change the host machine port).
3. Direct deployment of Sail's docker-compose.yml to production
Spinning up databases alongside the app container without persistent replication, backups, or secure access policies.
Reality: Sail's Compose file is a development utility, not a deployment manifest.
🧠 Self-Check Questions
- Why does changing
DB_PORT=3307inside.envbreak migrations in Sail, while changingFORWARD_DB_PORT=3307does not? - What is the security risk of deploying Sail's Compose file directly to a public production server?
- How does the
env_fileblock indocker-compose.ymlhelp structure environment overrides? - Which file should you update to ensure teammates have a list of all required API keys for a new feature?
Reveal Answers
- Changing
DB_PORTtells Laravel to look for the database on port3307inside the container network, where the database container is still listening on3306. ChangingFORWARD_DB_PORTchanges only the external port exposed to your host system, leaving internal container networking untouched. - It exposes development-only tooling (like Mailpit and Meilisearch) to the public internet, runs containers with developer privileges, and exposes databases without replication or production backups.
- It allows you to split your variables across multiple files (like
.envand.env.docker.local), loading docker-specific overrides without cluttering the main configuration file. - You should update
.env.examplewith empty or safe placeholder keys and document their usage.