如何从动态创建的容器向另一个网络中的容器发出API调用?

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

How to make an API call from dynamically created container to a container which is part of another network?

问题

My simplified docker compose file:

  1. services:
  2. myapp:
  3. volumes:
  4. - /var/run/docker.sock:/var/run/docker.sock
  5. networks:
  6. - mynetwork
  7. ports:
  8. - 3000:3000
  9. networks:
  10. mynetwork:

我运行以下两个命令来启动我的容器:

  1. docker swarm init
  2. docker stack deploy -c docker-compose.yml app

现在,在应用程序中,我使用 dockerode 来创建动态容器。我有一些代码在动态创建的容器内部,它执行对 myapp 容器的 API 调用。但是,当使用 URL http://myapp:3000 触发该调用时,它失败了,因为它没有关于网络的任何上下文信息。所以我尝试添加由docker compose创建的网络,但这也失败了。

用于创建动态容器并尝试将其附加到相同网络的代码:

  1. const Docker = require("dockerode");
  2. const docker = new Docker({
  3. socketPath: "/var/run/docker.sock",
  4. });
  5. let network = docker.getNetwork("mynetwork");
  6. let container = await docker.createContainer({
  7. Image: '1234567890',
  8. NetworkMode: "mynetwork",
  9. });
  10. await network.connect({
  11. Container: container.id,
  12. });
  13. container.start();

之后,我收到了这个错误:

  1. Error: (HTTP code 500) server error - Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found
  2. at /app/node_modules/docker-modem/lib/modem.js:343:17
  3. at getCause (/app/node_modules/docker-modem/lib/modem.js:373:7)
  4. at Modem.buildPayload (/app/node_modules/docker-modem/lib/modem.js:342:5)
  5. at IncomingMessage.<anonymous> (/app/node_modules/docker-modem/lib/modem.js:310:16)
  6. at IncomingMessage.emit (node:events:523:35)
  7. at endReadableNT (node:internal/streams/readable:1367:12)
  8. at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  9. reason: 'server error',
  10. statusCode: 500,
  11. json: {
  12. message: 'Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found'
  13. }
  14. }

如何正确将动态创建的容器附加到我的网络?

英文:

My simplified docker compose file

  1. services:
  2. myapp:
  3. volumes:
  4. - /var/run/docker.sock:/var/run/docker.sock
  5. networks:
  6. - mynetwork
  7. ports:
  8. - 3000:3000
  9. networks:
  10. mynetwork:

I run these two commands to up my containers

  1. docker swarm init
  2. docker stack deploy -c docker-compose.yml app

Now within the app I use dockerode to create dynamic containers. I have some code inside the dynamically created containers which performs an API call to myapp container. But when that call is triggered using the url http://myapp:3000 it fails, as it does not have any context about the network. So I tried adding the network created by docker compose. But that also fails.

Code for creating dynamic container and trying to attach it to the same network

  1. const Docker = require(&quot;dockerode&quot;);
  2. const docker = new Docker({
  3. socketPath: &quot;/var/run/docker.sock&quot;,
  4. });
  5. let network = docker.getNetwork(&quot;mynetwork&quot;);
  6. let container = await docker.createContainer({
  7. Image: &#39;1234567890&#39;,
  8. NetworkMode: &quot;mynetwork&quot;,
  9. });
  10. await network.connect({
  11. Container: container.id,
  12. });
  13. container.start();

After this I get this error

  1. Error: (HTTP code 500) server error - Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found
  2. at /app/node_modules/docker-modem/lib/modem.js:343:17
  3. at getCause (/app/node_modules/docker-modem/lib/modem.js:373:7)
  4. at Modem.buildPayload (/app/node_modules/docker-modem/lib/modem.js:342:5)
  5. at IncomingMessage.&lt;anonymous&gt; (/app/node_modules/docker-modem/lib/modem.js:310:16)
  6. at IncomingMessage.emit (node:events:523:35)
  7. at endReadableNT (node:internal/streams/readable:1367:12)
  8. at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  9. reason: &#39;server error&#39;,
  10. statusCode: 500,
  11. json: {
  12. message: &#39;Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found&#39;
  13. }
  14. }

What is the right way to attach the dynamically created container to my network ?

答案1

得分: 1

网络不叫做 mynetwork。Docker Compose 在其前面添加了一个 '项目名称'。默认情况下,项目名称是包含 docker-compose.yml 文件的目录名称。

如果您的目录名称是 'mydirectory',那么网络名称将是 mydirectory_mynetwork

使用 docker network ls 命令查找名称,然后在您的节点程序中使用该名称。

英文:

The network isn't called mynetwork. Docker compose adds a 'project name' in front of it. By default, the project name is the directory name where the docker-compose.yml file is.

If your directory name is 'mydirectory', then the network name will be mydirectory_mynetwork.

Use docker network ls to find out what the name is and use that name in your node program.

huangapple
  • 本文由 发表于 2023年5月15日 03:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249379.html
匿名

发表评论

匿名网友

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

确定