How to check if a process is running inside docker container

后端 未结 8 1260
逝去的感伤
逝去的感伤 2020-11-30 23:08

[Updated1] I have a shell which will change TCP kernel parameters in some functions, but now I need to make this shell run in Docker container, that means, the shell need to

8条回答
  •  -上瘾入骨i
    2020-11-30 23:22

    Based on Dan Walsh's comment about using SELinux ps -eZ | grep container_t, but without requiring ps to be installed:

    $ podman run --rm fedora:31 cat /proc/1/attr/current
    system_u:system_r:container_t:s0:c56,c299
    $ podman run --rm alpine cat /proc/1/attr/current
    system_u:system_r:container_t:s0:c558,c813
    $ docker run --rm fedora:31 cat /proc/1/attr/current
    system_u:system_r:container_t:s0:c8,c583
    $ cat /proc/1/attr/current
    system_u:system_r:init_t:s0
    

    This just tells you you're running in a container, but not which runtime.

    Didn't check other container runtimes but https://opensource.com/article/18/2/understanding-selinux-labels-container-runtimes provides more info and suggests this is widely used, might also work for rkt and lxc?

提交回复
热议问题