Issue
I want to automate the building and execution of a java spring web service by using Docker.
I run a script from the dockerfile that generate a settings.xml file. I want to copy that file in the image, but because RUN create a new container (I think?) the build can't find the generated file.
Here is the begining of my Dockerfile:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh
COPY settings.xml /root/.m2/
and there is the output...
How can I copy the settings.xml file that was generated by my sh script?
Thanks!
Solution
COPY
copies from the host to the image. If you want to copy from the image to the image, you use the 'normal' cp
command.
You're right that each RUN statement runs in a separate shell. But since your generated file is stored in the file system, it'll work if you do it in different RUN statements.
But let's do it in one RUN statement to only get 1 new layer in the image:
FROM ubuntu:20.04
WORKDIR /app
COPY . .
RUN ./script.sh && \
cp settings.xml /root/.m2/
You can also use mv
to move the file if you don't need a copy to stay in /app
.
Answered By - Hans Kilian
Answer Checked By - Marilyn (JavaFixing Volunteer)