英文:
How to make an API call from dynamically created container to a container which is part of another network?
问题
My simplified docker compose file:
services:
myapp:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- mynetwork
ports:
- 3000:3000
networks:
mynetwork:
我运行以下两个命令来启动我的容器:
docker swarm init
docker stack deploy -c docker-compose.yml app
现在,在应用程序中,我使用 dockerode 来创建动态容器。我有一些代码在动态创建的容器内部,它执行对 myapp 容器的 API 调用。但是,当使用 URL http://myapp:3000 触发该调用时,它失败了,因为它没有关于网络的任何上下文信息。所以我尝试添加由docker compose创建的网络,但这也失败了。
用于创建动态容器并尝试将其附加到相同网络的代码:
const Docker = require("dockerode");
const docker = new Docker({
socketPath: "/var/run/docker.sock",
});
let network = docker.getNetwork("mynetwork");
let container = await docker.createContainer({
Image: '1234567890',
NetworkMode: "mynetwork",
});
await network.connect({
Container: container.id,
});
container.start();
之后,我收到了这个错误:
Error: (HTTP code 500) server error - Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found
at /app/node_modules/docker-modem/lib/modem.js:343:17
at getCause (/app/node_modules/docker-modem/lib/modem.js:373:7)
at Modem.buildPayload (/app/node_modules/docker-modem/lib/modem.js:342:5)
at IncomingMessage.<anonymous> (/app/node_modules/docker-modem/lib/modem.js:310:16)
at IncomingMessage.emit (node:events:523:35)
at endReadableNT (node:internal/streams/readable:1367:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
reason: 'server error',
statusCode: 500,
json: {
message: 'Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found'
}
}
如何正确将动态创建的容器附加到我的网络?
英文:
My simplified docker compose file
services:
myapp:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- mynetwork
ports:
- 3000:3000
networks:
mynetwork:
I run these two commands to up my containers
docker swarm init
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
const Docker = require("dockerode");
const docker = new Docker({
socketPath: "/var/run/docker.sock",
});
let network = docker.getNetwork("mynetwork");
let container = await docker.createContainer({
Image: '1234567890',
NetworkMode: "mynetwork",
});
await network.connect({
Container: container.id,
});
container.start();
After this I get this error
Error: (HTTP code 500) server error - Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found
at /app/node_modules/docker-modem/lib/modem.js:343:17
at getCause (/app/node_modules/docker-modem/lib/modem.js:373:7)
at Modem.buildPayload (/app/node_modules/docker-modem/lib/modem.js:342:5)
at IncomingMessage.<anonymous> (/app/node_modules/docker-modem/lib/modem.js:310:16)
at IncomingMessage.emit (node:events:523:35)
at endReadableNT (node:internal/streams/readable:1367:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
reason: 'server error',
statusCode: 500,
json: {
message: 'Could not attach to network mynetwork: rpc error: code = NotFound desc = network mynetwork not found'
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论