-1

I am looking for C specific function to return the container name if it get executed inside the container; Do we have anything of such?

Currently I am trying to get it by reading the file /proc/1/cgroup

@ container:

      $> cat /proc/1/cgroup 
      8:net_cls:/lxc/Container1
      7:freezer:/lxc/Container1
      6:devices:/lxc/Container1
      5:memory:/lxc/Container1
      4:cpuacct:/lxc/Container1
      3:ns:/Container1
      2:debug:/lxc/Container1
      1:cpuset:/lxc/Container1

@ Host:

      $> cat /proc/1/cgroup 
      8:net_cls:
      7:freezer:
      6:devices:
      5:memory:
      4:cpuacct:
      3:ns:
      2:debug:
      1:cpuset:

With this we can get the container name; here it is "container1" on executing the programm insider the container1 otherwise the function returns "NULL" on executing the same at host.

Viswesn
  • 4,674
  • 2
  • 28
  • 45

1 Answers1

0

I don't think there any such program exist what you are looking for in "C" langauage.

But you can write a simple C program on your own to check the existence of docker init file named "/.dockerinit". This file is present inside every docker container (Docker creates it).

If this file exist, you can print the host name.

spectre007
  • 1,489
  • 11
  • 18