Open Containers in Remote SSH Hosts using VSCode - docker

Using the Remote Development extension pack, Visual Studio Code is able to open a Docker container running on the current system, and to open a folder on a remote host connected using SSH.
If there is a Docker container running on a remote SSH host, how can VSCode open a file/folder in this container?

So you want to open a folder inside a container in a remote SSH server. Following are the steps through which I achieved it.
Step 1
install Remote Development extension in VSCode and open its control panel
Step 2
Connect VSCode to remote SSH server
Step 3
Upon connecting you'll notice a green tick sign, means you are connected to remote server, now select Containers from Remote Explorer dropdown. You'll see a list of containers. If you don't see the list of container or if you see list of local containers then follow along, otherwise move to Step 4.
Step 3.1
Add new context to your docker by executing following commands in terminal.
$ docker context create my-remote-docker-machine --docker "host=ssh://username#host:port"
$ docker context ls
$ docker context use my-remote-docker-machine
Step 3.2
$ docker ps -a
You'll notice that above command lists all the containers from remote server now (its because of the context which you just added)
Lets come back to VScode now, click on Docker from side bar menu, You'll notice a warning Failed to connect. Is Docker Running? know more about this bug
Troubleshooting
Verify that Docker extension is installed on the remote machine. As of
February 2020 there is a bug in VS Code that prevents the Docker
extension to be installed remotely if it is already installed locally.
This bug is scheduled to be fixed in VS Code 1.43 release. A
workaround to get the extension installed remotely is described here.
Uninstall Docker extension from VS Code. Close all VS Code windows.
Start VS Code again. Use `Remote-SSH: Connect to Host..' to connect to remote machine.
Open extension marketplace and install Docker extension on the remote machine (see screenshot below)
Once the Docker extension is installed remotely, it can also be installed locally.
Step 4
From the containers list select the target container which you want your VSCode to connect to.
Step 5
Vala! VSCode is connected to the container inside remote server, you can click on Explorer and select any folder/directory inside container and start working in that directory.

Related

Remote-Containers Extension of VS Code not able to execute docker commands

Setup:
Ubuntu 20.04 distro running inside WSL2 (5.4.72-microsoft-standard-WSL2)
VS Code version 1.55.2 installed in Windows
Docker version 19.03.8, build afacb8b7f0 installed in WSL2
docker-compose version 1.29.0, build 07737305 installed in WSL2
Remote-Containers Extension for VS Code v0.166.1 installed locally (I could not find an option to explicitly install this inside WSL2, but I am not sure if this even matters, see below)
Original error:
When I open the command palette and enter Remote-Containers: Rebuild and Reopen in Container, I get the error `The 'docker' command was not found. Make sure Docker is installed' in a pop up window.
What I tried:
Attempt 1
First, I thought that the Remote-Containers extension wants to execute the docker commands inside windows and not WSL2. Since I don't have Docker Desktop installed on Windows, it is impossible to execute docker commands in Windows. So I tried downloading a specific version of the the Remote-Containers extension file from the marketplace v0.165.0 I believe, copying it into the VS extension folder inside WSL2 /home/kevinsuedmersen/.vscode-server/extensions and installing it manually from the VS Code Extension panel. However, this also installed the Remote-Containers extension locally, so probably on Windows. I am not sure if locally means that this extension is available globally, but nevertheless, I am still getting the error that the docker command is not found as explained above.
Attempt 2
I notice that when I open a PowerShell on Windows, I can use the docker daemon inside WSL2 by prepending wsl to all docker commands, e.g. wsl docker run hello-world. So, I went to the VS Code and in settings (Settings > Remote > Containers > Docker Path), I changed the docker path from docker to wsl docker, but then, I got this error Docker Desktop WSL 2 backend required. This is even the case on another laptop, where I actually have Docker Desktop installed and the WSL2 backend enabled.
Attempt 3
I tried changing the docker path in VS code to the full path of the docker socket in WSL2 /var/run/docker.sock, but then I got the error Current user does not have permission to run 'docker'. Try adding the user to the 'docker' group. My current user kevinsuedmersen is already member of the docker group.
Any advice or directions are greatly appreciated.
Answer was given on the GitHub Repo of the VS-Remote-Container Extension here: https://github.com/microsoft/vscode-remote-release/issues/4914

How do I uninstall Docker or remove the Docker directory?

I am trying to learn Docker and have installed it on my laptop. I have tried to uninstall it and delete the Docker directory as Administrator, but keep getting the error message "The action cannot be completed because the file is open in Docker Desktop service". But Docker Desktop Service is not open, and does not appear whenever I open Task Manager. When I try to reinstall Docker, it says it it up to date, when clearly it is not. I would appreciate any help getting a fresh installation of Docker.
Here is the Docker directory C:\Program Files\Docker\Docker.
Here is the error message.
If you have access to the windows machine, through UI, you can:
Go to Start -> Services, and search for the Docker (probably is called Docker Desktop or something similar) service and
Manually Stop. You can also set the service to start Manually, not Automatically, give a restart and
Now Docker is completely stopped and
You can uninstall safely.
User azbarcea answered the question.

VS Code "Attach Visual Studio Code" to remote container error

I am trying to develop in a remote container.
I run VS Code on my local windows machine.
I have a linux machine which runs docker and a bunch of containers.
I have the "Remote - Containers" and "Remote - SSH" extensions installed in VS Code.
I can connect to my linux machine in VS Code and I can see the running containers.
I can right click on a container and choose "Attach Shell". This works fine:
When I right click on a container and choose "Attach Visual Studio Code" I get an error:
UPDATE
The above error was raised because (for some reason?) docker must be running locally on windows also even though we are fully on a remote machine. I've installed and run docker locally.
Now when I right click on a running container, I get a different error:
Of course the containers are running -- I see them.
How can I Attach Visual Studio Code to a running remote container successfully?
This may not be a real answer but it's too much for a comment.
I believe you have a local machine and docker on a remote server.
The first thing you have to do is to install docker on your local machine and configure it so that's its looking for the docker host on your remote server.
Then you can create a .devcontainer.json on your machine. If you have the extension installed, VSCode will offer you do open this as container environment. Since your docker host sits on remote, this will now happen on your server instead of your local machine.
When I did the setup, I followed amongst other things this guide. Especially the SSH-Agent was required to get a remote docker host working. https://code.visualstudio.com/docs/remote/containers-advanced#_a-basic-remote-example
Here is a example .devcontainer file of mine.
Now back to your initial question, I don't think you will be able to use the remote container extension on a container that wasn't started as dev container. This is because vscode will install a bunch of stuff in there when its first set up. Similar to the SSH Extension. I may be wrong on this so take it with a grain of salt.
It may also be worth noting that once you connect to your server via SSH and have then the regular docker extension (which is not the remote container extension) installed, on remote, you will see your docker images listed there. But that does not mean you will be able to connect like that from local to remote container. For that you need to configure a docker remote host.
I have also faced similar issue after doing some research I found the issue was with my installation.
But I faced this issue when I installed vs-code through snap in Ubuntu.
May be try uninstalling VS Code and reinstalling it.
It should work if Docker is installed properly.

Remote Docker Visual Studio Code - No running containers - Mac OS

I am trying to Attach VS Code Window into a Docker container inside my remote machine on Azure.
It has stopped working at some point, the message is "There are no running container to attach to", but clearly there is one because it finds it in the Docker extensions.
These are my steps.
Open VS Code
SSH-Remote Development to open VS Code on the Remote machine and install Docker extension there
I find the docker container in the remote machine, but it fails opening it.
I attach here a screenshot.
I have tried this issue of possibly being because of opening VS Code from Spotlight instead of the Terminal, but is not the case.
Thanks !
I had the same issue, but opening a folder first, on the VSCode instance on the remote machine, seems to solve the problem for me.
File > Open Folder > "chose a folder"

Linux Docker container on Windows Server 2016

What is the way to run Linux docker container on Windows Server 2016? With Docker for Windows it easy to do in Windows 10. Is the only way to install Docker Toolbox and ignore the build-in containers feature all together?
It is possible to use Linux containers on W2016. It's a bit hacky though. User "Algore" described it here but I've copied it into this answer, of course.
Download "Docker for Windows": https://docs.docker.com/docker-for-windows/
Install "Docker for Windows" on Windows 10 Pro.
Copy C:\Program Files\Docker from the Windows 10 machine to C:\Program Files on your Server 2016 box.
On the W2016 box run install-windowsfeature hyper-v,containers
Copy these registry entries to a file and import them on your Server 2016 box (reg import):
---START---
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Docker Inc.]
[HKEY_LOCAL_MACHINE\SOFTWARE\Docker Inc.\Docker]
[HKEY_LOCAL_MACHINE\SOFTWARE\Docker Inc.\Docker\1.0]
"AppPath"="\"C:\Program Files\Docker\Docker\Docker for Windows.exe\""
"BinPath"="\"C:\Program Files\Docker\Docker\resources\bin\""
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\DockerService]
"EventMessageFile"=hex(2):43,00,3a,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,\
00,73,00,5c,00,4d,00,69,00,63,00,72,00,6f,00,73,00,6f,00,66,00,74,00,2e,00,\
4e,00,45,00,54,00,5c,00,46,00,72,00,61,00,6d,00,65,00,77,00,6f,00,72,00,6b,\
00,36,00,34,00,5c,00,76,00,34,00,2e,00,30,00,2e,00,33,00,30,00,33,00,31,00,\
39,00,5c,00,45,00,76,00,65,00,6e,00,74,00,4c,00,6f,00,67,00,4d,00,65,00,73,\
00,73,00,61,00,67,00,65,00,73,00,2e,00,64,00,6c,00,6c,00,00,00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\com.docker.service]
"Type"=dword:00000010
"Start"=dword:00000002
"ErrorControl"=dword:00000000
"ImagePath"=hex(2):22,00,43,00,3a,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,\
6d,00,20,00,46,00,69,00,6c,00,65,00,73,00,5c,00,44,00,6f,00,63,00,6b,00,65,\
00,72,00,5c,00,44,00,6f,00,63,00,6b,00,65,00,72,00,5c,00,63,00,6f,00,6d,00,\
2e,00,64,00,6f,00,63,00,6b,00,65,00,72,00,2e,00,73,00,65,00,72,00,76,00,69,\
00,63,00,65,00,22,00,00,00
"DisplayName"="Docker for Windows Service"
"ObjectName"="LocalSystem"
"Description"="Run Docker for Windows backend service"
---END---
You can add an entry to currentversion\run or a scheduled job if you want docker for windows.exe to start up automatically.
Restart the computer and run the PowerShell script C:\Program Files\Docker\Docker\Resources\mobylinuxvm.ps1 -Create
Look into this ps1 file and notice that you can set various options. Please refer to these websites:
https://docker-saigon.github.io/post/Docker-Beta/
https://jpetazzo.github.io/2013/10/16/configure-docker-bridge-network/
Make sure the docker service com.docker.service (start-service com.docker.service) is running before you start C:\Program Files\Docker\Docker for Windows.exe
If you are running on server core and don't have the tray icon, click "change privacy settings" when the notification comes up after it starts to get to settings (or modify the json in programdata)
If you run into issues when trying things and rebuilding, check out the C:\Program Files\Docker\Docker\Resources\forceremovedocker.ps1 file for locations to delete settings such as in C:\programdata. I would advise against running the script as it deletes all docker related things. Just use pieces of it.
According to this from Stefan Scherer it is possible to run Linux containers on Server 2016. The instructions are:
Create a Linux Docker machine in Hyper-V
If you want to try out multi architecture you also use docker-machine
to create a Linux Docker Engine running in Hyper-V. I have prepared a
script that will set up everything as there are some known issues.
C:\vagrant\scripts\create-hyperv-linux-docker-machine.ps1
This PowerShell script creates a Docker machine and updates Docker
Engine to the latest so that the Windows Docker client is able to
communicate with the Linux Docker Engine.
Use the Linux Docker machine
Open a PowerShell terminal as an administrator and select the Linux
Docker machine with
docker-machine env --shell powershell | iex
Now run your first busybox container with
docker run -it busybox uname -a
Unfortunately, right now it is not possible to easily install the "Docker for Windows" tool. This installer provides a thin VM called MobyLinux which allows running of Linux containers on Windows Server 2016. You can find a hacky install attempt (which I have not tried) on the Docker forums.
For reference there is a good discussion about what is going on behind the scenes with "Docker for Windows".
The below worked on Windows Server 2016 build 14393.693 (January 2017) without hacky workarounds.
It may work on higher/lower builds but I tried to replicate on a recently updated version of Windows Server 2016 and it failed spectacularly! Feels like this hole had to be patched!
Download Docker Community Edition 2.0.0.3 2019-02-15
Link: https://download.docker.com/win/stable/31259/Docker%20for%20Windows%20Installer.exe
Page with all versions: https://docs.docker.com/docker-for-windows/release-notes/
Simply download/copy the file to the Windows Server 2016 machine and run it.
It can also run offline for air-gapped systems.
Uncheck the "Use Windows containers" during setup. Unchecked by default.
You will get a message after installing saying that windows so and so is deprecated. Click OK. enter image description here
Log out and log back in.
If you hadn't installed Hyper-V it'll prompt you to do it automatically. Allow it, let it finish and restart. You're good to go now. You may need to restart again if it fails to switch to Linux.
I've tested this by running several Linux containers, it works fine. If I'm missing something do share for the benefit of others. The downside is you cannot update to a later version of Docker.
Simply download and install Docker on Windows Server 2016 https://www.docker.com/docker-windows
If you run Windows Server 2016 as a VM using Hyper-V make sure to check this MS doc https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/nested-virtualization

Resources