Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dockerfile COPY file generated by RUN sh script

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FROM ubuntu:20.04

WORKDIR /app

COPY . .

RUN ./script.sh

COPY settings.xml /root/.m2/

and there is the output…

enter image description here

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading