<p>So you can use two <a href="https://docs.docker.com/develop/develop-images/multistage-build/" rel="nofollow noreferrer">build stages</a> in your Dockerfile so you don't end up having unnecessary things inside container.  Anything from first stage is gone until you move it to second stage. Something like:</p>

<pre><code>FROM whatever as build-stage
WORKDIR /app
RUN apt-get update &amp;&amp; apt-get install -y unzip
RUN wget https://getcomposer.org/download/1.8.3/composer.phar
RUN php installer.php

FROM whatever as production-stage
COPY --from=build-stage /app ./
</code></pre>

CC BY-SA 4.0
2019-02-08T16:53:41.490Z
This is _exactly_ what I was looking for! Thanks a lot.
Sylvain Leroux
CC BY-SA 4.0
2019-02-08T16:54:20.890Z
Welcome. You may accept the answer ;)
deosha
CC BY-SA 4.0
2019-02-08T16:55:23.627Z
Done. I've edited the answer to add a link to the docs.
Sylvain Leroux
CC BY-SA 4.0
2019-02-08T16:57:16.217Z
accepted the edit :-)
deosha
CC BY-SA 4.0
2019-02-08T16:46:08.173Z
2019-02-08T16:55:51.740Z
2019-02-08T16:55:51.740Z
deosha
<p>I have a third party PHP application to install in a container. The application is shipped without its dependencies. There is a provided script to automate dependency installation, but it relies on a couple of tools not available in my base image (both general utilities like <code>unzip</code> as well as some PHP-specific tools like <a href="https://getcomposer.org/" rel="nofollow noreferrer">composer</a>). Those tools are only required for the dependency installation. There are not required afterward.</p>

<p><br>
A simple solution would be to install the required tools and run the installation script in a <code>RUN</code> section of my Dockerfile. But I don't want to clutter the image with software only useful during the image build step.</p>

<pre><code>  RUN apt-get update &amp;&amp; apt-get install --yqq unzip
  RUN wget https://getcomposer.org/download/1.8.3/composer.phar
  RUN php installer.php
  # I end up polluting my container with unused tools and possible
  # behind the scene changes in some files (`php.ini`, ...)
</code></pre>

<p><br>
Another solution would be to perform the installation on the host and to <code>COPY</code> the application alongside its dependencies into the container. But I want a reproducible build.</p>

<pre><code>  COPY app/ ./
  # Not reproducible: I do not control the version of
  # the installed app neither the version of the tools
  # and dependencies
</code></pre>

<hr>

<p>So, I envision a two-steps process: </p>

<ol>
<li>using a first container to perform the installation;</li>
<li>then copying the application alongside its dependencies into another "clean" container.</li>
</ol>

<p><strong>But I don't see exactly how to achieve that. Do you have any suggestions?</strong></p>

<docker><automation><installation>
Docker image two-steps build
Sylvain Leroux
# Docker image two-steps build

(Asked by: username_0 on 2019-02-08)

I have a third party PHP application to install in a container. The application is shipped without its dependencies. There is a provided script to automate dependency installation, but it relies on a couple of tools not available in my base image (both general utilities like unzip as well as some PHP-specific tools like composer). Those tools are only required for the dependency installation. There are not required afterward.

A simple solution would be to install the required tools and run the installation script in a RUN section of my Dockerfile. But I don't want to clutter the image with software only useful during the image build step.
  RUN apt-get update && apt-get install --yqq unzip
  RUN wget https://getcomposer.org/download/1.8.3/composer.phar
  RUN php installer.php
  # I end up polluting my container with unused tools and possible
  # behind the scene changes in some files (`php.ini`, ...)


Another solution would be to perform the installation on the host and to COPY the application alongside its dependencies into the container. But I want a reproducible build.
  COPY app/ ./
  # Not reproducible: I do not control the version of
  # the installed app neither the version of the tools
  # and dependencies


So, I envision a two-steps process: 

using a first container to perform the installation;
then copying the application alongside its dependencies into another "clean" container.

But I don't see exactly how to achieve that. Do you have any suggestions?

**Tags:** docker, automation, installation


---

## 1 answers

### Answer by username_1 on 2019-02-08. Score: 3 | Accepted answer

So you can use two build stages in your Dockerfile so you don't end up having unnecessary things inside container.  Anything from first stage is gone until you move it to second stage. Something like:
FROM whatever as build-stage
WORKDIR /app
RUN apt-get update && apt-get install -y unzip
RUN wget https://getcomposer.org/download/1.8.3/composer.phar
RUN php installer.php

FROM whatever as production-stage
COPY --from=build-stage /app ./

> **Comments:**
> **username_0**: This is _exactly_ what I was looking for! Thanks a lot.  
> **username_1**: Welcome. You may accept the answer ;)  
> **username_0**: Done. I've edited the answer to add a link to the docs.  
> **username_1**: accepted the edit :-)  

---

