0

I am trying to do a conditional COPY in Dockerfile.

The problem is that I can't do it the way as I did, as since RUN executes in sh/bash the condition operation is brought from sh/bash, not from the RUN instruction itself.

RUN if [ "$PARAM" = "dev" ]; then \
COPY /somefolder/* ./; \
else \
COPY /somefolder/* .; \
fi

This results in a COPY not found message.

I want to COPY the SAME folder, just to different target.

If the condition is true, copy that folder to ./

If not true, copy to .

Is there any workaround to do this via docker command?

Darksymphony
  • 2,155
  • 30
  • 54
  • You could COPY both directories into 2 temporary directories in the image and then have a `RUN if` statement that copied (using `cp`) from the temp dir in the image to where you want it placed. Then delete the temp directories. – Hans Kilian Sep 21 '21 at 12:32
  • did u looked at https://stackoverflow.com/a/54245466/2853439 – Prateek Jain Sep 21 '21 at 12:35
  • If you're building two very different images, having two separate Dockerfiles could make sense; if it's just configuration, consider injecting it at run time. Docker doesn't have any sort of conditional logic in a Dockerfile (beyond things you can run in a single `RUN` command). – David Maze Sep 21 '21 at 12:43
  • but I am trying to copy the same folder (only one), just the destination changes in first case ./ and second case only dot . As I am running that Dockerfile from Jenkins and it requires to have a slash at the end. – Darksymphony Sep 21 '21 at 13:21

0 Answers0