Search code examples
dockerdocker-composedocker-swarm

How do I order a container in the worker node of the docker-swarm?


I built one manager node and one worker node with docker-swarm.

A project was created through docker-compose.

I wanted to command the container "A" in the worker node from the manager node.

So I entered the command:

docker exec -it A mkdir /home/test

but it printed out that the container name was not found.

How can I execute the command from the manager node to the container on the worker node?

NOTE. Currently, I am bypassing this problem through ssh and wc commands. Is there any other way?


Solution

  • A lot of docker resources, such as containers and volumes, are not global and can't be accessed from a swarm manager, only from the manager that hosts the resource.

    This means that accessing the resource is a two step process - typically interrogating the swarm managers for some swarm resource (such as a service) that gives you information about local resource of interest: (such as a node and container id).

    To actually access each node, ssh is the correct way, but docker can be used directly. DOCKER_HOST, or a -H parameter can pass a ssh uri

    docker -H ssh://user@node exec A /bin/sh
    

    Or you can create and use named docker contexts:

    #Create a context
    docker create context node1 --description "Worker1" --docker "host=ssh://user@node1"
    #Various ways of using the context
    docker -c node1 exec A /bin/sh
    DOCKER_CONTEXT=node1 docker exec A /bin/sh
    docker context use node1
    docker exec A /bin/sh
    docker context use default
    

    IDK if it is necessary, but it will certainly help if you have setup the ssh account used for no-password access (by installing the pub key etc).