title: Pass variables from .env file to dockerfile through docker-compose
body: <pre><code>project
  └───app
  │   │   ...
  │   │   Dockerfile
  │   │
  └───prod.env
  └───docker-compose.yml
  </code></pre>
  <p>My docker-compose looks like this:</p>
  <pre><code>services:
     app:
        build:
           context: .\app
           args:
              ARG1: val1
              ARG2: val2
        env_file:
           - prod.env
  </code></pre>
  <p>But I've tried this too:</p>
  <pre><code>services:
     app:
        build:
           context: .\app
           args:
              ARG1: ${ARG1}
              ARG2: ${ARG2}
        env_file:
           - prod.env
  </code></pre>
  <p>My prod.env file looks like this:</p>
  <pre><code>ARG1 = 'val1'
  ARG2 = 'val2'
  </code></pre>
  <p>But I've tried this too:</p>
  <pre><code>ARG1=val1
  ARG2=val2
  </code></pre>
  <p>I would like for either the values of args or the values from the prod.env file to be passed to the dockerfile.</p>
  <p>This is what I've tried to get this:</p>
  <pre><code>ARG ARG1
  ARG ARG2
  
  RUN echo ${ARG1}
  RUN echo ${ARG2}
  </code></pre>
  <pre><code>ENV ARG1 ${ARG1}
  ENV ARG2 ${ARG2}
  
  RUN echo ${ARG1}
  RUN echo ${ARG2}
  </code></pre>
  <pre><code>ENV ARG1 &quot;new val2&quot;
  ENV ARG2 &quot;new val2&quot;
  
  RUN echo ${ARG1}
  RUN echo ${ARG2}
  </code></pre>
  <p>It always end with blank values.</p>
  <p>Any help would be greatly appreciated. I feel like no answers from other posts have worked when I tried them.</p>
  <p>To build I use <code>docker-compose --env-file prod.env build</code></p>
  <p>Thanks</p>
  <p><strong>Update</strong>
  Sergio Santiago asked if I could run <code>docker-compose config</code> and show the results.</p>
  <p>Here are the final files I used for this test.</p>
  <p>docker-compose:</p>
  <pre><code>services:
     app:
        build:
           context: .\app
           args:
              ARG1: val1
              ARG2: val2
        env_file:
           - prod.env
  </code></pre>
  <p>prod.env:</p>
  <pre><code>ARG3 = 'val3'
  ARG4 = 'val4'
  </code></pre>
  <p>And here is the output of <code>docker-compose --env-file prod.env config</code></p>
  <pre><code>networks:
    demo-net: {}
  services:
    app:
      build:
        args:
          ARG1: val1
          ARG2: val2
        context: C:\project\app
      environment:
        ENV: prod.env
        ARG3: val3
        ARG4: val4
  </code></pre>
  <p>I would like to add that clearly from here getting the variable from the .env file to the docker-compose file is not the issue. I also have a flask app running on the container and through os.environ it is able to use the variables in the .env file. I just can't figure out how to give the same access to the Dockerfile.</p>
  <p><strong>Update 2</strong>
  More specific information in relation to ErikMD's answer</p>
  <p>prod.env</p>
  <pre><code>DOMAIN = 'actualdomain.com'
  ENV = 'prod.env'
  ENV_NUM = 1
  ARG1 = 'value1'
  </code></pre>
  <p>dev.env</p>
  <pre><code>DOMAIN = 'localhost'
  ENV = 'dev.env'
  ENV_NUM = 0
  ARG1 = 'value1'
  </code></pre>
  <p>Notice that the value for ARG1 is the same but the other values are different.</p>
  <p>docker-compose.yml</p>
  <pre><code>version: &quot;3.7&quot;
  services:
    home:
      image: home-${ENV_NUM}
      build: 
        context: .\home
        args:
          ARG1: &quot;${ARG1}&quot;
      networks:
        - demo-net
      env_file:
        - ${ENV}
      labels:
        - traefik.enable=true
        - traefik.http.routers.home.rule=Host(`${DOMAIN}`)
        - traefik.http.routers.home.entrypoints=web
      volumes:
        - g:\:c:\sharedrive
  ...
  ...
    reverse-proxy:
      restart: always
      image: traefik:v2.6.1-windowsservercore-1809
      command:
        - --api.insecure=true
        - --providers.docker=true
        - --entrypoints.web.address=:80
        - --providers.docker.endpoint=npipe:////./pipe/docker_engine
      ports:
        - 80:80
        - 443:443
        - 8080:8080
      networks:
       - demo-net
      volumes:
        - source: \\.\pipe\docker_engine\
          target: \\.\pipe\docker_engine\
          type: npipe
  networks:
    demo-net:
  </code></pre>
  <p>The dots represent other apps that would be formatted the same as home.</p>
  <p>dockerfile</p>
  <pre><code>FROM python:3.10.3
  
  ARG ARG1=&quot;default&quot;
  
  ENV ARG1=&quot;${ARG1}&quot;
  
  WORKDIR /app
  
  ENV PYTHONDONTWRITEBYTECODE 1
  ENV PYTHONUNBUFFERED 1
  
  RUN echo &quot;This is argument 1 -&gt; ${ARG1}&quot;
  </code></pre>
  <p>output of <code>docker-compose --env-file prod.env config</code></p>
  <pre><code>networks:
    demo-net: {}
  services:
    home:
      build:
        args:
          ARG1: value1
        context: C:\MIS-Web-App\home
      environment:
        DOMAIN: actualdomain.com
        ENV: prod.env
        ENV_NUM: '1'
        ARG1: value1
      image: home-1
      labels:
        traefik.enable: &quot;true&quot;
        traefik.http.routers.home.entrypoints: web
        traefik.http.routers.home.rule: Host(`mis.canaras.net`)
      networks:
        demo-net: null
      volumes:
      - g:\:c:\sharedrive:rw
  ...
  ...
  </code></pre>
  <p>Then I run either <code>docker-compose --env-file prod.env build</code> or <code>docker-compose --env-file dev.env build</code></p>
  <p>output of build</p>
  <pre><code>Step 9/23 : RUN echo &quot;This is argument 1 -&gt; ${ARG1}&quot;
   ---&gt; Running in 5142850de365
  This
  is
  argument
  1
  -&gt;
  Removing intermediate container 5142850de365
  </code></pre>
  <p>Now I call pass the env_file in the command as well as in the actual file because there are variables in there that my docker-compose file needs and variables that my flask app needs. And there is definitely overlap.</p>
  <p>Getting the values from the prod.env or dev.env files to docker-compose is not the issue. Neither is getting it to my flask app. The issue is getting those values to the dockerfile.</p>