-1

Is it possible to install package on condition, when building image?

I have my base image and I only want to install some packages when running tests. This way I could reuse same image, even when running tests.

For example

FROM some-image
RUN pip3 intall-something
# Install this if environment variable (or ARG) `TEST` is used?
RUN pip3 install websocket-client==1.2.1 
Andrius
  • 19,658
  • 37
  • 143
  • 243
  • 2
    Does this answer your question? [Dockerfile if else condition with external arguments](https://stackoverflow.com/questions/43654656/dockerfile-if-else-condition-with-external-arguments) – LinFelix Nov 09 '21 at 10:42
  • @LinFelix yes it did, thanks – Andrius Nov 09 '21 at 11:00

1 Answers1

0

Here's an idea:

FROM alpine
ARG cmd1
RUN ${cmd1}
CMD ["ash","-c","sleep 3600"]

docker build --build-arg cmd1="apk add curl" -t myapp .

docker run -it --rm myapp curl -k https://www.google.com # ok, curl was installed and working

docker build --build-arg cmd1="true" -t myapp .

docker run -it --rm myapp curl -k https://www.google.com # curl not found as expected

You can tweak "cmd1" as you see fit. When you don't want to do anything, true is like a noop.

gohm'c
  • 13,492
  • 1
  • 9
  • 16
  • I used similar approach, just also added if/else in `RUN` – Andrius Nov 09 '21 at 11:01
  • if/else is nice to have consider a simple `true` is a noop already, it is cleaner compare to a ternary statement in a Dockerfile. But that's up to what you need. – gohm'c Nov 09 '21 at 14:07