title: How-to: Dockerfile layer(s) reuse / reference
body: <p>I want to create a few images where each dockerfile will include some common layers. Its all mixed and no consistent sequence for all of the images.
  E.g. some will need to have java on them, and I want to define the below lines (taken from the official java 8 dockerfile) as a building block (layer) that will be referenced in other dockerfiles. </p>
  
  <p>In some cases it could be more than one layer that I would want to reuse - making this requirement recursive in nature (building blocks constructed of building blocks).</p>
  
  <pre><code>RUN \
      echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections &amp;&amp; \
      add-apt-repository -y ppa:webupd8team/java &amp;&amp; \
      apt-get update &amp;&amp; \
      apt-get install -y oracle-java8-installer &amp;&amp; \
      rm -rf /var/lib/apt/lists/* &amp;&amp; \
      rm -rf /var/cache/oracle-jdk8-installer
  </code></pre>
  
  <p>Is that supported by Docker today? Is it a good practice to install e.g. Java separately as a layer that way ... or should I merge it with other installs (to keep the image minimal)? What are the best practices around such a scenario please?</p>
  
  <p>=========== UPDATE ==============</p>
  
  <p>Seems like the feature of referencing/including is not supported. But I am still not sure about the best practices ...</p>
  
  <p>@Sri pointed out below to the best practices:
  <a href="https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#run" rel="noreferrer">https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#run</a></p>
  
  <p>Where is it encouraged to use "RUN apt-get update &amp;&amp; apt-get install -y" for all package installation.</p>
  
  <p>But does it also mean that we are not encouraged to use different layers for different packages? like the below example:</p>
  
  <pre><code>RUN apt-get update &amp;&amp; apt-get install -y package-foo
  RUN apt-get update &amp;&amp; apt-get install -y package-bar
  </code></pre>