How can a script detect whether it is running inside a container?
#!/bin/sh
if [ ... ]; then # ?
echo 'running in container'
else
echo 'running on host'
fi
This is one way with bash
:
#!/bin/bash
in_docker(){
local cgroup=/proc/self/cgroup
test -f $cgroup && [[ "$(<$cgroup)" = *:cpuset:/docker/* ]]
}
if in_docker; then
echo 'running in container'
else
echo 'running on host'
fi
You need to convert to sh
syntax if you don't have bash
in your container.