Docker Community Forums

Share and learn in the Docker community.

Can I Omit Environment Config in Docker-Compose?

I was running my container (postgresql) previously by just doing a super long “one-liner” of docker run…

I recently created a docker-compose.yml file to spin up my container however I don’t have any of the environment configurations simply because my volume is persistent and already configured prior to me using the yaml config.

Is it OK to not have the environment stanza in my config?

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

Declare default environment variables in file
Compose supports declaring default environment variables in an environment file named .env placed in the project directory. The project directory is specified by order of precedence:

–project-dir flag
Folder of the first --file flag
Current directory
Syntax rules
These syntax rules apply to the .env file:

Compose expects each line in an env file to be in VAR=VAL format.
Lines beginning with # are processed as comments and ignored.
Blank lines are ignored.
There is no special handling of quotation marks. This means that they are part of the VAL.
Compose file and CLI variables
The environment variables you define here are used for variable substitution in your Compose file, and can also be used to define the following CLI variables:

COMPOSE_API_VERSION
COMPOSE_CONVERT_WINDOWS_PATHS
COMPOSE_FILE
COMPOSE_HTTP_TIMEOUT
COMPOSE_PROFILES
COMPOSE_PROJECT_NAME
COMPOSE_TLS_VERSION
DOCKER_CERT_PATH
DOCKER_HOST
DOCKER_TLS_VERIFY