dace
dace

Reputation: 6373

How to pass variables from a .env file to Docker?

I have a .env file with a large list of variables in the root of my node project. In the root is also my docker-compose.yml file:

version: '3'

services:
  api:
    build: .
    env_file:
      - .env
    ports:
      - 5000:5000

I'm trying to pass in these variables to Docker (from here), but when I run docker-compose up, I'm running into an error in my node app because it doesn't have access to these variables. What am I doing wrong? Thanks.

Upvotes: 1

Views: 129

Answers (1)

Stefano
Stefano

Reputation: 5076

Change the file name to something different like node.env. The .env file is used by the docker-compose itself to define the content of the docker-compose file itself.

Example:

$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"

The content of this file is not used inside the docker instance itself.

Upvotes: 3

Related Questions