title: Use env variables in Dockerfile
body: <p>I have a Dockerfile in a php project where I need to pass a user and a password to download a library during the execution.</p>
  
  <p>The user and password must be hidden in production or in the local .env files. At the moment I'm just trying the local option and the user and password come empty.</p>
  
  <p>I have used <code>"${USER}"</code> and <code>${USER}</code>, but not only the login fails, but when I print the variables they come empty. Also I've tried putting the variables hardcoded and it works fine, so the problem is that the variables are not retrieve from the .env file.</p>
  
  <p>The docker-compose starts as follows</p>
  
  <pre><code>version: '3'
  
  services:
    server:
      build:
        context: .
        dockerfile: docker/Server/Dockerfile
      container_name: "server"
      ports:
        - 80:80
        - 8888:8888
      networks:
        - network
      env_file:
        - .env
      command: sh /start-workers.sh
  </code></pre>
  
  <p>And the Dockerfile:</p>
  
  <pre><code>FROM php:7.3-cli-alpine3.10
  RUN apk add --update
  
  #
  # Dependencies
  #
  RUN apk add --no-cache --no-progress \
      libzip-dev zip php7-openssl pkgconfig \
      php-pear php7-dev openssl-dev bash \
      build-base composer
  
  #
  # Enable PHP extensions
  #
  RUN docker-php-ext-install bcmath sockets pcntl
  
  #
  # Server Dependencies
  #
  RUN echo '' | pecl install swoole \
      &amp;&amp; echo "extension=swoole.so" &gt;&gt; /usr/local/etc/php/conf.d/swoole.ini
  
  #
  # installation
  #
  WORKDIR /var/www/service
  COPY . .
  
  RUN echo "START"
  
  RUN echo "${USER}"
  
  RUN echo "${PASSWORD}"
  
  RUN echo "END"
  
  
  RUN composer config http.libraries.com "${USER}" "${PASSWORD}" --global \
      &amp;&amp; composer install -n -o --prefer-dist --no-dev --no-progress --no-suggest \
      &amp;&amp; composer clear-cache \
      &amp;&amp; mv docker/Server/start-workers.sh /
  EXPOSE 80
  </code></pre>
  
  <p>The .env starts and ends as follows:</p>
  
  <pre><code>APP_ENV=dev
  APP_SECRET=666666666666666
  .
  .
  .
  USER=user
  PASSWORD=password
  </code></pre>
  
  <p>At the moment if I execute <code>docker-compose up --build</code> the output as follows</p>
  
  <pre><code>Step 10/15 : RUN echo "START"
   ---&gt; Running in 329b1707c2ab
  START
  Removing intermediate container 329b1707c2ab
   ---&gt; 362b915ef616
  Step 11/15 : RUN echo "${USER}"
   ---&gt; Running in e052e7ee686a
  
  Removing intermediate container e052e7ee686a
   ---&gt; 3c9cfd43a4df
  Step 12/15 : RUN echo "${PASSWORD}"
   ---&gt; Running in b568e7b8d9b4
  
  Removing intermediate container b568e7b8d9b4
   ---&gt; 26a727ba6842
  Step 13/15 : RUN echo "END"
   ---&gt; Running in 726898b3eb42
  END
  </code></pre>
  
  <p>I'd like the user and the password to be printed, so I know I'm receiving the .env data and I can use it.</p>