How to determine if a process runs inside lxc/Docker?

后端 未结 16 1390
情歌与酒
情歌与酒 2020-11-28 18:15

Is there any way to determine if a process (script) runs inside an lxc container (~ Docker runtime)? I know that some programs are able to detect whether they run inside a v

16条回答
  •  青春惊慌失措
    2020-11-28 19:07

    I have translated JJC's answer into ruby

    def in_docker
      File.open('/proc/1/cgroup', 'rt') do |f|
        contents = f.read
        return contents =~ /docker/i || contents =~ /kubepod/i
      end
    rescue StandardError => e
      p 'Local development'
      p e
      false
    end
    

提交回复
热议问题