Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Created April 10, 2024 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonRichards/cae2204bf97da9dbf2ccff27159e3e4c to your computer and use it in GitHub Desktop.
Save DonRichards/cae2204bf97da9dbf2ccff27159e3e4c to your computer and use it in GitHub Desktop.
Troubleshooting isle-dc build conflicts.
#!/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