Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(MacOS) Ports are not available: 5900 #147

Open
p-i- opened this issue Nov 1, 2024 · 4 comments · May be fixed by #148
Open

(MacOS) Ports are not available: 5900 #147

p-i- opened this issue Nov 1, 2024 · 4 comments · May be fixed by #148

Comments

@p-i-
Copy link

p-i- commented Nov 1, 2024

Executing the (minimally tweaked) docker run command from the README.md:

docker run -d \
    -e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
    -v "$(pwd)":/host \
    -p 5900:5900 \
    -p 8501:8501 \
    -p 6080:6080 \
    -p 8080:8080 \
    --name "$CONTAINER_NAME" \
    -it "$IMAGE_NAME"

28e..43e
docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5900 -> 0.0.0.0:0: listen tcp 0.0.0.0:5900: bind: address already in use.

Inspecting, ...

> lsof -i :5900
Password:
COMMAND PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
launchd   1 root   11u  IPv6 0xc679adcb0b88da06      0t0  TCP *:rfb (LISTEN)
launchd   1 root   12u  IPv4 0xdaeb2706f944b831      0t0  TCP *:rfb (LISTEN)
launchd   1 root   14u  IPv6 0xc679adcb0b88da06      0t0  TCP *:rfb (LISTEN)
launchd   1 root   15u  IPv4 0xdaeb2706f944b831      0t0  TCP *:rfb (LISTEN)

It turns out that I have to disable screen-sharing on macOS to free this port:

Screenshot 2024-11-01 at 20 42 30

I think it's a mistake to re-use a port that macOS has, by default, reserved, given that you are free to use ANY port you wish.

It does seem odd to me that in 2024 the best solution still seems to be "Pick a random 16-bit integer and hope nobody else on your OS picked the same one". Very retro. hey ho...

I think it would make much more sense to have a .ports file and make the ports dynamic. And to use different numbers for external and internal. That would make the code easier to work with.

Stevo-G-Swag added a commit to Stevo-G-Swag/anthropic-quickstarts that referenced this issue Nov 2, 2024
Fixes anthropics#147

Implement dynamic port allocation to avoid conflicts with reserved ports.

* **computer-use-demo/image/x11vnc_startup.sh**
  - Add functions to check port availability and find an available port.
  - Dynamically assign the VNC server port instead of hardcoding port 5900.
  - Update the `netstat` command to use the dynamically assigned port.

* **computer-use-demo/image/novnc_startup.sh**
  - Add functions to check port availability and find an available port.
  - Dynamically assign the VNC server port instead of hardcoding port 5900.
  - Modify the `--vnc` option to use the dynamic port.

* **computer-use-demo/image/port_check.sh**
  - Create a script to check port availability.
  - Use `lsof` to check if a port is in use.
  - Return an available port if the specified one is in use.

* **computer-use-demo/.ports**
  - Maintain a list of reserved ports.
  - Include common ports used by macOS and other systems.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/anthropics/anthropic-quickstarts/issues/147?shareId=XXXX-XXXX-XXXX-XXXX).
@p-i-
Copy link
Author

p-i- commented Nov 2, 2024

Ok, so here's what I've done.

pi@πlocal ~/demeisen_gen2/computer-use-demo main
> cat .ports
# INTERNAL ports end in 1 (think 1 ~ I for Internal), EXTERNAL ports end in 0
PORT_VNC_INTERNAL=5901
PORT_VNC_EXTERNAL=5900

PORT_NOVNC_INTERNAL=6081
PORT_NOVNC_EXTERNAL=6080

PORT_HTTP_INTERNAL=8081
PORT_HTTP_EXTERNAL=8080
PORT_STREAMLIT_INTERNAL=8501
PORT_STREAMLIT_EXTERNAL=8500

And I've tweaked entrypoint.sh:

pi@πlocal ~/demeisen_gen2/computer-use-demo main
> cat image/entrypoint.sh
#!/bin/bash
set -e

# Source and export all variables from .ports
set -a
source /host/computer-use-demo/.ports || exit 1
set +a

./start_all.sh
./novnc_startup.sh

# Generate index.html from template
envsubst < static_content/index.html.template > static_content/index.html
:

Now, as all these port variables are now in the shell environment, the subservient ?_startup.sh scripts can all use them, as can any Python script.

The only gotcha is that static_content/index.html uses a port, so I've moved that to a .template file, which entrypoint.sh renders (as you can see above).

Another benefit of what I've done is to separate EXTERNAL from INTERNAL ports. That's important for code-clarity, otherwise you have two separate objects that happen to have the same value only being referenced by that value. Confusing.

Finally grep for all the 4 hardcoded initial ports to make sure you've replaced EVERY SINGLE HARDCODED reference, and you should be good to go.

There's one gotcha though. Does this .ports go INTO the docker image via COPY in the Dockerfile? For me no, as I'm mounting the containing host-folder, so that the container can see its self.

demeisen-avatar added a commit to demeisen-avatar/anthropic-quickstarts that referenced this issue Nov 3, 2024
- Add .ports configuration file for clean port mapping
- Separate internal from external ports
- Add port availability validation
- Update all components to use configured ports
- Add gettext-base for template processing

This change prevents port conflicts when running multiple instances
and provides a cleaner separation between internal and external ports.

Fixes anthropics#147
jwalsh added a commit to defrecord/anthropic-quickstarts that referenced this issue Nov 3, 2024
- Check for existing container ports (Streamlit/NoVNC)
- Add helpful warning with docker commands
- Auto-retry for commonly conflicting ports (VNC/HTTP)
- Continue execution to allow multiple instances

Related to anthropics#147
@IgorGanapolsky
Copy link

Hmm, I have screen sharing off on my Mac, and still get this error.

@tms1337
Copy link

tms1337 commented Nov 5, 2024

ok fixed it by:

  1. clearing site data for localhost:8080
  2. including more ports to open (see cmd below)

full cmd:

docker run \
  -p 5900:5900 \
  -p 6080:6080 \
  -p 8080:8080 \
  -p 8501:8501 \
  -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
  -it ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest

cc @IgorGanapolsky

@mattlittlecfx
Copy link

Running this command stopped the Mac service that uses the port for me.
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -deactivate -stop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants