anthropics / anthropic-quickstarts

A collection of projects designed to help developers quickly get started with building deployable applications using the Anthropic API
MIT License
7.02k stars 1.06k forks source link

(MacOS) Ports are not available: 5900 #147

Open p-i- opened 1 month ago

p-i- commented 1 month ago

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.

p-i- commented 1 month ago

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.

IgorGanapolsky commented 4 weeks ago

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

tms1337 commented 4 weeks ago

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

ghost commented 3 weeks ago

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