使用PHP将文件从Docker容器从Docker容器移至同一服务器上的外部文件夹
我是Docker的新手,如果这是一个基本问题,请忍受我。我在服务器上有一个Laravel项目,该项目已停靠。我要做的是将文件从项目移动到未对其进行停靠的服务器上的另一个位置。
因此,我的项目是在/var/www/my-project
目录上设置的,我想从my-project/storage/app/app/public/file.csv to
/var/{destination_folder}
。我该如何在拉拉维尔(Laravel)做到这一点?我认为我的问题与Laravel无关,它与Docker无关,Docker不允许将文件移出。请不要添加Laravel或PHP文件复制代码片段,我尝试了很多。
我尝试了什么?
1-我尝试使用以下方式复制文件:
Storage::disk('local')->put('/var/{destination_folder}', 'my-project/storage/app/public/file.csv' )
但是,它不会复制文件。
2-我还尝试使用我使用shell_exec
或Process
从Laravel控制器执行的bash脚本移动文件,但是它也无法正常工作。
cp "/var/www/my-project/storage/app/public/file.csv" "/var/destination_folder"
该解决方案中的含义是,当我从终端运行命令时,它正在工作,但是当我从控制器调用它时它不起作用,并且它给了我
cp:无法创建常规文件'/var/var/datestagon_folder/file。 CSV':没有这样的文件或目录
搜索上述错误后,这似乎是一个权限问题,我将目标文件夹的许可更改为775,我还检查了我正在运行Laravel应用程序的用户,当我从应用程序中运行whoami
时,它给了我根。
让我知道如何实现这一目标,谢谢!
I am fairly new to docker, please bear with me if its a basic question. I have a laravel project on a server, the project is dockerized. What I want to do is move a file from my project to another location on the same server that is not dockerized.
So, my project is setup on /var/www/my-project
directory and I want to copy a file from my-project/storage/app/public/file.csv
to /var/{destination_folder}
. How can I do that in laravel? I think my issue is not related to laravel it is related to docker which is not allowing to move files out of it. Please don't add laravel or php file copy code snippets,I have tried plenty.
What I've tried?
1- I have tried copying file using:
Storage::disk('local')->put('/var/{destination_folder}', 'my-project/storage/app/public/file.csv' )
but, it does not copy the file.
2- I have also tried moving the file using bash script which I'm executing from my laravel controller using shell_exec
or process
but, it is also not working.
cp "/var/www/my-project/storage/app/public/file.csv" "/var/destination_folder"
What's hapening in this solution is that it is working when I run the command from terminal, but its not working when I call it from my controller and it gives me
cp: cannot create regular file '/var/destination_folder/file.csv': No such file or directory
After googling the above error it seemed that this is a permission issue wo, I changed the permission of the destination folder to 775 and I also checked the user from which I was running the laravel app and it gave me root when I ran whoami
from the app.
Let me know how this could be achieved, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Docker的全部要点是它与基本主机隔离。您不能简单地将文件复制出来,因为Docker主机无法访问任何未安装的磁盘。
最简单的选择是创建目标目录并根据 https:// docs创建目标目录并创建一个绑定。 .docker.com/storage/bind-mounts/
然后,您将使用以下参数进行Docker运行:
-mount type = bind,source =/var/destination_folder,target =/some_directory_inside_your_docker,然后将文件复制到Some_directory_inside_your_docker,它将显示在父宿主中。
另一个选项是在父主机上生成一个用户帐户,出于安全原因而难以将其锁定,然后在您的Docker内部有一个私钥,这将使您的docker可以ssh ssh到父宿主那里(请注意,这不起作用每个网络配置)。我认为何时可以绑定安装座,但这不是一个好主意,但是可以工作。
The entire point of docker is that it is isolated from the base host. You cannot simply copy the file out, as the docker host does not have access to any disk that is not mounted.
The easiest option is to create a destination directory and create a bind mount as per https://docs.docker.com/storage/bind-mounts/
You would then use the following argument for your docker run:
--mount type=bind,source=/var/destination_folder,target=/some_directory_inside_your_docker and copy the file to some_directory_inside_your_docker and it will appear in the parent host.
Another option is to generate a user account on the parent host, LOCK IT DOWN HARD for security reasons, and then have a private key inside your docker that would allow your docker to SSH to the parent host (note, this won't work with every network configuration). I don't think it's a good idea when you can do bind mounts, but it would work.