Created
April 10, 2024 17:57
-
-
Save DonRichards/cae2204bf97da9dbf2ccff27159e3e4c to your computer and use it in GitHub Desktop.
Troubleshooting isle-dc build conflicts.
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 | |
DOCKER_COMPOSE_FILE="docker-compose.yml" | |
# Check if docker-compose file exists | |
if [ ! -f "$DOCKER_COMPOSE_FILE" ]; then | |
echo "Docker Compose file not found at $DOCKER_COMPOSE_FILE" | |
exit 1 | |
fi | |
# Extract ports from the 'ports' section of 'traefik' service | |
TRAFFIC_PORTS=$(awk '/traefik:/{f=1} f==1&&/ports:/{p=1;next} /ports:/{p=0} p&&/published:/{print $2}' $DOCKER_COMPOSE_FILE | tr -d '"') | |
# Extract ports from Traefik labels for load balancer server port | |
LABEL_PORTS=$(grep -oP 'traefik.http.services.\w+.loadbalancer.server.port: "\K\d+' $DOCKER_COMPOSE_FILE) | |
# Combine and deduplicate port lists | |
ALL_PORTS=$(echo -e "$TRAFFIC_PORTS\n$LABEL_PORTS" | sort | uniq) | |
# Check if any ports are found | |
if [ -z "$ALL_PORTS" ]; then | |
echo "No ports found in $DOCKER_COMPOSE_FILE" | |
exit 1 | |
fi | |
echo "Ports found in $DOCKER_COMPOSE_FILE: $ALL_PORTS" | |
# Check each port to see if it is already in use and by which process | |
for PORT in $ALL_PORTS; do | |
# Use lsof to check if the port is in use and extract the process using it | |
PROCESS_INFO=$(lsof -iTCP:$PORT -sTCP:LISTEN -Fpcn) | |
if [ ! -z "$PROCESS_INFO" ]; then | |
echo "Port $PORT is already in use by:" | |
# Extract and format the command and PID from the lsof output | |
COMMAND=$(echo "$PROCESS_INFO" | grep '^c' | cut -c2-) | |
PID=$(echo "$PROCESS_INFO" | grep '^p' | cut -c2-) | |
NAME=$(echo "$PROCESS_INFO" | grep '^n' | cut -c2-) | |
if [ ! -z "$COMMAND" ] && [ ! -z "$PID" ]; then | |
echo "$COMMAND (PID: $PID) listening on $NAME" | |
else | |
echo "Process details unavailable" | |
fi | |
else | |
echo "Port $PORT is available." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment