Docker: How to copy a file from one folder in a co

2020-07-16 03:33发布

问题:

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?

回答1:

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>