title: docker-compose : variables from .env not available in the service
body: <p>I'm new to nodejs and I can't figure out why environment variables are not always available.</p>
  <p>Env vars in my <code>.env</code> are parsed by docker-compose automatically. So I'm not using the dotenv package.</p>
  <pre><code>console.log(process.env.API_URL) // outputs undefined
  </code></pre>
  <p>But in an asynchronous function (maybe it doesn't work in all async functions)</p>
  <pre><code>async function foo() {
      console.log(process.env.API_URL) // outputs http://example.com
  }
  </code></pre>
  <p>After moving a few ways the console.log in my <code>index.ts</code>, I've found the line where env vars become available :</p>
  <pre><code>// index.ts
  import {createConnection} from 'typeorm'
  console.log(process.env.API_URL) // outputs undefined
  createConnection()
  console.log(process.env.API_URL) // outputs http://example.com
  </code></pre>
  <p>Any idea on why this is happening ?</p>
  <p><strong>Edit :</strong></p>
  <p>I'm using this docker-compose.yml :</p>
  <pre><code>version: '3.7'
  services:
    server:
      build:
        context: ./docker-build
        target: server-develop
      ports:
        - &quot;3200:3000&quot;
      volumes:
        - .:/var/app
      command: &quot;yarn run dev&quot;
  </code></pre>
  <p><a href="https://docs.docker.com/compose/environment-variables/#pass-environment-variables-to-containers" rel="nofollow noreferrer">Docker docs</a> seems to tell that the <code>.env</code> file is parsed by default when env_file is not specified. Screenshot from docker documentation :</p>
  <p><a href="https://i.stack.imgur.com/uSZan.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uSZan.png" alt="docker-compose reference" /></a></p>
  <p>However I don't see env vars when using <code>docker-compose exec server env</code>.
  But if I specify the <code>env_file</code> in the <code>docker-compose.yml</code> (see below), environment vars are correctly loaded.</p>
  <pre><code>version: '3.7'
  services:
    server:
      build:
        context: ./docker-build
        target: server-develop
      env_file: .env # the only change
      ports:
        - &quot;3200:3000&quot;
      volumes:
        - .:/var/app
  </code></pre>
  <p>Maybe I misunderstood what actually docker does when It says</p>
  <blockquote>
  <p>The .env file is loaded by default</p>
  </blockquote>