Docker:如何将文件从容器中的一个文件夹复制到另一个文件夹? [英] Docker: How to copy a file from one folder in a container to another?

查看:1644
本文介绍了Docker:如何将文件从容器中的一个文件夹复制到另一个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将已编译的war文件复制到Docker容器中的tomcat部署文件夹中。由于 COPY ADD 处理文件从主机到容器的移动,我尝试了

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?

推荐答案

更好的解决方案是使用卷将docker容器内的各个war文件绑定到此处

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

您正在运行的命令尝试访问与dockerfile无关的文件。当您使用 docker build。生成映像时,守护进程会将 context 发送给生成器,并且在运行期间仅可访问这些文件。建立。在 docker build。中,上下文为,即当前目录。因此,它将无法访问 /tmp/projects/myproject/target/myproject.war

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.

另一种选择是在容器内部进行复制。首先使用将本地文件夹安装在容器内,然后进入容器使用 docker exec -it< container_name> bash ,然后复制所需的文件。

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.

但是,我强烈建议使用

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

这篇关于Docker:如何将文件从容器中的一个文件夹复制到另一个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆