问题描述
我使用 docker-compose
创建一个相互关联的容器的公园。这里没什么特别的,我有API /前端/后端等。有我的 docker-compose.yml
:
版本: 2
服务:
api:
构建:./ deimos_api / docker
端口:
- 39999:80
卷:
-./deimos_api:/root/deimos_api
资产:
生成:./deimos_asset/docker
端口:
- 8000:80
量:
-./deimos_asset:/root/deimos_asset
取决于:
-api
链接:
-api
数据管理器:
构建:./deimos_data_manager/docker
端口:
- 3333:3333
数量:
-。 / deimos_data_manager:/ root / deimos_data_manager
depend_on:
-api
链接:
-api
后端:
构建:./deimos_backend/docker
端口:
- 1080:80
卷:
-./deimos_backend:/root/deimos_backend
-/var/run/docker.sock:/var/run/docker.sock
depends_on:
-api
-资产
链接:
-api
我刚刚学习到在新网络上创建一个像该链接容器一样的公园。 docker network ls
结果:
网络ID名称驱动器
448daa341d56网桥
c7feeeb44cd0 deimos_default网桥
1fc2d6132b86无null
a80e41eab3cc主机主机
项目的另一部分将在运行容器中生成。但是我希望新容器访问以前由 docker-compose
创建的 deimos_api
容器。
我在JavaScript中使用 dockerode
(Docker远程API的包装器),其代码如下:
docker.createContainer({
Image:config.dockerGameServerImageName,
name:config.gameServerPrefix + serverName,
卷:{ / root / deimos_server:{}}
},函数(错误,容器){
if(err){
console.log(err);
reject(err);
} else {
container.start({
PortBindings:{ 80 / tcp:[{ HostPort:lastPortUsed +}]} ,
Binds:[config.deimosServerVolumePath +:/ root / deimos_server],
Links:[config.dockerApiContainerName +:api]
},函数(错误,数据){if(err){
console.log(err);
reject(err);
} else {
console.log(创建容器 + serverName);
gameContainer [config.gameServerPrefix + serverName] = lastPortUsed +;
resolv({
’serverPort’:lastPortUsed,
‘serverName’:serverName
});
}
});
}
});
但是我有以下错误:
错误:HTTP代码为500,表示错误:服务器错误-无法获取deimos_api_1
的容器
任何想法如何在dockerode中指定如何加入特定网络(此处为deimos_default)?
谢谢。
编辑:我正在使用docker 1.10.3,docker-compose 1.6.2和dockerode 2.2.10
我找到了它!
在dockerode启动过程中添加以下选项就可以了!
HostConfig:{
NetworkMode: deimos_default
}
在此处找到文档中的行:
I use docker-compose
to create a "park" of container inter-linked. Nothing special here, I have API/Frontend/Backend etc.. There is my docker-compose.yml
:
version: "2"
services:
api:
build: ./deimos_api/docker
ports:
- "39999:80"
volumes:
- ./deimos_api:/root/deimos_api
asset:
build: ./deimos_asset/docker
ports:
- "8000:80"
volumes:
- ./deimos_asset:/root/deimos_asset
depends_on:
- api
links:
- api
data-manager:
build: ./deimos_data_manager/docker
ports:
- "3333:3333"
volumes:
- ./deimos_data_manager:/root/deimos_data_manager
depends_on:
- api
links:
- api
backend:
build: ./deimos_backend/docker
ports:
- "1080:80"
volumes:
- ./deimos_backend:/root/deimos_backend
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- api
- asset
links:
- api
I just learn that create a "park" like this linked container on a new network. docker network ls
result :
NETWORK ID NAME DRIVER
448daa341d56 bridge bridge
c7feeeb44cd0 deimos_default bridge
1fc2d6132b86 none null
a80e41eab3cc host host
An other part of the project is to generated on the fly container. But I want that new container access to the deimos_api
container previously created by docker-compose
.
I'm using dockerode
(wrapper of Docker Remote API) in javascript with the following code :
docker.createContainer({
"Image": config.dockerGameServerImageName,
"name": config.gameServerPrefix + serverName,
"Volumes": {"/root/deimos_server":{}}
}, function (err, container) {
if(err){
console.log(err);
reject(err);
} else {
container.start({
"PortBindings": { "80/tcp": [{"HostPort": lastPortUsed+""}]},
"Binds":[config.deimosServerVolumePath+":/root/deimos_server"],
"Links":[config.dockerApiContainerName+":api"]
}, function (err, data) {if(err) {
console.log(err);
reject(err);
} else {
console.log("Create container " + serverName);
gameContainer[config.gameServerPrefix + serverName] = lastPortUsed+"";
resolv({
'serverPort':lastPortUsed,
'serverName':serverName
});
}
});
}
});
But I have the following error :
Error: HTTP code is 500 which indicates error: server error - Could not get container for deimos_api_1
Any idea how to specify in dockerode how to join a specific network (here deimos_default) ?
Thank You.
EDIT : I'm using docker 1.10.3, docker-compose 1.6.2 and dockerode 2.2.10
I find it !
Add following option to the dockerode start process did the tricks !
"HostConfig": {
"NetworkMode": "deimos_default"
}
Find the row in documentation here : https://docs.docker.com/engine/reference/api/docker_remote_api_v1.22/#create-a-container
这篇关于如何使用Docker远程API连接到现有的Docker网络(通过dockerode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!