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
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
2
Answers
The
/.dockerenv
file always exists in a docker container, so I check that:But note that file is an artefact of an older docker design, and could be removed in a future docker version. So this is a workaround, rather than a solution.
This is one way with
bash
:You need to convert to
sh
syntax if you don’t havebash
in your container.