简体   繁体   English

如何从docker容器rsync到我的主机

[英]how to rsync from a docker container to my host computer

My current development environment allows for automatic code reload whenever changing a file (ie nodemon / webpack ). 我当前的开发环境允许在更改文件时自动重新加载代码(即nodemon / webpack )。 However I am setting up a kubernetes ( minikube ) environment so that I can quickly open 3-4 related services at once. 但是我正在建立一个kubernetesminikube )环境,以便我可以一次快速打开3-4个相关服务。

Everything is working fine, but it is not currently doing the automatic code reload. 一切正常,但它目前没有进行自动代码重新加载。 I tried mounting the volume but there is some conflict with the way docker and virtualbox handles files such that the conflict leads to file changes from the host not reflected in docker container . 我尝试安装卷但是与dockervirtualbox处理文件的方式存在一些冲突,这样冲突导致主机的文件更改未反映在docker容器中 (That's not the first link I have that appears related to this problem, it's just the first I found while googling it on another day)... (这不是我遇到的与此问题相关的第一个链接,它只是我在另一天搜索时发现的第一个链接)...

Anyways, long story short, ppl have trouble getting live reload done in development. 无论如何,长话短说,人们在开发过程中无法进行实时重载。 I've found the problem literred throughout the interweb with very few solutions. 我发现这个问题在整个互联网中很少发现,只有很少的解决方案。 The best solution I would say I found so far is This person used tar from the host to sync folders. 我说到目前为止我发现的最佳解决方案是这个人使用主机中的tar来同步文件夹。

However I would like a solution from the container. 但是我想从容器中找到解决方案。 The reason is that I want to run the script from the container so that the developer doesn't have to run some script on his host computer every time he starts developing in a particular repo. 原因是我想从容器中运行脚本,这样开发人员每次开始在特定仓库中开发时都不必在他的主机上运行一些脚本。

In order to do this however I need to run rsync from the container to the host machine. 为了做到这一点,我需要从容器运行rsync到主机。 And I'm having a surprising lot of trouble figuring out how to write the syntax for that. 而且我在弄清楚如何编写语法时遇到了很多麻烦。

Let's pretend my app exists in my container and host respectively as: 让我们假装我的应用程序分别存在于我的容器和主机中:

/workspace/app # location in container
/Users/terence/workspace/app  # location in host computer

How do I rsync from the container to the host? 如何从容器rsync到主机? I've tried using the 172.17.0.17 and 127.0.0.1 to no avail. 我尝试过使用172.17.0.17127.0.0.1无济于事。 Not entirely sure if there is a way to do it? 不完全确定是否有办法做到这一点?

examples I tried: 我试过的例子:

rsync -av 172.17.0.17:Users/terence/workspace/app /workspace/app
rsync -av 127.0.0.1:Users/terence/workspace/app /workspace/app

If you're running the rsync from the host (not inside the container), you could use docker cp instead: 如果您从主机(而不是容器内)运行rsync,则可以使用docker cp代替:

eg, docker cp containerName:/workspace/app Users/terence/workspace/app 例如, docker cp containerName:/workspace/app Users/terence/workspace/app

Could you clarify: 你能澄清一下:
1. are you running the rsync from the host or from inside the container? 1.您是从主机还是从容器内部运行rsync?
If it's from inside the container it'll depend a lot on the --network the container is attached to (ie, bridged or host) and also the mounted volumes (ie, when you started up the container did you use -v flag?) 如果从容器内的,它会在依赖大量--network容器附着(即桥接或主机),并安装卷(即,当你启动了集装箱你用-v标志? )

Update : For rsync to work from within the container you need to expose the host's dir to the container. 更新 :要使rsync在容器内工作,您需要将主机的目录公开给容器。

As you think of a solution, keep this in mind: host dir as a data volume 在考虑解决方案时,请记住这一点:将主机目录作为数据卷

Note : The host directory is, by its nature, host-dependent. 注意 :主机目录本质上是依赖于主机的。 For this reason, you can't mount a host directory from Dockerfile, the VOLUME instruction does not support passing a host-dir, because built images should be portable. 因此,您无法从Dockerfile安装主机目录,VOLUME指令不支持传递host-dir,因为构建的映像应该是可移植的。 A host directory wouldn't be available on all potential hosts. 主机目录在所有潜在主机上都不可用。

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

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