title: Defining ENV variables in a Dockerfile that is used in a docker-compose file
body: <p>My situation is the following, I have a project with this structure:</p>
  
  <pre><code>project/
  |
  |______docker-compose.yml
  |______database/
         |______Dockerfile
  |______webapp/
         |______Dockerfile
  </code></pre>
  
  <p>My problem is with the <code>database</code> container build. My <code>database/Dockerfile</code> is the following:</p>
  
  <pre><code>FROM mysql:8.0.3
  ENV MYSQL_ROOT_PASSWORD=secret
  ENV MYSQL_DATABASE=clients
  EXPOSE 3306
  </code></pre>
  
  <p>My <code>docker-compose.yml</code> file looks like this:</p>
  
  <pre><code>version: "3"
  services:
  database-server:
    build: ./database
    ports:
      - 3306:3306
  </code></pre>
  
  <p>So as you see above, I defined my <code>ENV</code> variables (<code>MYSQL_ROOT_PASSWORD</code> and <code>MYSQL_DATABASE</code>) in my <code>Dockerfile</code>, so I expect them to be used during the build when I use <code>docker-compose</code>. But they don't!</p>
  
  <p>My <code>ENV</code> variables only get used when I build the container manually in <code>project/database</code> using the <code>Dockerfile</code> and the <code>docker build</code> command.</p>
  
  <pre><code>Dockerfile
  |
  |
  | [Variables are available here]
  |
  |
  docker-compose.yml
  |
  |
  | [Variables are NOT available here]
  |
  docker run
  </code></pre>
  
  <p>Is it possible to do what I want to do? I want to define everything relative to a specific container in it's <code>Dockerfile</code>, and keep the <code>docker-compose.yml</code> to define the relationships and links between the containers.</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As asked by @Tim, here is the output of my <code>docker-compose up</code>:</p>
  
  <pre><code>Starting root_database-server_1 ... done
  Starting root_webapp_1 ... done
  Attaching to root_database-server_1, root_webapp_1
  database-server_1  | error: database is uninitialized and password option is         not specified
  database-server_1  |   You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
  ...
  ... Rest of the log is fine..
  </code></pre>