2

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:

  1. using a first container to perform the installation;
  2. 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?

1 Answer 1

3

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 ./
4
  • This is exactly what I was looking for! Thanks a lot. Commented Feb 8, 2019 at 16:53
  • Welcome. You may accept the answer ;)
    – deosha
    Commented Feb 8, 2019 at 16:54
  • Done. I've edited the answer to add a link to the docs. Commented Feb 8, 2019 at 16:55
  • 1
    accepted the edit :-)
    – deosha
    Commented Feb 8, 2019 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.