简体   繁体   English

在docker容器内将文件从docker移动到本地

[英]Moving file from docker to local while inside docker container

I'm having a python program that stores the output file in local.我有一个将输出文件存储在本地的python 程序。 Now my code is run inside docker container I want to move the generated output file (like output.txt) to my local (outside the docker container)现在我的代码在 docker 容器内运行我想将生成的输出文件(如 output.txt)移动到我的本地(在 docker 容器外)

I know there is a command which transfers files from docker to local:我知道有一个命令可以将文件从 docker 传输到本地:

docker cp <containerId>:/file/path/within/container /host/path/target

#I tried like this inside docker container but it didn't work
os.system(sudo docker cp <containerId>:/file/path/within/container /host/path/target) 

But since my program is executing inside docker this doesn't work and I want to push the file to local as the code runs.但是由于我的程序在 docker 中执行,这不起作用,我想在代码运行时将文件推送到本地。

If you have any ideas please share them.如果您有任何想法,请分享。

There is no way for code inside a container to directly manipulate anything outside the container.容器内的代码无法直接操作容器外的任何内容。 The whole purpose of a container is to isolate it from the host system;容器的全部目的是将它与主机系统隔离; breaking this barrier would have grave security consequences, and basically render containers pointless.打破这个障碍会产生严重的安全后果,并且基本上会使容器变得毫无意义。

What you can do is mount a directory from the host inside the container with -v (or run docker cp from outside the container once you are confident it has succeeded in creating the file successfully; but then how would you communicate this fact to the outside?)可以做的是使用-v从容器内的主机挂载一个目录(或者一旦您确信它已成功成功创建文件,则从容器外部运行docker cp ;但是您将如何将此事实传达给外部?)

With

docker run --volume=`pwd`:/app myimage

you can你可以

cp myfile /app

inside the container;容器内; this will create ./myfile from within Docker.这将从 Docker 中创建./myfile

Treat container as a Linux system, your question will consider as: how transfer files between two hosts.把容器当成Linux系统,你的问题会考虑为:如何在两台主机之间传输文件。

Maybe there are some another ways without rerun the container:也许还有其他一些方法无需重新运行容器:

  • scp (recommend) with other options or tools while you need, like expect could handle the ssh 's accept fingerprint or input the password , assume 172.17.0.1 is the host's docker interface and ssh_port is 22 by default, and the ssh progress is listening the docker interface such as 0.0.0.0:22 . scp (建议)与其他选项或工具,而你所需要的,就像expect可以处理ssh接受指纹或输入密码,假设172.17.0.1是主机的泊坞窗的界面和ssh_port默认是22, ssh进步听力docker接口,例如0.0.0.0:22
os.system(sudo scp /file/path/within/container user@172.17.0.1:/host/path/target)
  • other client–server models, such as rsync (client and server), python SimpleHTTPServer (server) and curl |其他客户端-服务器模型,例如rsync (客户端和服务器)、 python SimpleHTTPServer (服务器)和curl | python request | python request | wget (client) and so on. wget (客户端)等。 But these are not recommend, because the server and client need to deploy.但是这些都不推荐,因为服务器和客户端都需要部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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