Use the VS Code Docker Extension with a remote Docker host

Use the VS Code Docker Extension with a remote Docker host

9. November 2017

Use the VS Code Docker Extension with a remote Docker host

If you are a NAV developer working with Docker containers and the new dev environment aka VS Code (or for that matter anyone using Docker containers and VS Code), then you should very much look into the Docker extension for VS Code. It lets you see the registries you are using, the containers running on your host and the pulled or created images. You can also interact with those like looking at the logs, starting and stopping containers, opening a shell or tagging and pushing an image.

You can get it by pressing F1 in VS Code and typing ext install vscode-docker or go to the extension tab and search for Docker. The very first result should be named only “Docker” and be published by Microsoft.

Now this works right out of the box if you are working with VS Code on your Docker host, being it your laptop or a Windows Server somewhere. But what if you want to work on your development machine and the Docker host with the containers is a separate somewhere else? The extension documentation states “You can connect to a docker-machine instance if you launch Visual Studio Code and have the DOCKER_HOST environment variable set to a valid host.” and while I won’t go into docker-machine here, this also helps you to connect to your Docker host. You just have to do the following three things:

  1. Install Docker locally on your development machine even if you won’t run containers there as we need the binaries locally
  2. Expose your Docker API endpoint as I described here in chapter Networking, list item 11
  3. Open a command line, set the environment variable DOCKER_HOST to your Docker host name and port like e.g. SET DOCKER_HOST=tcp://s00-nsys-cont1:2375 (of course you need to replace s00-nsys-cont1:2375 with your host and port) and then start VS Code by just entering code

If you want to do this every time you start VS Code, it would probably be easier to create a new shortcut or edit the existing one like this
C:\Windows\System32\cmd.exe /c "SET DOCKER_HOST=tcp://s00-nsys-cont1:2375 && START /D ^"C:\Program Files\Microsoft VS Code^" Code.exe"2.

With that setup the Docker extension will work against your Docker host and allow you to manipulate the containers and images there without needing to log into the machine through RDP or remote PS, which makes it very convenient to work even in a distributed environment. The same “trick” works for the docker cli, so that you can run docker commands in a cmd against the remote host as well but at least for me for the cli opening a remote PS session works just as well. But the extension really makes it very convenient for the most common usages.

  1. be aware that this is potentially unsafe as it, well, exposes your API endpoint…
  2. you might want to change the icon to the one in C:\Program Files\Microsoft VS Code\Code.exe as it otherwise will show as the black cmd icon

Leave a Reply