ssh port mapping - access jupyter in locally mapped target host mapped Docker container

background:

My computer is the local host, and my colleague's computer (3080TI) is the target host. There are containers in the system. I need to connect to the container in the target host, and I need a graphical interface or a folder directory. Only using vscode or SFTP client can not see the files and graphical interface in the container, so we explored the use of jupyter to access the folders in the container.

But now the VNC method has been used to access the desktop of the container, and this article is only for records.

(1 message) vnc connects to the desktop of the docker container in the remote server


Map locally to the target host

Create a new powershell, run the following code, open the terminal and map port 8888 of the local machine to port 8888 of the remote host, using the -L parameter of the ssh command to achieve.

 ssh -L 8888:127.0.0.1:8888 [email protected]

 The following is the ssh format. Among them, the local address is usually the local machine, which can be ignored directly, and the default is localhost. The mapping between two hosts needs to fill in the address of the target host, which is 127.0.0.1.

ssh -L 本地地址:本地端口:目标地址:目标端口 用户名@跳板机地址

Check container running status, enable container, enter container from any terminal.

# 查看当前所有容器:已运行、未运行的
docker container ls -a
# 若容器时up状态,则直接exec进入容器,忽略第一行。
docker start ybd
docker exec -it ybd bash

Check whether the container has enabled the ssh service. If the container is in the up state in the previous step, and the ssh state is known to be enabled, there is no need to additionally run the command to start the ssh service.

service ssh start # 启动ssh服务
ps -e | grep ssh #查看ssh服务是否启动

 Run the following code in the terminal opened in the first step (to complete the port mapping) to use ssh to connect to the container and forward the container to the target host. Since it is forwarded by the local machine (target host), and the target address is consistent with the springboard machine, the target address is directly filled with localhost.

# 下面两句含义相同,本机转发,本机地址为127.0.0.1
ssh -L 8888:127.0.0.1:8888 [email protected] -p 8080
ssh -L 8888:localhost:8888 [email protected] -p 8080

After connecting to the container, first generate the configuration file.

$jupyter notebook --generate-config

A password needs to be generated for the first connection. Refer to Remote access to jupyter notebook - Echo/ - Blog Garden (cnblogs.com) . After generating a password according to the operation, you need to open ~/.jupyter/jupyter_notebook_config.py for configuration:

vim ~/.jupyter/jupyter_notebook_config.py

 Add some content. Note to replace 'sha1:854d1...' with the key generated in the previous step.

c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha1:854d183417ac:db1e783bde7bbab704d009c996e93dc2d3e31c25'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888

After configuring the jupyter configuration file, run jupyter:

jupyter notebook --allow-root

The following results appear:

 Open a browser and enter localhost:8888 to access the docker container mapped to the target host mapped to the local host.

There is also a problem, maybe the default path of jupyter is not the path where the container is mounted to the target host, so you need to open jupyter_notebook_config.py again to modify the default path.

vim ~/.jupyter/jupyter_notebook_config.py

Press "/" to find: "c.NotebookApp.notebook_dir":

The default is:  

# c.NotebookApp.notebook_dir = ''

Modify as needed to:

c.NotebookApp.notebook_dir = '/'

The default path has been modified successfully!


 at last! Very important!

Every time you exit jupyter, you must exit according to the normal procedure, otherwise the port will remain open, and the next time the container is mapped, you will not be able to access 8888, which will be postponed to 8889, resulting in the failure to connect normally!

If it exits unexpectedly, the most efficient way is to restart the container! ! !

reference:

Docker: How to use jupyter notebook elegantly in Docker (sharp tool) - Felaim's Blog - CSDN Blog

SSH port forwarding realizes Jupyter Notebook remote connection server_ID_AF12's Blog-CSDN Blog_jupyter notebook port forwarding

Guess you like

Origin blog.csdn.net/weixin_41467446/article/details/125216873