如何连接运行在远程Docker容器中的Jupyter Notebook?
Let's break down why you're hitting that "Connection refused" error and walk through the fixes step by step:
The Core Problem
When you start Jupyter inside the container with just --port=8884, it defaults to binding only to the container's local loopback (127.0.0.1). Even though you've mapped the remote machine's port 8884 to the container's 8884, the remote machine's 127.0.0.1 is a separate network namespace from the container's. That means your SSH tunnel can't reach the Jupyter instance running inside the container.
Step-by-Step Solutions
1. Update Jupyter's Listening Address in the Container
Modify your Jupyter startup command to bind to all network interfaces in the container using --ip=0.0.0.0. This lets the Docker port mapping connect to the Jupyter instance properly:
jupyter-notebook --NotebookApp.iopub_data_rate_limit=10000000000 --no-browser --port=8884 --allow-root --ip=0.0.0.0
2. Verify Port Mapping and Listening States
On the remote machine, check if Docker is correctly forwarding port 8884:
netstat -tulpn | grep 8884You should see a line like
0.0.0.0:8884linked to thedocker-proxyprocess—this confirms the remote machine is listening for connections on that port.Inside the container, confirm Jupyter is listening on all interfaces:
- Grab your container ID first:
docker ps - Enter the container:
docker exec -it <container-id> bash - Run:
netstat -tulpn | grep 8884
You should see0.0.0.0:8884associated with the Jupyter Python process.
- Grab your container ID first:
3. Ensure Your SSH Tunnel is Active
Your SSH LocalForward only works while the SSH session is running. If you want to keep it alive in the background, add -fN when connecting:
ssh -fN p3
4. Test the Connection
Once all the above is set up, open your local browser and navigate to http://localhost:8883. You'll need to enter the Jupyter token (printed in the container's terminal when you start the notebook) or a pre-configured password to access it.
Troubleshooting If It Still Fails
- Check for port conflicts: If another process on the remote machine is using port 8884, switch to a different port. For example:
- Update your SSH config to
LocalForward 8883 127.0.0.1:8885 - Restart the container with
nvidia-docker run -it -p 8885:8885 <docker image> - Start Jupyter inside the container with
--port=8885
- Update your SSH config to
- Double-check firewall settings: Ensure the remote machine's firewall isn't blocking incoming connections on port 8884 (though since you can access non-containerized Jupyter, this is less likely to be the issue).
内容的提问来源于stack exchange,提问作者avitheavi

