docker-headless-vnc-container icon indicating copy to clipboard operation
docker-headless-vnc-container copied to clipboard

vnc_startup.sh fails when running in AWS ECS

Open fountopoulos opened this issue 10 months ago • 0 comments

Image:
Dockerfile.debian-xfce-vnc

Tag:
latest

Short Overview:
The vnc_startup.sh script fails to work correctly in AWS ECS because it uses the hostname -i command, which does not work in ECS environments. In AWS ECS, the hostname -i command fails with the error:

hostname: Name or service not known

This happens due to ECS's networking configurations. To mitigate this, we should implement a fallback to 127.0.0.1 when hostname -i fails.

Detailed Error Description:
In AWS ECS, the hostname -i command fails to return a valid IP address due to the network configuration of the container instances. As a result, the vnc_startup.sh script breaks, causing issues when attempting to start the VNC server.

Proposed Solution:
To fix this, I suggest modifying the script to gracefully handle the failure of hostname -i and fall back to 127.0.0.1 instead of terminating or causing errors. I recommend replacing lines 63-64 of the vnc_startup.sh script located at https://github.com/ConSol/docker-headless-vnc-container/blob/ae28fa8ad10f93e19ab11134bb5721a2efabf8b2/src/common/scripts/vnc_startup.sh#L63

with the following code:

## resolve_vnc_connection
# Function to get the container's IP address with a fallback to 127.0.0.1
# This function attempts to retrieve the IP using 'hostname -i' and handles failure gracefully.
# In AWS ECS, 'hostname -i' may fail with "hostname: Name or service not known"
# due to ECS networking configurations, which is why we fall back to 127.0.0.1.
get_ip_address() {
  local ip
  # Temporarily disable 'set -e' to allow handling the error gracefully
  set +e
  ip=$(hostname -i 2>/dev/null)
  if [ $? -ne 0 ]; then
    # Error case: If 'hostname -i' fails, capture the error message and print a warning
    ERROR_MESSAGE=$(hostname -i 2>&1) # Capture the error message
    ip="127.0.0.1"
    echo "WARNING: Unable to determine IP using 'hostname -i'. Falling back to $ip. Error: $ERROR_MESSAGE" >&2
  fi
  # Re-enable 'set -e' after handling the error
  set -e
  echo "$ip"
}

echo "VNC Server will start on IP: $(get_ip_address)"

Additional Information:

  • Error Message Example:
    hostname: Name or service not known
    
  • Reason for Change: The hostname -i command fails in AWS ECS, so we need to fall back to 127.0.0.1 for the IP address to ensure the script works in ECS environments.

Note:
I was unable to create a pull request (PR) due to an "Access Denied" error on my account. If possible, please consider applying this change directly to the repository.

fountopoulos avatar Feb 03 '25 15:02 fountopoulos