docker-compose run --rm が遅く起動します。

docker-compose run --rm が遅く起動します。

私はdockerにいくつかのオーバーヘッドがあることを知っており、ローカルストレージと同じくらい速くしたくありませんが、2秒のオーバーヘッドはありますか?あまりにもそうですね…コンテナが実行されたら、実行自体はまったく同じようです。

$ time docker-compose run --rm php-cli php -i > /dev/null
docker-compose run --rm php-cli php -i > /dev/null  0,43s user 0,07s system 23% cpu 2,107 total

$ time php -i > /dev/null
php -i > /dev/null  0,04s user 0,01s system 98% cpu 0,050 total

単純なドッカーのhello-worldでさえ、適切だと思うよりも時間がかかります。

time docker run --rm hello-world > /dev/null
docker run --rm hello-world > /dev/null  0,07s user 0,02s system 9% cpu 0,869 total

コマンドを追跡しようとしましたが、ほとんどの時間wait4で停止します(Dockerデーモンが応答するのを待っていると思いますか?私は専門家ではないので修正してください)。役に立つ場合は出力の一部です。https://pastebin.com/pdA63zBi

これが予想される動作ですか、それとも設定に問題がありますか?

編集:きれいなphp-cli画像のstraceの概要は次のとおりです。

strace -tt -c -f -S time docker run --rm php:7.2-cli php -i > /dev/null
strace: Process 30557 attached
strace: Process 30558 attached
strace: Process 30559 attached
strace: Process 30560 attached
strace: Process 30561 attached
strace: Process 30562 attached
strace: Process 30563 attached
strace: Process 30565 attached
strace: Process 30566 attached
strace: Process 30567 attached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 71.53    0.039424          43       919       163 futex
 18.01    0.009927          47       211           pselect6
  6.56    0.003617          50        73           mmap
  1.27    0.000702           5       128           rt_sigaction
  0.41    0.000227           2       138           rt_sigprocmask
  0.34    0.000188           9        22           sigaltstack
  0.34    0.000188          17        11           gettid
  0.30    0.000165          14        12           munmap
  0.25    0.000138           4        38           mprotect
  0.22    0.000124          12        10           clone
  0.14    0.000076          76         1           readlinkat
  0.11    0.000062           1        87           sched_yield
  0.10    0.000053           3        17           openat
  0.09    0.000049           1        65           epoll_pwait
  0.09    0.000048           4        11           set_robust_list
  0.06    0.000032           3        10           epoll_ctl
  0.04    0.000024           1        34        10 read
  0.04    0.000020           3         6           fcntl
  0.03    0.000018           1        20           close
  0.02    0.000012          12         1           epoll_create1
  0.01    0.000006           3         2           lseek
  0.01    0.000004           0        13           fstat
  0.01    0.000004           0        48        47 newfstatat
  0.01    0.000003           0         7           write
  0.00    0.000002           0         5         2 connect
  0.00    0.000001           0         5           socket
  0.00    0.000001           0         3           getpeername
  0.00    0.000001           0         3           setsockopt
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         2         1 ioctl
  0.00    0.000000           0        10        10 access
  0.00    0.000000           0         2           getpid
  0.00    0.000000           0         1           shutdown
  0.00    0.000000           0         3           getsockname
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           getuid
  0.00    0.000000           0         1           arch_prctl
  0.00    0.000000           0         1           sched_getaffinity
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         1           prlimit64
  0.00    0.000000           0         1           getrandom
------ ----------- ----------- --------- --------- ----------------
100.00    0.055116                  1928       233 total

bashを実行してコンテナ内で同じことを行うと、futexシステムコールはほとんど効果がありません。正常に実行されます。

ベストアンサー1

ホストからコンテナへのコマンドの実行は、dockerデーモンによって処理されます。

exec -it [image ID] bash時間を再テストすると、次のようになります。

$ docker exec -it php bash
root@0fddab587d30:/# time php -i > /dev/null

real    0m0.029s
user    0m0.011s
sys     0m0.018s

stracedockerdを介してコンテナとしてホスト上で実行

$ strace -c -f -S name docker run -d --rm php:cli php -i > /dev/null
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.04    0.000073           7        10        10 access
  0.01    0.000025          25         1           arch_prctl
  0.04    0.000064          21         3           brk
  0.21    0.000359          45         8           clone
  0.09    0.000158           9        17           close
  0.02    0.000036           9         4         2 connect
  0.02    0.000027          27         1           epoll_create1
  0.04    0.000068          11         6           epoll_ctl
  3.97    0.006676          56       120           epoll_wait
  0.00    0.000000           0         1           execve
  0.03    0.000056          14         4           fcntl
  0.05    0.000082           6        13           fstat
 72.92    0.122754         128       959       161 futex
  0.00    0.000001           1         2           getpeername
  0.00    0.000000           0         1           getpid
  0.01    0.000022          22         1           getrandom
  0.00    0.000000           0         2           getsockname
  0.12    0.000199          22         9           gettid
  0.00    0.000004           4         1           getuid
  0.03    0.000044          22         2         1 ioctl
  0.01    0.000011           6         2           lseek
  0.78    0.001317          20        67           mmap
  0.49    0.000821          24        34           mprotect
  0.25    0.000427          39        11           munmap
  0.01    0.000013          13         1           open
  0.09    0.000154          11        14           openat
  0.02    0.000027          27         1           prlimit64
 17.57    0.029574          78       381           pselect6
  0.10    0.000166           7        23         6 read
  0.03    0.000046          46         1           readlinkat
  1.50    0.002522          20       128           rt_sigaction
  0.74    0.001241           9       132           rt_sigprocmask
  0.02    0.000032          32         1           sched_getaffinity
  0.30    0.000500          18        28           sched_yield
  0.10    0.000175          19         9           set_robust_list
  0.01    0.000025          25         1           set_tid_address
  0.00    0.000000           0         2           setsockopt
  0.19    0.000323          18        18           sigaltstack
  0.03    0.000044          11         4           socket
  0.13    0.000215           8        28        27 stat
  0.03    0.000049          10         5           write
------ ----------- ----------- --------- --------- ----------------
100.00    0.168330                  2056       207 total

コンテナでstraceを実行すると、php:cliより多くのシステムコールがあっても時間が大幅に短縮されます。futexここでシステムコールに注目する価値があるようです。

$ docker run -it --security-opt seccomp:unconfined php:cli bash
root@3bd1a83a2586:/# apt update && apt install strace

root@3bd1a83a2586:/# strace -c -f -S name php -i > /dev/null
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  4.38    0.000830          15        55        54 access
  0.03    0.000006           6         1           arch_prctl
  0.00    0.000000           0        15           brk
  2.73    0.000518           8        61           close
  0.00    0.000000           0         1           execve
  3.74    0.000708          12        61           fstat
  0.02    0.000003           0        18           futex
  0.00    0.000000           0         1           getcwd
  0.00    0.000000           0         2           getdents
  0.00    0.000000           0         3           getrandom
  0.00    0.000000           0         2           getrlimit
  0.00    0.000000           0         1         1 ioctl
  0.00    0.000000           0         2           lseek
  0.00    0.000000           0         5         1 lstat
  0.00    0.000000           0         1           madvise
 10.42    0.001974          14       145           mmap
  5.06    0.000959           9       108           mprotect
  0.24    0.000046           4        13           munmap
 10.34    0.001960          28        69         9 open
  3.45    0.000654          10        63           read
  0.00    0.000000           0        80           rt_sigaction
  0.00    0.000000           0         2           rt_sigprocmask
  0.00    0.000000           0         1           set_robust_list
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         1           socket
  0.23    0.000043           7         6         4 stat
  0.00    0.000000           0         1           sysinfo
  0.00    0.000000           0         1           uname
 59.36    0.011250           4      3037           write
------ ----------- ----------- --------- --------- ----------------
100.00    0.018951                  3757        69 total

おすすめ記事