How to "Attach Visual Studio Code" to container on remote EC2 instance - visual-studio-code

For the last month I can only connect VS Code to remote docker containers maybe 10% of the time I try to do it. When it works, it works great, but I cannot reliably connect when I want to.
Right now I'm not setting docker.host in VS Code settings. Things have worked without setting that, some of the time. Right now I'm consistently seeing this:
I can connect via SSH to the remote instance.
I can see the docker container is running (docker ps).
The Docker->containers list shows the container.
But "Attach Visual Studio Code" does not show the container as an option.
My question is how can I attach to the container that's definitely right there and running?
Maybe notable I have the Remote Extensions installed locally, but there is no option to install any of the extensions on the remote instance. And there is no "Attach To Running Container" option. I don't know how to get that option to come back.
Thanks for any ideas.

Related

is there a way to work offline with vscode remote ssh to save cost?

I am using vscode ssh remote plugin https://code.visualstudio.com/docs/remote/ssh
to connect to gcp sandbox vm. gcp sandbox vm has an hourly cost if kept turned on.
most of the time I am reading or changing the code and I am not running any command in the VM's terminal. So, if I had the code offline, I would not need to keep the VM running and pay for each second of it.
Is there a way I can do this?
When using VSCode over SSH, you are running a VSCode server on the remote host which is used to read the files.
If you want to be able to open files locally, you will need to download them.
If you already have the files opened, you can disconnect from the VM and keep looking at them.
Another option to consider is mounting whatever file store you're using locally as a FUSE file system. Here is an example for git: https://github.com/presslabs/gitfs

Poor terminal performance in local VS Code Dev Container

I'm exploring remote container development in VS Code and GitHub Codespaces.
For context, I'm working in Windows 11 on performant Dell XPS 9310 i7 / 32 GB RAM machine.
When I open my Codespace / Dev Container in GitHub Codespaces via the browser, or in VS Code via the > Codespaces: Open in VS Code command, there are no performance issues.
Because I want to know whether development offline is feasible, I am testing having cloned my repo on my local machine, and having opened the repository folder in VS Code locally. This causes a local Docker container to be started in Docker Desktop running on WSL 2. (As expected, Git commands need to be managed outside of VS Code as expected due to permissions, but otherwise looks OK).
A performance issue is apparent when interacting with the terminal (bash or zsh). A terminal prompt takes literal minutes to appear, and to return after every command. I am often stuck waiting for a prompt as shown below after running simple commands requiring few resources.
What is the cause of the poor performance here?

vscode chrome attach unable to connect after some time

I have a really wired problem (or a bug) on vscode. I can "attach chrome" debugging totally fine usually,
but after some random time connecting to the chrome instance (may be a couple of hours or 8 hours, like I left it connected while I'm sleeping), it automatically disconnect the debugging,
then when I try to re-run attach chrome to reconnect it, for some weird reason it doesn't connect to the instance.
It's not a failed, it's not like "cannot connect to the port 9222", it's infinitely trying to connect showing the left up side blue indicator left to right.
This is my second time to encounter this weird problem. For the first time I even don't know how I solved the issue. maybe I just waited its reconnect for an hour or like that.
The first things I happened to think from the problem is simply its port conflict. So I checked vscode side's (I mean I use vscode in the remote ssh mode from Windows to Linux Ubuntu) port and process, used htop to filter port "9222" or process "chrome", but I didn't find anything stucking there. So I assume in the linux side the debugging process is exiting successfully.
So I did the same on the Windows side, I used resource monitor to check port "9222", no running process. If I re-open a 9222 debugging enable chrome, then it appears. closing, it disappears. It seems there is no problem here as well.
I then checked vscode's version. I was using vscode 1.64.2 so I side-installed 1.65.2 and 1.66.1 (the latest) and tested it. Doesn't work.
"refreshing the window" doesn't work. closing the window and re-open, doesn't work.
So what can I do?
Did you just install some new extensions in Visual Studio Code? I found that I had this exact same behaviour, "infinitely trying to connect showing the left up side blue indicator left to right", after I installed Karma Test Explorer and Test Explorer UI extensions. Once I uninstalled those extensions, debugging started working again.

Set up Visual Studio Code with multiple containers

I have an application consisting of a Flask web application and a RQ worker application. Both live in separate containers but use the same image. I would like to use Visual Studio Code's Remote Extension to run the containers and debug inside them. This all works well for either container using devcontainer.json and the automatically generated .devcontainer/docker-compose.yml which overwrite mine partially.
This Article explains that connecting to multiple containers is possible using multiple windows. They assume the two containers to have different source, stored in different folders which can be used to store separate devcontainer.json files.
I don't understand though how to configure the startup. When starting any of the two containers I would need the other one to start as well. That's easily achieved using the original docker-compose.yml. I could then manually run and debug the application in the started devcontainer.
Yet how would I connect to the other, then also running container and run and debug there?
First, install the Docker extension.
In VS Code, you can right-click on the docker-compose.yaml File and click on compose up to start all the containers.
You debug your script in exactly one terminal shell. This is the reason why you have just one VS Code window per container.
However, you can attach VS Code afterwards to as many already started containers as you like (e.g. after docker compose up) by right-click on the container and click on Attach Visual Studio code in the Docker tab.

No "open folder in container" or any other command in remote-containers vs code

I'm using a remote machine, and want to run a docker inside that machine (local->remote->docker) and wish to use the Remote development in Containers tools of vs code
Unfortunatlly, I do not see any of the actions after installing the extension.
My view (ctrl+shift+p -> type remote-containers)
While the tutorial has actions I don't even see:
You need to make sure you are not connected to your remote host via Remote-SSH in VSCode. If you are, you will not see "Open Folder in Container..." or the other options.
Try opening up VSCode without connecting using Remote-SSH. You will find that the option is now present.
To set up a remote Docker host, you first need to be able to access your remote host using key based authentication as describe here
You then set
"docker.host":"ssh://your-remote-user#your-remote-machine-fqdn-or-ip-here"
in setting.json.
Once that is setup, you can only attach to running containers. Test this out before proceeding:
Start a container on the remote host
Hit F1 in your vscode and then select "Attach to Running Container..."
You should see the container you started and you should be able to attach to this
Once you are past this point, you will need to create your devcontainer.json file as per the documentation
Make sure you have installed Remote Container extension.
and ssh as mentioned on the comment above.
Once installed, click on Docker icon, under containers, right click on the container you need to work with vcode and select attach vscode.
I had this issue when I opened vscode for the first time in a year after only needing to install it for some random project that wanted me to use "dev containers" for, I don't remember what.
But I don't care about dev containers anymore and I don't remember that project, however, vscode was functionally unusable because it opened in that old project directory and was trying to do... something with containers. I don't know, I don't care, I just wanted to open another folder so a junior programmer could use an IDE on my machine they're familiar with instead of emacs/vim/whatever.
The solution for me was to use the "extensions" tab CTRL+SHIFT+X, disable the "Dev Containers" extension, then use "File -> Open Recent Folder" to select some other directory. This gets around the "can't open in container" bug. If you don't have a history of other opened directories, I think you need to completely reinstall Vscode or something.