How to connect to an existing docker network with docker remote API (through dockerode)

时间秒杀一切 提交于 2020-01-01 17:01:24

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/36607483/how-to-connect-to-an-existing-docker-network-with-docker-remote-api-through-doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!