execute a command within docker swarm service

后端 未结 11 1799
眼角桃花
眼角桃花 2020-12-04 15:16
  1. Initialize swarm mode:

    root@ip-172-31-44-207:/home/ubuntu# docker swarm init --advertise-addr 172.31.44.207
    
    Swarm initialized: current node (4mj61o         
    
    
            
11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 16:15

    Using the Docker API

    Right now, Docker does not provide an API like docker service exec or docker stack exec for this. But regarding this, there already exists two issues dealing with this functionality:

    • github.com - moby/moby - Docker service exec
    • github.com - docker/swarmkit - Support for executing into a task

    (Regarding the first issue, for me, it was not directly clear that this issue deals with exactly this kind of functionality. But Exec for Swarm was closed and marked as duplicate of the Docker service exec issue.)

    Using Docker daemon over HTTP

    As mentioned by BMitch on run docker exec from swarm manager, you could also configure the Docker daemon to use HTTP and than connect to every node without the need of ssh. But you should protect this using TLS authentication which is already integrated into Docker. Afterwards you would be able to execute the docker exec like this:

    docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem \
        -H=$HOST:2376 exec $containerId $cmd
    

    Using skopos-plugin-swarm-exec

    There exists a github project which claims to solve the problem and provide the desired functionality binding the docker daemon:

    docker run -v /var/run/docker.sock:/var/run/docker.sock \
        datagridsys/skopos-plugin-swarm-exec \
        task-exec   [...]
    

    As far as I can see, this works by creating another container at same node where the container reside where the docker exec should by executed on. On this node this container mounts the docker daemon socket to be able to execute docker exec there locally.
    For more information have a look at: skopos-plugin-swarm-exec

    Using docker swarm helpers

    There is also another project called docker swarm helpers which seems to be more or less a wrapper around ssh and docker exec.

    Reference:

    • https://github.com/docker/swarmkit/issues/1895#issuecomment-302147604
    • https://github.com/docker/swarmkit/issues/1895#issuecomment-358925313

提交回复
热议问题