简体   繁体   中英

Docker: How to copy a file from one folder in a container to another?

I want to copy my compiled war file to tomcat deployment folder in a Docker container. As COPY and ADD deals with moving files from host to container, I tried

RUN mv /tmp/projects/myproject/target/myproject.war /usr/local/tomcat/webapps/ 

as a modification to the answer for this question . But I am getting the error

mv: cannot stat ΓÇÿ/tmp/projects/myproject/target/myproject.warΓÇÖ: No such file or directory

How can I copy from one folder to another in the same container?

A better solution would be to use volumes to bind individual war files inside docker container as done here .

Why your command fails

The command you are running tries to access files which are out of context to for the dockerfile. When you build the image using docker build . the daemon sends context to the builder and only those files are accessible during the build. In docker build . the context is . , the current directory. Therefore, it will not be able to access /tmp/projects/myproject/target/myproject.war .

Copying from inside the container

Another option would be to copy while you are inside the container. First use volumes to mount the local folder inside the container and then go inside the container using docker exec -it <container_name> bash and then copy the required files.

Recommendation

But still, I highly recommend to use

docker run -v "/tmp/projects/myproject/target/myproject.war:/usr/local/tomcat/webapps/myproject.war" <image_name>

You can create a multi-stage build:

https://docs.docker.com/develop/develop-images/multistage-build/

Build the .war file in the first stage and name the stage eg build, like that:

FROM my-fancy-sdk as build
RUN my-fancy-build #result is your myproject.war 

Then in the second stage:

FROM my-fancy-sdk as build2
COPY --from=build /tmp/projects/myproject/target/myproject.war /usr/local/tomcat/webapps/ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM