0

I need to check is my springboot is running in a docker container or not. I am doing it as following. I don't know if .dockerenv is always on root or not. If it is not correct. How to do it by java?

public static boolean isRunningInsideDocker() {
    if(Files.exists(Path.of("/.dockerenv"))){
        return true;
    }else{
        return false;
    }
} 
WJS
  • 36,363
  • 4
  • 24
  • 39
Masi Boo
  • 635
  • 1
  • 10
  • 24
  • I tired it and isRunningInsideDocker always return flase even under continaer. – Masi Boo Jan 11 '23 at 16:15
  • Did you look at the content of `/proc/1/cgroup`? Maybe in your container runtime the content differs, but it is certainly possible to see if you are running in a container using that information. – Josef Jan 11 '23 at 16:18
  • cat /proc/1/cgroup in real ubuntu `0::/init.scope`. And in docker it is `0::/`. So his comment doens't matach. – Masi Boo Jan 11 '23 at 16:20
  • Also reaing the file from Java mgiht be diffcult becuse of root access and permision matter. – Masi Boo Jan 11 '23 at 16:26
  • In no container runtime I know of, it is `0::/` in docker. Are you sure you are running in docker and using the official docker runtime? Also reading the information is allowed for all users and this is not a actual file but process information provided by the kernel! – Josef Jan 11 '23 at 16:28
  • Tasted for 3 diff docker type as `FROM ubuntu:latest`, `FROM ubuntu:latest` and `FROM postgres:10-alpine`. All has same cat ` cat /proc/1/cgroup` return `0::/` – Masi Boo Jan 11 '23 at 17:59

0 Answers0