Last active
April 1, 2022 15:02
-
-
Save Bitnik212/57041ab0ba05f764d4b8140e2d3b2859 to your computer and use it in GitHub Desktop.
Check docker container port and stop if exist
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 | |
function checkDockerContainer() { | |
result=$(docker inspect --format="{{json .}}" $1 | jq '.NetworkSettings.Ports["'${2}'/tcp"][0].HostPort') | |
qoute='"' | |
if [[ "$result" == $qoute"$2"$qoute ]]; then | |
echo true | |
else | |
echo false | |
fi | |
} | |
function handleDockerContainer() { | |
if $2 ; then | |
docker stop $1 | |
fi | |
} | |
containers=$(docker container ls -a -q) | |
for container in $containers | |
do | |
handleDockerContainer $container $(checkDockerContainer $container $1) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment