Last active
February 19, 2023 13:14
-
-
Save leiless/a4558b67c9d7da7d64e32c00e06dda7a to your computer and use it in GitHub Desktop.
Check if the script is running in the Docker/Podman/K8S
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# If you using sh, please remove the `-o pipefail` | |
set -eufo pipefail | |
#set -x | |
check_in_container() { | |
if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ] && [ -z ${KUBERNETES_SERVICE_HOST+x} ]; then | |
echo "[ERR] This script should be run inside a container environment!" | |
exit 1 | |
fi | |
} | |
check_in_container |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://superuser.com/questions/1021834/what-are-dockerenv-and-dockerinit/1115029#1115029
containers/podman#648
https://stackoverflow.com/questions/36639062/how-do-i-tell-if-my-container-is-running-inside-a-kubernetes-cluster
https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash