fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.util.stream.Stream;
  9.  
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone {
  13. public static void main(String[] args) throws java.lang.Exception {
  14. System.out.println("Am I running inside docker? " + Ideone.isRunningInsideDocker());
  15. }
  16.  
  17. public static Boolean isRunningInsideDocker() {
  18.  
  19. try (Stream < String > stream =
  20. Files.lines(Paths.get("/proc/1/cgroup"))) {
  21. return stream.anyMatch(line -> line.contains("docker"));
  22. } catch (IOException e) {
  23. return false;
  24. }
  25. }
  26. }
Success #stdin #stdout 0.15s 2188288KB
stdin
Standard input is empty
stdout
Am I running inside docker? false