title: How to create a initialised MySQL Docker image?
body: <p>Using Docker image <code>mysql:5.7.21</code> I want to create a new image based of it that will already have its database initialised. (I want to use this for my acceptation environment).</p>
  
  <p>I know about the <code>/docker-entrypoint-initdb.d</code> and I could do the fallowing:</p>
  
  <pre><code>FROM mysql:5.7.21
  
  COPY ./init /docker-entrypoint-initdb.d
  </code></pre>
  
  <p>But the problem is that it will only copy the tar/sql files, but it will not execute until the first time it's run. Another disadvantage is also the image size. If you a copying for example 200MB of data inside the container then those files will stay in the image (as a layer).</p>
  
  <p>So I was wondering if there is a combination I haven't thought of with multi-stage or the new --squash flag (that would enable, adding the files, executing the files, removing the files).</p>
  
  <p>In my search for this problem I also found about the <code>--datadir</code> flag. Not sure how this could help.</p>
  
  <p>edit:</p>
  
  <p>So far I have the fallowing:</p>
  
  <pre><code>FROM mysql:5.7.21 as builder
  
  ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
  ENV MYSQL_DATABASE example
  
  COPY ./init /docker-entrypoint-initdb.d
  
  RUN head -n-2 &lt; /usr/local/bin/docker-entrypoint.sh &gt; /usr/local/bin/docker-entrypoint.sh \
      &amp;&amp; docker-entrypoint.sh mysqld \
      &amp;&amp; /etc/init.d/mysql start \
      &amp;&amp; mysql -uroot -e 'FLUSH TABLES;' \
      &amp;&amp; mysql -uroot -e 'show tables;' mysql \
      &amp;&amp; mysql -uroot -e 'show tables;' example \
      &amp;&amp; /etc/init.d/mysql stop \
      # This shows the files!
      &amp;&amp; ls -la /var/lib/mysql
  
  # This shows no files?!!
  RUN ls -la /var/lib/mysql
  
  FROM mysql:5.7.21
  
  ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
  ENV MYSQL_DATABASE example
  
  COPY --from=builder /var/lib/mysql /var/lib/mysql
  RUN ls -la 
  
  RUN ls -la /var/lib/mysql \
      &amp;&amp; /etc/init.d/mysql start \
      &amp;&amp; mysql -uroot -e 'show tables;' mysql \
      &amp;&amp; mysql -uroot -e 'show tables;' example
  </code></pre>
  
  <p>See the above inline comments... something really strange is happening? So in the same RUN when I perform an <code>ls -la</code> on <code>/var/lib/mysql</code> I can see the files. But in a new layer (new RUN) it's empty :S</p>