分类
devops

docker run 带-d和不带-d的区别

解释detach

--detach , -d       Run container in background and print container ID

不带-d

docker run nginx:1.19.4-alpine

pstree -s -a -g -p `ps -ef |grep "docker run nginx:1.19.4-alpine" |grep -v grep |awk '{print $2}'`
systemd,1,1 --switched-root --system --deserialize 22
  `-sshd,9891,9891 -D
      `-sshd,29889,29889
          `-sshd,29948,29889
              `-bash,29958,29958
                  `-docker,15132,15132 run nginx:1.19.4-alpine
                      |-{docker},15133,15132
                      |-{docker},15134,15132
                      |-{docker},15135,15132
                      |-{docker},15136,15132
                      |-{docker},15137,15132
                      |-{docker},15138,15132
                      |-{docker},15139,15132
                      |-{docker},15140,15132
                      |-{docker},15141,15132
                      `-{docker},15142,15132
## 关闭终端
systemd,1,1 --switched-root --system --deserialize 22
  `-docker,15132,15132 run nginx:1.19.4-alpine
      |-{docker},15133,15132
      |-{docker},15134,15132
      |-{docker},15135,15132
      |-{docker},15136,15132
      |-{docker},15137,15132
      |-{docker},15138,15132
      |-{docker},15139,15132
      |-{docker},15140,15132
      |-{docker},15141,15132
      `-{docker},15142,15132

带-d

docker run -d nginx:1.18.0-alpine
Unable to find image 'nginx:1.18.0-alpine' locally
1.18.0-alpine: Pulling from library/nginx
cbdbe7a5bc2a: Already exists
d1b1b4361acf: Pull complete
c6b64ab35715: Pull complete
6056107753b1: Pull complete
db9b4bdd7608: Pull complete
Digest: sha256:1d53eb2d7a5cf09f2208ccfefeaa91d637fbeb438c3c047abafc9d0124151da2
Status: Downloaded newer image for nginx:1.18.0-alpine
92607cb81bb6ee84800134297b1d4baf232a9c95949ddb7f346801ef10b48b95

pstree -sg -p `docker inspect -f '{{ .State.Pid }}' 92607cb81bb6ee84800134297b1d4baf232a9c95949ddb7f346801ef10b48b95`
systemd(1,1)---containerd(1353,1353)---containerd-shim(18119,18119)---nginx(18134,18134)-+-nginx(18201,18134)
                                                                                         |-nginx(18202,18134)
                                                                                         |-nginx(18203,18134)
                                                                                         `-nginx(18204,18134)

ps -l -f --forest -g `docker inspect -f '{{ .State.Pid }}' 92607cb81bb6ee84800134297b1d4baf232a9c95949ddb7f346801ef10b48b95`
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root     18134 18119  0  80   0 -  1496 -      17:23 ?        00:00:00 nginx: master process nginx -g daemon off;
5 S 101      18201 18134  0  80   0 -  1606 -      17:23 ?        00:00:00  \_ nginx: worker process
5 S 101      18202 18134  0  80   0 -  1606 -      17:23 ?        00:00:00  \_ nginx: worker process
5 S 101      18203 18134  0  80   0 -  1606 -      17:23 ?        00:00:00  \_ nginx: worker process
5 S 101      18204 18134  0  80   0 -  1606 -      17:23 ?        00:00:00  \_ nginx: worker process
# sudo systemctl stop containerd
# sudo systemctl start containerd
# 重启containerd之后,containerd-shim被systemd接管
systemd(1,1)---containerd-shim(18119,18119)---nginx(18134,18134)-+-nginx(18201,18134)
                                                                 |-nginx(18202,18134)
                                                                 |-nginx(18203,18134)
                                                                 `-nginx(18204,18134)

结论

带有-d的进程的父进程是containerd-shim,爷进程是containerd
不带-d的进程的父进程是bash(或者sh等),爷进程是sshd

其他

## 查看进程id,父进程id
ps -x -o pid,ppid,pgid,sid,tty,comm

ref

  • https://docs.docker.com/engine/reference/commandline/run/
  • https://superuser.com/questions/651111/what-is-the-definition-of-a-session-in-linux
  • https://aleiwu.com/post/cncf-runtime-landscape/