容器和虚拟机 Container vs VM

六、容器和虚拟机 - 图1

容器不是Mini虚拟机

  • 容器其实是进程Containers are just processes
  • 容器中的进程被限制了对CPU内存等资源的访问
  • 当进程停止后,容器就退出了

容器的进程process

  • 显示容器的进程:docker container top <container ID>
    六、容器和虚拟机 - 图2

Note
以下是在Ubuntu20.04中演示,因Windows环境下的Docker和Linux具有一些差异。pstree 命令需要额外安装,可以使用 yum install psmisc 或者 sudo apt-get install psmisc 安装

  1. ~ docker container run -d nginx
  2. 57fe4033dd7e1e620a0b6a7b83b85d4f8f98772f0ce585624c384de254826fd0
  3. ~ docker container ls
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 57fe4033dd7e nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 80/tcp festive_proskuriakova
  6. ~ docker container top 57f
  7. UID PID PPID C STIME TTY TIME CMD
  8. root 7646 7625 0 12:14 ? 00:00:00 nginx: master process nginx -g daemon off;
  9. systemd+ 7718 7646 0 12:14 ? 00:00:00 nginx: worker process
  10. ~
  11. ~
  12. ~ ps -aef --forest
  13. UID PID PPID C STIME TTY TIME CMD
  14. root 1 0 0 May14 ? 00:00:00 /init
  15. root 157 1 0 May14 ? 00:00:00 /init
  16. root 523 157 0 May14 ? 00:02:32 \_ /usr/bin/dockerd -p /var/run/docker.pid
  17. root 545 523 0 May14 ? 00:25:55 | \_ containerd --config /var/run/docker/containerd/containerd.toml --log-level info
  18. root 7625 157 0 12:14 ? 00:00:00 \_ /usr/bin/containerd-shim-runc-v2 -namespace moby -id 57fe4033dd7e1e620a0b6a7b83b85d4f8f98772f0ce585624c384de254826fd0 -address /var/run/d
  19. root 7646 7625 0 12:14 ? 00:00:00 \_ nginx: master process nginx -g daemon off;
  20. systemd+ 7718 7646 0 12:14 ? 00:00:00 \_ nginx: worker process
  21. root 6442 1 0 May18 ? 00:00:00 /init
  22. root 6443 6442 0 May18 ? 00:00:00 \_ /init
  23. penxiao 6444 6443 0 May18 pts/2 00:00:01 \_ -zsh
  24. penxiao 7770 6444 0 12:15 pts/2 00:00:00 \_ ps -aef --forest
  25. ~
  26. ~ pstree -halps 7718
  27. init,1
  28. └─init,157
  29. └─containerd-shim,7625 -namespace moby -id 57fe4033dd7e1e620a0b6a7b83b85d4f8f98772f0ce585624c384de254826fd0 -address /var/run/docker/containerd/containerd.sock
  30. └─nginx,7646
  31. └─nginx,7718

Gain Access to the MobyLinux VM on Windows or MacOS

https://nickjanetakis.com/blog/docker-tip-70-gain-access-to-the-mobylinux-vm-on-windows-or-macos