How can multiple docker containers execute a command at the same time?

  docker, question

I need to implement a function: create multiple docker containers at the same time and execute a command in these containers at the same time.

My method is to use pssh (a python tool that can execute commands on multiple servers in parallel) to connect multiple containers and implement simultaneous execution. Like this:

pssh -h ip.txt -P uptime

Txt contains the server’s ip list, which is the container’s ip list. Uptime is a command that needs to be executed simultaneously within a container.

In order to verify whether it is executed at the same time, I used the date command to print the current system time (milliseconds) before executing specific commands (such as uptime). I found that the difference between the first container and the tenth container is about 50ms:

图片描述

This means that if I need to execute the date command once before and after the command is executed to calculate the execution time of the command, then when the specific command is executed, the first container and the tenth container are 50ms apart, and if the specific command execution time is only a few ms, then simultaneous execution cannot be realized.

And when I cleaned up the cache, and then executed, only 3mm difference:

图片描述

The contents of cache_clear are as follows:

sudo sync
 sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

Does it look like the date command will be affected by caching?

The disk was replaced with ssd(cpu, memory, system and kernel are all the same), and the same tests were conducted. It was found that when the cache was not cleared, the first container and the last container were 40+ms apart:

图片描述

To sum up, only in case2 can my needs be met:

图片描述

If pssh itself cannot execute within a few ms at the same time (that is, the date command of the first container is executed 50ms earlier than the command of the tenth container), then in case2, even if the cache is cleared, it should not be only 3ms worse.So I guess pssh itself can achieve parallelism within a few ms.(In other words, the execution time of date is only a few ms short at most). However, the completion time is different in the date execution process, so the system time obtained differs by tens of ms.

Since I need to measure the execution time of the command, I have to execute date before and after the command is executed, which results in that the time measured in the other 3 cases is not the time when all containers execute the command in real parallel except case2.

Of course, I have also tried to use time to directly calculate the command execution time, but the result is not essentially different from the time calculated by date. Moreover, using time cannot verify whether all containers execute commands (such as uptime) at the same time (only a few ms away). Although date or time is not used, specific commands (such as uptime) should be executed simultaneously, but now I need to test the execution time of the commands, so I cannot avoid the two commands.

Now I hope to be able to eliminate this effect under SSD, but I don’t know why date or time has this effect in the implementation process, and whether there are other timing commands? Or other methods to implement concurrent execution of docker multi-container commands? If my guess is wrong, that is to say, pssh cannot guarantee simultaneous execution within a few ms, then why is case2 only 3ms short? I hope my predecessors can give me some guidance or suggestions.

Use dockerfile or docker-compose to execute your own shell file. This is the only solution for the time being.