在两个共享同一网络的Docker容器之间共享数据。

huangapple go评论68阅读模式
英文:

Share data between two docker containers sharing same network

问题

我有一个需求,需要构建两个应用程序(使用Golang编写),第一个应用程序只是通过UART接收数据,并将其发送到第二个应用程序进行处理,第二个应用程序应该接收数据并进行处理。

我已经完成了第一个应用程序通过UART接收数据的部分,现在我正在寻找更好的方法将数据从第一个模块传递给第二个模块。它们都作为Docker容器运行,并共享同一个Docker网络。

我考虑在第二个应用程序中创建REST API,第一个应用程序将通过HTTP调用简单地发送数据,但是否有更好的方法可以实现?是否有其他选项可以利用Docker网络的优势?

英文:

I have a requirement to build two applications (in Golang), first application just receives data via UART and send it to the second application for processing, second application should receive the data and process.

I have already completed receiving data via UART in first application, now I'm looking for better way to get data from first module to second module. They both are running as docker containers and sharing same docker network.

I was thinking of creating rest API in second application and first application will simply send data with http call, but is there a better way to do? Any other option that can take advantage of docker network?

答案1

得分: 2

一般来说,是的,你需要使用套接字。可以使用普通的TCP/UDP、HTTP服务器(RESTful API或非RESTful API)、gRPC等。

或者你可以启动另一个容器来使用消息队列(NATS、Kafka、RabbitMQ等),并编写发布-订阅逻辑。或者你可以使用数据库。

或者你可以在两个容器之间挂载一个共享的Docker卷,并通过文件进行通信。

这些方法都不一定只适用于Golang,可以在任何语言中使用。

英文:

In general, yes sockets are what you need. Plain TCP/UDP, HTTP server (RESTful API or not), gRPC, etc.

Or you start another container of a message queue (NATS, Kafka, RabbitMQ, etc), and write pub-sub logic. Or you can use a database.

Or you can mount a shared Docker volume between both containers and communicate via files.

None of these are necessarily unique to Golang and will work with any language.

huangapple
  • 本文由 发表于 2022年8月4日 01:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/73225827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定