如何判断进程是在主机中运行还是在 Docker 容器中运行

如何判断进程是在主机中运行还是在 Docker 容器中运行

我有很多gitlab进程,但gitlab没有安装在主机上:

» ps -ef | grep -c gitlab
61

乍一看,这些过程类似于正常过程:

» ps -ef | grep gitlab | head
root      1161     1  0 06:24 ?        00:00:06 /usr/local/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runner
root      3379  3164  0 06:24 ?        00:00:00 runsvdir -P /opt/gitlab/service log: ...........................................................................................................................................................................................................................................................................................................................................................................................................
root      3390  3389  0 06:24 ?        00:00:00 svlogd -tt /var/log/gitlab/sshd
root      3813  3164  0 06:24 ?        00:00:00 /bin/bash /opt/gitlab/bin/gitlab-ctl tail
root      3814  3813  0 06:24 ?        00:00:00 /opt/gitlab/embedded/bin/ruby /opt/gitlab/embedded/bin/omnibus-ctl gitlab /opt/gitlab/embedded/service/omnibus-ctl* tail
root      3824  3814  0 06:24 ?        00:00:00 sh -c find /var/log/gitlab -type f -not -path */sasl/* | grep -E -v '(config|lock|@|gzip|tgz|gz)' | xargs tail --follow=name --retry
root      3828  3827  0 06:24 ?        00:00:00 tail --follow=name --retry /var/log/gitlab/gitlab-shell/gitlab-shell.log /var/log/gitlab/redis/state /var/log/gitlab/redis/current /var/log/gitlab/sidekiq/state /var/log/gitlab/sidekiq/current /var/log/gitlab/postgres-exporter/state /var/log/gitlab/postgres-exporter/current /var/log/gitlab/redis-exporter/state /var/log/gitlab/redis-exporter/current /var/log/gitlab/postgresql/state /var/log/gitlab/postgresql/current /var/log/gitlab/unicorn/unicorn_stdout.log /var/log/gitlab/unicorn/state /var/log/gitlab/unicorn/unicorn_stderr.log /var/log/gitlab/unicorn/current /var/log/gitlab/gitlab-rails/gitlab-rails-db-migrate-2018-02-28-08-06-37.log /var/log/gitlab/gitlab-rails/grpc.log /var/log/gitlab/gitlab-rails/githost.log /var/log/gitlab/gitlab-rails/application.log /var/log/gitlab/gitlab-rails/gitlab-rails-db-migrate-2018-02-20-15-14-44.log /var/log/gitlab/gitlab-rails/production_json.log /var/log/gitlab/gitlab-rails/api_json.log /var/log/gitlab/gitlab-rails/production.log /var/log/gitlab/nginx/gitlab_error.log /var/log/gitlab/nginx/error.log /var/log/gitlab/nginx/access.log /var/log/gitlab/nginx/current /var/log/gitlab/nginx/gitlab_access.log /var/log/gitlab/node-exporter/state /var/log/gitlab/node-exporter/current /var/log/gitlab/logrotate/current /var/log/gitlab/sshd/current /var/log/gitlab/prometheus/state /var/log/gitlab/prometheus/current /var/log/gitlab/gitlab-monitor/state /var/log/gitlab/gitlab-monitor/current /var/log/gitlab/gitaly/state /var/log/gitlab/gitaly/current /var/log/gitlab/gitlab-workhorse/state /var/log/gitlab/gitlab-workhorse/current
root      3838  3379  0 06:24 ?        00:00:00 runsv gitlab-monitor
root      3843  3379  0 06:24 ?        00:00:00 runsv gitlab-workhorse
root      3845  3833  0 06:24 ?        00:00:00 svlogd -tt /var/log/gitlab/postgresql

但它们是 docker 进程:

» docker ps 
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                    PORTS                                                                  NAMES
8d0321cae62a        nginx                     "nginx -g 'daemon ..."   3 days ago          Up 26 minutes             0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp                               nginx
e97521e04a5d        apt-cacher                "/bin/sh -c 'chmod..."   4 days ago          Up 26 minutes             0.0.0.0:3142->3142/tcp                                                 apt-cacher
69608a09b0e7        muccg/devpi:latest        "/docker-entrypoin..."   5 days ago          Up 26 minutes             0.0.0.0:3141->3141/tcp                                                 devpi
83d2b5accc94        registry:2                "/entrypoint.sh /e..."   6 days ago          Up 26 minutes             0.0.0.0:45500->5000/tcp                                                registry
76a82938a1a2        redis                     "docker-entrypoint..."   7 days ago          Up 26 minutes             0.0.0.0:6379->6379/tcp                                                 redis
5d58e1fd47f1        gitlab/gitlab-ee:latest   "/assets/wrapper"        7 days ago          Up 26 minutes (healthy)   0.0.0.0:45022->22/tcp, 0.0.0.0:45080->80/tcp, 0.0.0.0:45443->443/tcp   gitlab

如何轻松列出仅主机的进程,使用:

  • ps
  • top

答案1

首先使用以下命令获取容器内的主进程id:

$ docker inspect -f '{{.State.Pid}}' <container id>

获取进程 ID 后,您可以从 ps 输出中排除这些进程 ID。示例如下:

$ docker inspect -f '{{.State.Pid}}' portainer
5580

5580从 ps 输出中排除一行

$ ps aux | grep -v 5580

您可以轻松创建一个脚本来循环运行正在运行的容器。

相关内容