title: How to set default environment variables for docker-compose services?
body: <p>Consider the example:</p>
  <p><strong>Dockerfile</strong></p>
  <pre><code>FROM ubuntu
  
  ENV var=hello
  
  ENTRYPOINT [&quot;printenv&quot;]
  </code></pre>
  <p>This container has an environment variable with a default value, which I can override:</p>
  <pre><code>docker build -t exp .
  docker run exp
  
  ...
  var=hello
  ...
  
  docker run -e var=lalala exp
  
  ...
  var=lalala
  ...
  </code></pre>
  <p>I would like to have this <em>optional</em> overridability when I use the image with docker-compose. So far I tried the following three approaches:</p>
  <h1>Obligatory override - otherwise variable unset</h1>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: &quot;3.1&quot;
  
  services:
    hello-teller:
      image: exp:latest
      environment:
        - var
  </code></pre>
  <p>result:</p>
  <pre><code>docker-compose up
  </code></pre>
  <blockquote>
  <p><code>var</code> is not set, default value ignored.</p>
  </blockquote>
  <pre><code>env var=lala docker-compose up
  </code></pre>
  <blockquote>
  <p><code>var</code> is overridden.</p>
  </blockquote>
  <h1>Cast in stone</h1>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: &quot;3.1&quot;
  
  services:
    hello-teller:
      image: exp:latest
      environment:
        - var=lala
  </code></pre>
  <p>result:</p>
  <pre><code>docker-compose up
  </code></pre>
  <blockquote>
  <p><code>var</code> is overridden to <code>lala</code>.</p>
  </blockquote>
  <pre><code>env var=huhu docker-compose up
  </code></pre>
  <blockquote>
  <p><code>var</code> is still overridden to <code>lala</code>.</p>
  </blockquote>
  <h1>No effect</h1>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: &quot;3.1&quot;
  
  services:
    hello-teller:
      image: exp:latest
      environment:
        - ${var:-lala}
  </code></pre>
  <p>result:</p>
  <pre><code>docker-compose up
  </code></pre>
  <blockquote>
  <p><code>var</code> is not overridden.</p>
  </blockquote>
  <pre><code>env var=huhu docker-compose up
  </code></pre>
  <blockquote>
  <p><code>var</code> is not overridden.</p>
  </blockquote>