Menu

Laravel Docker: Set Up Environment Variables for Production
📰

Laravel Docker: Set Up Environment Variables for Production

DEV Community: docker·Muhammad Yasir Amin·3 days ago
#JmH3dUP0
#devtodocker#devto#dev#production#laravel
Reading 0:00
15s threshold

To set environment variables for Laravel in Docker production, do not bake your .env file into the image. Pass the values when the container starts, using environment or env_file in Docker Compose, --env-file with docker run , or your platform's secret store. Then run php artisan config:cache so Laravel reads configuration from a cached file. This keeps one image usable across staging and production.

This guide targets Laravel 13 on PHP 8.3 or newer. The same ideas apply to Laravel 12.

Why not copy .env into the image?

Anyone who can pull the image can read the secrets in its layers.

The same image can no longer be promoted from staging to production, because the values are fixed.

Rotating a password means rebuilding and redeploying the image.

Laravel's own documentation says .env should not be committed to source control, because each environment may need a different configuration. The same reasoning applies to image layers.

Step 1: Which variables does production need?

At minimum, set these.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.