英文:
Communication between services within the docker compose network
问题
My question was based on a situation that happened on a daily basis at work. When creating a single dockerfile and using it to create 2 services in docker compose do they communicate the same files and folder?
基于我日常工作中经历的情况,我的问题是,当创建一个单一的Docker文件并在Docker Compose中使用它来创建2个服务时,它们是否会共享相同的文件和文件夹?
From this principle it would be, I have service "A" that generated something with the "command" written in docker compose, using as an example an api request that downloads a file "random.txt", and with service "B" I want to be able to take advantage of this file. Using the same dockerfile I am creating a communication between point "A" and "B".
基于这个原则,我有一个名为"A"的服务,它使用在Docker Compose中编写的"command"生成了一些内容,以一个API请求下载一个名为"random.txt"的文件为例,并且我希望在服务"B"中能够利用这个文件。使用相同的Docker文件,我正在创建点"A"和点"B"之间的通信。
This would be to avoid creating volume in docker compose with the same path for both service points "A" and "B". When point "A" something is executed that brings a file to the machine that has its volume connected. To be able to capture in "B". I have to have another volume.
这是为了避免在Docker Compose中为服务点"A"和"B"创建具有相同路径的卷。当点"A"执行某些操作并将文件传输到具有其连接卷的机器上时,为了能够在"B"中捕获它,我必须拥有另一个卷。
I will put two example images.
我将放置两个示例图像。
英文:
My question was based on a situation that happened on a daily basis at work. When creating a single dockerfile and using it to create 2 services in docker compose do they communicate the same files and folder?
From this principle it would be, I have service "A" that generated something with the "command" written in docker compose, using as an example an api request that downloads a file "random.txt", and with service "B" I want to be able to take advantage of this file. Using the same dockerfile I am creating a communication between point "A" and "B".
This would be to avoid creating volume in docker compose with same path for both service points "A" and "B". When point "A" something is executed that brings a file to the machine that has its volume connected. To be able to capture in "B" . I have to have another volume.
I will put two example images.
Guys if I'm wrong in any concept, please correct me.
答案1
得分: 1
以下是翻译好的部分:
问题不太清楚,缺少一些代码片段,但我会做出一些假设。
无论您是否使用相同的Dockerfile,要在容器之间共享文件的唯一方法是使用具有相同主机路径的卷或绑定挂载。
这是一个示例:
version: "3.9"
services:
service-1:
image: ubuntu
volumes:
- datavolume:/yourdata
entrypoint: touch /yourdata/file1
service-2:
image: ubuntu
depends_on:
- service-1
volumes:
- datavolume:/yourdata
entrypoint: ls /yourdata
volumes:
datavolume:
在这里,service-1
将在卷datavolume
中创建一个名为file1
的文件。
service-2
也挂载了相同的卷,因此将能够访问第一个服务创建的file1
。您还可以想象file1
是通过API调用而不是由touch
命令创建的。
这是我运行上述compose文件时的样子:
在这种情况下,两个服务都使用相同的ubuntu
镜像(或者它可以是您从Dockerfile创建的自己的镜像)。但是,这并不会改变与卷和卷中的文件有关的任何内容。
干杯!
英文:
The question is not quite clear, some snippets of code are missing, but I'll make a few assumptions.
Regardless of whether you use the same Dockerfile or not, your only way to share files between containers is to use volumes or bind mounts with the same host path.
Here is an example
version: "3.9"
services:
service-1:
image: ubuntu
volumes:
- datavolume:/yourdata
entrypoint: touch /yourdata/file1
service-2:
image: ubuntu
depends_on:
- service-1
volumes:
- datavolume:/yourdata
entrypoint: ls /yourdata
volumes:
datavolume:
Here, service-1
will create a file called file1
in the volume datavolume
service-2
also mounts that same volume, and thus will be able to access file1
created by the first service. You can also imagine the file1
came from an API call instead of being created by the touch
command.
Here is what it looks like when I ran the above compose file :
In this case, both services use the same ubuntu
image (or it can be your own Image created from a Dockerfile. But again, this does not change anything regarding the volumes. The can be 2 completely different images, and still share the same volumes, and files within those volumes.
Cheers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论