Developing with Visual Studio Code + devcontainer with VSCode on Windows Host and Docker on Linux

Hey guys. I am trying to set up a devcontainer for VSCode using these instructions:

As with most things in my life, I have an unusual set up. My development machine where I run VSCode is Windows 11 and my Docker host is a separate Linux machine. The instructions are for folks who are doing both on the same machine best I can gather. Is there an easy way for me to use the Linux machine for the devcontainer? I really don’t want to install WSL on this Windows machine.

Apologies if this is documented but my attempts from my searches have not proven fruitful. Thanks!

I managed to get a ways into this. I am connecting to my Linux machine via SSH when clicking on the link to my repo as provided in the docs. Things are building but I get a fail message that the port is already in use and I should edit the json file to change the port. I am assuming this is due to the HA container I am running on the same host is using 8123. How do I change that port?

On closer inspection, this was installing via an old instance of WSL I had set up on the Windows machine. I just removed WSL and it is opening but is giving me this error:

image

I’m not sure what was happening before as I was definitely prompted to SSH into my server but on closer inspection of the install logs, it was listing paths on the Windows machine so I guess it was using WSL. Sigh. I did try this before with WSL and it failed. I really prefer to do this on the Linux machine rather than Windows.

I haven’t tried it, but there’s some “remote” extensions that you may need to make it easier to work with. There’s one for remote ssh which sounds like you may have that, but there’s also Remote Development, Remote Explorer, Remote - Tunnels, and Dev Containers. I am fairly certain that with those, I saw somewhere that allowed me to configure a remote container for development. There should also be info you could find online about your type of setup.

I can definitely SSH into my Linux machine using VSCode but I think the problem may be with the link from the docs. It opens a new window that is not connected via SSH and I am guessing that it would need to be to run the Docker install on the connected Linux machine.

Can I open that link manually from within the connected VSCode window?

The link looks like this:
vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https%3A%2F%2Fgithub.com%2Fdinki%2Fhome-assistant

Ah, yes - it tries to build everything locally. If you’ve got a remote docker setup and want to use a remote container, I think you’ll have to do it yourself. I haven’t tried this so I couldn’t help you more.

There’s a good chance I’m making this more complicated than it needs to be. Am I just creating another instance of HA in another container? After, that I can then connect to it via VSCode?

What I am really trying to do is make things easier for me while I learn to write an HA extension. Right now I am making changes, restarting HA, finding problems, edits, restart, repeat repeat. What I would like to do is to have an environment where I can test python code quicker without the restarts. I did connect via Jupyter Notebook and did some things there but that seems to be using the database itself. I could not understand how to use it to test the service call I am working on at the moment.

Perhaps someone can tell me a better way to approach this if there is one.

Yeah, basically - I just went down the same road. All it does is fire up a container given your git repo and runs that as an instance of HA. As you develop, you can test using that instance, etc. I don’t believe there’s anything overly special about it, but I could be wrong.
This is the dev container config I have using HA’s recommended method, so just copy the pieces you think you need if building remotely yourself:

{
  "name": "Home Assistant Dev",
  "context": "..",
  "dockerFile": "../Dockerfile.dev",
  "postCreateCommand": "git config --global --add safe.directory ${containerWorkspaceFolder} && script/setup",
  "postStartCommand": "script/bootstrap",
  "containerEnv": {
    "PYTHONASYNCIODEBUG": "1"
  },
  "features": {
    "ghcr.io/devcontainers/features/github-cli:1": {}
  },
  // Port 5683 udp is used by Shelly integration
  "appPort": ["8123:8123", "5683:5683/udp"],
  "runArgs": [
    "-e",
    "GIT_EDITOR=code --wait",
    "--security-opt",
    "label=disable"
  ],
  "customizations": {
    "vscode": {
      "extensions": [
        "charliermarsh.ruff",
        "ms-python.pylint",
        "ms-python.vscode-pylance",
        "visualstudioexptteam.vscodeintellicode",
        "redhat.vscode-yaml",
        "esbenp.prettier-vscode",
        "GitHub.vscode-pull-request-github",
        "GitHub.copilot"
      ],
      // Please keep this file in sync with settings in home-assistant/.vscode/settings.default.json
      "settings": {
        "python.experiments.optOutFrom": ["pythonTestAdapter"],
        "python.defaultInterpreterPath": "/home/vscode/.local/ha-venv/bin/python",
        "python.pythonPath": "/home/vscode/.local/ha-venv/bin/python",
        "python.terminal.activateEnvInCurrentTerminal": true,
        "python.testing.pytestArgs": ["--no-cov"],
        "pylint.importStrategy": "fromEnvironment",
        "editor.formatOnPaste": false,
        "editor.formatOnSave": true,
        "editor.formatOnType": true,
        "files.trimTrailingWhitespace": true,
        "terminal.integrated.profiles.linux": {
          "zsh": {
            "path": "/usr/bin/zsh"
          }
        },
        "terminal.integrated.defaultProfile.linux": "zsh",
        "yaml.customTags": [
          "!input scalar",
          "!secret scalar",
          "!include_dir_named scalar",
          "!include_dir_list scalar",
          "!include_dir_merge_list scalar",
          "!include_dir_merge_named scalar"
        ],
        "[python]": {
          "editor.defaultFormatter": "charliermarsh.ruff"
        },
        "json.schemas": [
          {
            "fileMatch": ["homeassistant/components/*/manifest.json"],
            "url": "${containerWorkspaceFolder}/script/json_schemas/manifest_schema.json"
          }
        ]
      }
    }
  }
}

Thanks for sharing. I’m starting to think that this doesn’t give me really what I was hoping for. I was probably reading into things but I was hoping for something more interactive sort of like Jupyter Notebooks where I can build and test in a ‘live’ environment. I’m thinking this isn’t it and not sure if that even exists.