execute a command within docker swarm service

后端 未结 11 1800
眼角桃花
眼角桃花 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:18

    See addendum 2...

    Example of a oneliner for entering the database my_db on node master:

    DB_NODE_ID=master && docker exec -it $(docker ps -q -f name=$DB_NODE_ID) mysql my_db

    In case you want to configure, say max_connections:

    DB_NODE_ID=master && $(docker exec -it $(docker ps -q -f name=$DB_NODE_ID) mysql -e "SET GLOBAL max_connections = 1000") && docker exec -it $(docker ps -q -f name=$DB_NODE_ID) mysql my_db

    This approach allows to enter all database nodes (e.g. slaves) just by setting the DB_NODE_ID variable accordingly.

    Example for slave s2:

    DB_NODE_ID=s2 && docker exec -it $(docker ps -q -f name=$DB_NODE_ID) mysql my_db

    or

    DB_NODE_ID=s2 && $(docker exec -it $(docker ps -q -f name=$DB_NODE_ID) mysql -e "SET GLOBAL max_connections = 1000") && docker exec -it $(docker ps -q -f name=$DB_NODE_ID) mysql my_db

    Put this into your KiTTY or PuTTY configuration for master / s2 under Data/Command and you are set.


    As an addendum:

    The old, non swarm mode version reads simply

    docker exec -it master mysql my_db
    

    resp.

    DB_ID=master && $(docker exec -it $DB_ID mysql -e "SET GLOBAL max_connections = 1000") && docker exec -it $DB_ID mysql tmp
    

    Addendum 2:

    As it turned out by example, the term docker ps -q -f name=$DB_NODE_ID may return wrong values under certain conditions.

    The following approach works correctily:

    docker ps -a  | grep "_$DB_NODE_ID." | awk '{print $1}'
    

    You may substitute the examples above accordingly.


    Addendum 3:

    Well, these terms look awful and they certainly are painful to type, so you may want to ease your work. On Linux, everybody knows how to do this. On Windws, you may want to use AHK.

    This is the AHK term I use:

    :*:ii::DB_NODE_ID=$(docker ps -a  | grep "_." | awk '{{}print $1{}}') && docker exec -it $id ash{Left 49} 
    

    So when I type ii -- which is as simple as it can get -- I get the desired term with the cursor in place and just have to fill in the container name.

提交回复
热议问题