Home > Net >  Docker Compose .env File Not Read
Docker Compose .env File Not Read

Time:10-19

I have a .env file I'm trying to use in a Docker Compose file to pass to the container to be used in the entrypoint script defined in the Dockerfile.

I have the following effective Dockerfile (spread out over two files, one override). The .env.dev file is in the same directory as the docker compose files. The environment variable value is not getting passed to the container. When I add "=${RUN_MIGRATIONS_ON_START}", the variable value is blank. If I leave that off, then the variables aren't even set in the container.

Docker compose files:

Main docker compose file:

version: '3.4'

services:
  web:
    build
      context: .

Override docker compose file:

version: '3.4'
services:

  web:
    environment:
      - RUN_MIGRATIONS_ON_START=${RUN_MIGRATIONS_ON_START}
      - WS_SCHEME=${WS_SCHEME}
    env_file:
      - .env.dev

CodePudding user response:

Solution

docker-compose.override.yml

version: '3.4'
services:
  web:
    env_file:
      - .env.dev

.env.dev

RUN_MIGRATIONS_ON_START=FOO
WS_SCHEME=BAR

Why

environment:
  - X=${Y} # Y is a variable from the local shell environment, not from .env

Described in detail in documentation

Your configuration options can contain environment variables. Compose uses the variable values from the shell environment in which docker-compose is run.

  • Related