title: Setting build args in docker-compose.yml using env_file
body: <p>I'm trying to use Docker and <strong>Docker Compose</strong> to create a containerized app. I have a PubNub account, which allows me to use <strong>different API keys</strong> for different environments (dev, test, prod). To help me build images for this, I am trying to use <strong>build args</strong> set with an <strong>env_file</strong>.</p>
  
  <p>It's not working.</p>
  
  <pre><code>WARNING: The PUB_KEY variable is not set. Defaulting to a blank string.
  WARNING: The SUB_KEY variable is not set. Defaulting to a blank string.
  </code></pre>
  
  <p><strong>Questions:</strong> </p>
  
  <ul>
  <li>What mistake am I making in setting the build args?</li>
  <li>How do I fix it?</li>
  <li>Is this a good way to set <code>ENV</code> variables for the containers <code>scan</code> and <code>flask</code>?</li>
  </ul>
  
  <p>At the very bottom is an <strong>IntelliJ IDE screenshot</strong>, or the text code is just below.</p>
  
  <p>Here is the <code>docker-compose.yml</code> content:</p>
  
  <pre><code>version: '3.6'
  
  services:
  
    scan:
      env_file:
        - sample.env
      build:
        context: .
        dockerfile: Dockerfile
        args:
          pub_key: $PUB_KEY
          sub_key: $SUB_KEY
        target: scan
      image: bt-beacon/scan:v1
  
    flask:
      env_file:
        - sample.env
      build:
        context: .
        dockerfile: Dockerfile
        args:
          pub_key: $PUB_KEY
          sub_key: $SUB_KEY
        target: flask
      image: bt-beacon/flask:v1
      ports:
        - "5000:5000"
  </code></pre>
  
  <p>And the <code>Dockerfile</code>:</p>
  
  <pre><code># --- BASE NODE ---
  FROM python:3.6-jessie as base
  ARG pub_key
  ARG sub_key
  
  RUN test -n "$pub_key"
  RUN test -n "$sub_key"
  
  # --- SCAN NODE ---
  FROM base as scan
  
  ENV PUB_KEY=$pub_key
  ENV SUB_KEY=$sub_key
  
  COPY app/requirements.scan.txt /
  
  RUN apt-get update
  RUN apt-get -y install bluetooth bluez bluez-hcidump python-bluez python-numpy python3-dev libbluetooth-dev libcap2-bin
  RUN pip install -r /requirements.scan.txt
  RUN setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f $(which python))
  
  COPY app/src /app
  WORKDIR /app
  
  CMD ["./scan.py", "$pub_key", "$sub_key"]
  
  
  # -- FLASK APP ---
  FROM base as flask
  
  ENV SUB_KEY=$sub_key
  
  COPY app/requirements.flask.txt /
  COPY app/src /app
  
  RUN pip install -r /requirements.flask.txt
  
  WORKDIR /app
  
  EXPOSE 5000
  
  CMD ["flask", "run"]
  </code></pre>
  
  <p>Finally, <code>sample.env</code>:</p>
  
  <pre><code># PubNub app keys here
  PUB_KEY=xyz1
  SUB_KEY=xyz2
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QDecX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QDecX.png" alt="enter image description here"></a></p>