microsoft / vscode-docker

Docker Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker
Other
1.18k stars 508 forks source link

Failed to connect error #4273

Closed ricber closed 3 months ago

ricber commented 3 months ago

The docker extension shows the error "Failed to connect" and when I launch the command "docker ps" from the terminal I get "Failed to initialize: unable to resolve docker endpoint: parse "tcp://ssh username@myServerIP_A": net/url: invalid userinfo". But I get the same error even when I am connected to ServerIP_B! Instead, if I only run the command "docker" it runs fine giving me the usage suggestions.

Does this occur consistently? Yes Repro steps:

  1. Connect to host with IP ServerIP_A
  2. Run the command "docker ps" or look at the docker extension tab

Version: 1.29.0 OS: linux OS Release: 5.4.0-165-generic Product: Visual Studio Code Product Version: 1.87.2 Language: en

bwateratmsft commented 3 months ago

If you're unable to run Docker commands like docker ps in the terminal then there's no way the explorer view in the extension can work. I'd suggest looking at the documentation for Docker contexts and try to set up an SSH context.

ricber commented 3 months ago

@bwateratmsft I can actually run the “docker ps” command if I connect via SSH through a standard terminal outside VSCode, it only happens with VSCode terminals. Can Docker contexts solve the problem?

bwateratmsft commented 3 months ago

You might consider using the Remote - SSH extension to open VSCode as a remote session into your SSH target. When in that mode, the Docker extension essentially works the same as if it were running locally. More info here: https://code.visualstudio.com/docs/remote/ssh

Docker contexts can definitely be used to connect to an SSH host though.

ricber commented 3 months ago

@bwateratmsft I’m already using the Remote-SSH extension. The connection works fine like every other command. I’m just having problem with docker commands.

bwateratmsft commented 3 months ago

Do you have anything set for the docker.environment setting in VSCode? image

ricber commented 3 months ago

@bwateratmsft I don't have any docker.environment setting in my settings.json file

bwateratmsft commented 3 months ago

Are you using a DOCKER_HOST environment variable to point Docker to the SSH target? Based on the error message it looks like it's having trouble parsing the value of DOCKER_HOST or something similar.

ricber commented 3 months ago

@bwateratmsft yes, the command echo $DOCKER_HOST returns ssh username@myServerIP_A . What should I do?

bwateratmsft commented 3 months ago

Is there a space in your username? I think if you set DOCKER_HOST to ssh://username@myServerIP_A it should work. Notice the prefaced ssh://.

Also, make sure that the environment variable is present in VSCode as well--there are many ways to launch VSCode that do not inherit environment variables from other places.

ricber commented 3 months ago

@bwateratmsft it seems that the DOCKER_HOST variable has unpredictable behavior each time I launch VSCode, sometimes it contains the value ssh username@myServerIP_A and docker ps doesn't work, other times it's empty and docker ps works fine. However even when docker ps works fine, the docker extension shows the same error message. I found this issue https://github.com/microsoft/vscode-docker/issues/3739 and followed the suggestion to roll back the vscode-docker extension to version 1.22.2 and it's now working without errors! Something I have in common with the user who opened that issue is that I've installed Docker Desktop on my Windows 10 laptop at some point. However, I've now uninstalled it.

ricber commented 3 months ago

@bwateratmsft I've solved the docker extension problem rolling back to a previous version but still when I open VSCode for the first time the DOCKER_HOST variable is set to that strange value and I need to set it to an empty value to make the docker commands work in the VSCode terminal. Why is the DOCKER_HOST variable automatically set to that value? How do I disable this behavior?

ricber commented 2 months ago

@bwateratmsft is there any update on this?

bwateratmsft commented 2 months ago

It's hard to say what could be causing your environment to be set to unexpected values. It could be in your system/user environment variable settings in Windows. It could be in the way you're launching VSCode (e.g. from the start menu/desktop, or from the command line, could all result in differing behavior). There are also VSCode settings for controlling the environment, both the docker.environment VSCode setting (which can also be overridden in specific workspaces), as well as more general VSCode settings for controlling arbitrary environment variables. There could also be other extensions interfering. Ultimately all you can do is investigate all of these until you find the source of the environment variables. There are probably other possible sources that I've missed as well.

ricber commented 2 months ago

@bwateratmsft Thank you for the suggestions. I checked the Windows user and system environment variables, but there's nothing related to Docker. I tried to launch VSCode from the desktop, menu, and command line, but nothing changed either. Also, I don't have any settings.json file inside any of the workspaces .vscode directory that overrides the docker.environment setting. How can I check the value of that setting? Also, it is strange that the DOCKER_HOST variable is always set to ssh myusername@serverIP_A even when I connect to serverIP_B. Do you have any guesses as to why that is?

bwateratmsft commented 2 months ago

When you say you "connect to serverIP_B" is that via SSH remoting, i.e. https://code.visualstudio.com/docs/remote/ssh ?

ricber commented 2 months ago

@bwateratmsft yes, I mean exactly that!

bwateratmsft commented 2 months ago

You can check settings specific to remote machines in the Settings menu. Here I am connected to a Remote WSL connection but SSH would be similar. image

ricber commented 2 months ago

@bwateratmsft I finally found it! Thank you very much! The docker.environment setting was set in my User settings.json file on my Windows PC under this path C:\\User\myusername\AppData\Roaming\Code\User\settings.json.