Separating our environment-specific configurations

First, we will need some directories to store our environment-specific config:

$ mkdir -p .env/development

Configurations of web

Then we require a file, .env/development/web (without a file extension), which contains our web-service-specific environment variables:

DATABASE_HOST=database

Configurations of database

And another file, .env/development/database, containing variables for our database service:

POSTGRES_USER=postgres
POSTGRES_PASSWORD=some-long-secure-password
POSTGRES_DB=myapp_development

Updating the Compose file

Now we need to tell Compose to use these files instead of explicitly setting the variables directly. We do this using the env_file directive:

Get hands-on with 1200+ tech skills courses.