When doing machine learning and data analysis, it is rare that the analysis / development work is completed only with the machine at hand. I think that it is being developed using Docker etc. on a remote server with GPU Morimori.
Starting from VS Code, I made a trial and error about Python development in a remote environment, so it is a summary.
I'm a beginner just starting to use VS Code, so I would appreciate any advice.
It is a working environment. I don't think I'm bound by this environment.
--local machine: Macbook Pro - VSCode: v1.48.2 --Remote OS: Ubuntu 20.04 LTS
Since it is an integrated development environment, I want to start VS Code on a local machine and complete the work with only local VS Code.
--Ssh connection from VS Code to remote server --Connect to Docker container on the destination server --Debugging while visually setting breakpoints, etc.
Remote Development Use the extension. This extension seems to be a collection of remote development extensions for SSH, Container, and WSL (they look the same even if they are installed).
Reference: https://code.visualstudio.com/docs/remote/ssh#_getting-started
Remote Development:https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
Open the command palette and select Remote-SSH: Connect to Host
to bring up a selection list of connections.
If you select ʻAdd New SSH Host from here, you will be prompted to enter the shsh command, so enter the same command as for ssh connection from the terminal. When connecting for the first time, I remember that there was only an item of ʻAdd New SSH Host
.
For key authentication, specify the key with the -i
option when you type the ssh command.
These settings are written in ~ / .ssh / config
by default.
You can also write port forwarding settings, key file settings, etc. directly. (Maybe adding a host)
Select the remote server from Remote-SSH: Connect to Host
on the command palette, just as you did when you added the host. Alternatively, you can access it by selecting the host from "Remote Explorer" in the shortcut menu on the left side of VS Code.
After connecting to the remote server, it's time to connect to the Docker container.
I referred to the following article (I just traced it as it is for reference, so please check the reference source). Operate Docker container on server with SSH from VS Code: https://qiita.com/_kani/items/1ce36e36b0bcda403be2
Launch the VS Code terminal and launch the container with docker commands. The RemoteDevelopment Extension also has a function that supports from launching the docker container to connecting, but this seems to be a docker operation on the local machine. So, when setting up a container on a remote server, use the normal docker command (I just couldn't do it?).
Set up port forwarding. (I don't really understand how this works ...)
Open the ssh config. If you open the command palette and enter ssh config
etc., items will appear, so select it.
Add the LocalForward
setting to the relevant remote host settings.
Host 192.168.*******
HostName 192.168.*******
IdentityFile ~/.ssh/id_rsa
User *****
LocalForward 23750 /var/run/docker.sock
Connect to a remote host from VS Code. It is also necessary to install the Remote Development extension on the remote host.
From Preferrence, set the parameters as follows. Select Remote on the tab and search for Docker.host and it will come up.
tcp://localhost:23750
You are now ready.
The setting screen will appear when you enter preferences Open User Setting
etc. from the command palette.
Operate from Remote Explorer in the sidebar. When you select Containers from the pull-down menu at the top, a list of containers will appear, so select the container you want to connect to. Connect to the container with "Attach to Container" (a new window will be launched).
Since VSCod is an integrated development environment, you can debug by setting breakpoints interactively. However, in a specific environment, it has been confirmed that breakpoints cannot be set and steps cannot be executed, and the program runs to the end.
Reference: Problem that does not stop at breakpoint when debugging Python virtual environment with VS Code)
This is a problem, but it seems that the debugger function is enabled by setting parameters in the debugger configuration file'launch.json'.
'launch.json' is displayed on the editor screen by opening the debugger from the side menu of VS Code and pressing the gear mark at the top. Add parameters to this json file.
"stopOnEntry":true
You need to be careful about the order in which you put stopOnEntry
.
When I put it before ʻargs, I got stuck for a while when I encountered the phenomenon that the ʻargs
parameter was not applied and the argument was not passed to the debugger.
After some trial and error, I have now settled on the above procedure. If you know a better way, please let me know.
I'm particularly worried that I don't really understand how to operate Docker containers.
Recommended Posts