From 34bd9d4c0ca327488d04d85181632e8cf6bc38f9 Mon Sep 17 00:00:00 2001 From: Christopher Date: Sun, 2 Jun 2024 13:49:51 -0500 Subject: [PATCH] feat: Add script to check for Docker port usage --- check-for-docker-port/README.md | 5 ++++ check-for-docker-port/run.sh | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 check-for-docker-port/README.md create mode 100644 check-for-docker-port/run.sh diff --git a/check-for-docker-port/README.md b/check-for-docker-port/README.md new file mode 100644 index 0000000..1f80d19 --- /dev/null +++ b/check-for-docker-port/README.md @@ -0,0 +1,5 @@ +# Run command + +```bash +bash -c "$(wget -qLO - https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/check-for-docker-port/run.sh)" +``` diff --git a/check-for-docker-port/run.sh b/check-for-docker-port/run.sh new file mode 100644 index 0000000..cbf60e9 --- /dev/null +++ b/check-for-docker-port/run.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Function to check if jq is installed +check_jq_installed() { + if ! command -v jq &> /dev/null; then + echo "jq is required but it's not installed. Would you like to install it now? (yes/no)" + read answer + if [[ "$answer" == "yes" ]]; then + if [ -x "$(command -v apt-get)" ]; then + sudo apt-get update && sudo apt-get install -y jq + elif [ -x "$(command -v yum)" ]; then + sudo yum install -y jq + else + echo "Package manager not supported. Please install jq manually." + exit 1 + fi + else + echo "jq is not installed. Exiting." + exit 1 + fi + fi +} + +# Check if jq is installed +check_jq_installed + +# Check if a port number is provided as an argument +if [ -z "$1" ]; then + read -p "Enter the port number to search for: " port +else + port=$1 +fi + +# Check if the input is a valid number +if ! [[ "$port" =~ ^[0-9]+$ ]]; then + echo "Invalid port number. Please enter a valid number." + exit 1 +fi + +# Iterate through all containers and check if the specified port is used +echo "Searching for containers using port $port..." +for container in $(sudo docker ps -a -q); do + if sudo docker inspect $container | jq -e --arg port "$port/tcp" '.[0].NetworkSettings.Ports | has($port)'; then + status=$(sudo docker inspect -f '{{.State.Status}}' $container) + echo "Container ID $container is configured to use port $port and is currently $status" + fi +done + +echo "Search completed."