Draft — not published. This lesson is superseded and may be incomplete or wrong. The MCP API returns it with status: "superseded" so callers can filter it.

Ruby Path Resolution and Docker Port Allocation Issues

Domain: devops · Evidence E1 — Maintainer reviewed
rubydockerpath-resolutionport-allocationtroubleshootingshell-helpers

Summary

> **CORRECTION (this lesson is `status: superseded`; do not follow its Ruby steps).**

CORRECTION (this lesson is status: superseded; do not follow its Ruby steps). The Root Cause line for Issue 1 below is correct — require_relative resolves against the file that contains the call — but the Issue 1 Solution (Steps 1–3: pwd, cd into the script's directory, and the File.expand_path(File.dirname(__FILE__), …) rewrite) is wrong and cannot repair the failure: a cd does not change a file-anchored lookup, and the File.expand_path form is semantically what require_relative already does. The corrected diagnosis and fix live in lessons/core/ruby-require-relative-resolves-against-the-requiring-file.md, which supersedes this lesson. The Docker port half of this lesson is unaffected and is covered by lessons/contrib/port-already-allocated-find-and-free-the-holder.md. This file is kept as-is for history; lessons are immutable once merged.

Problem

This lesson addresses two distinct but commonly encountered issues in development environments:

  1. A Ruby shell helper script (try.rb) fails during initialization with a LoadError indicating that require_relative cannot find the lib/tui directory. Ruby version and standalone code work fine, suggesting the issue is related to the script's working directory or path resolution.
  2. After restarting a Docker stack, docker compose up fails with a port allocation error indicating that port 8080 is already in use on the host system.

Root Cause

Issue 1: Ruby Path Resolution

The require_relative directive in Ruby loads files relative to the file containing the directive, not relative to the current working directory. When the script is run from a different directory than where it's located, the relative path to lib/tui becomes invalid, causing the LoadError.

Issue 2: Docker Port Allocation

When a Docker stack is restarted, containers may not immediately release the host ports they were bound to. This can happen if the container process doesn't shut down cleanly or if there's a delay in releasing network resources. As a result, when attempting to restart the stack, Docker cannot bind to the already allocated port.

Solution

Issue 1: Ruby Path Resolution

Step 1: Verify Current Working Directory

Before running the script, ensure you're in the correct directory:

pwd

Step 2: Run Script from Correct Directory

Execute the script from its containing directory:

cd /path/to/script/directory
ruby try.rb

Step 3: Modify Script for Flexibility (Optional)

If you need to run the script from other directories, modify it to use absolute paths or adjust the relative path:

# At the top of try.rb
require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'tui'))

Issue 2: Docker Port Allocation

Step 1: Identify Process Using the Port

Find which process is using port 8080:

lsof -i :8080
# On Linux
sudo netstat -tulpn | grep :8080

Step 2: Terminate the Conflicting Process

Kill the process that's holding the port:

# Using PID from previous command
kill -9 <PID>

Step 3: Clean Up Docker Resources

Remove any stopped containers and networks that might be holding resources:

docker-compose down -v --remove-orphans
docker system prune -f

Verification

Issue 1: Ruby Path Resolution

After implementing the solution, verify the script runs correctly:

cd <LOCAL_DIR> && ruby try.rb

Issue 2: Docker Port Allocation

After resolving the port conflict, verify the stack starts successfully:

docker compose up

Check that the container is running and accessible on port 8080:

docker ps | grep -E '8080/tcp'

Notes

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons