skip to Main Content

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…

enter image description here

How can I copy the settings.xml file that was generated by my sh script?

Thanks!

2

Answers


  1. 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.

    Login or Signup to reply.
  2. By executing RUN script.sh the file will be generated inside the container. Thus it cannot be copied to the image using the COPY Dockerfile command. Instead you could mv the file inside the container to the desired destination:

    FROM ubuntu:20.04
    
    WORKDIR /app
    
    COPY . .
    
    RUN ./script.sh 
      && mv /path/to/settings.xml /root/.m2/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search