How to check if running inside Docker?
To check if you are inside a docker container, run the command
cat /proc/1/cgroup | grep --color docker
If you get some result with docker in it, you are inside a docker container.
Example
See Docker
To check if you are inside a docker container, run the command
cat /proc/1/cgroup | grep --color docker
If you get some result with docker in it, you are inside a docker container.
Example
See Docker
This doesn’t work with cgroup 2.0, and it doesn’t work inside Docker Desktop on Mac.
root@8a1d175a0b6c:/# cat /proc/self/cgroup
0::/
The presence of a /.dockerenv file on the root filesystem in Linux is probably a more reliable way to know that you’re running inside a Docker container, but that doesn’t give you your container Id; it’s just an empty file.
There needs to be a less fragile way to 1) detect when you’re running in a container, and 2) get your own container Id. The Docker team really dropped the ball on this. At a minimum, they should have put the container Id inside /.dockerenv.