I’m trying to build the Matter (https://github.com/project-chip/connectedhomeip) project from VS Code using devcontainer on a Windows 11 PC (with WSL and Docker Desktop installed).
The problem is that I’m getting this error when trying to "Reopen in Container" for this project from VS Code:
Running the initializeCommand from devcontainer.json...
[10282 ms] Start: Run: C:Windowssystem32cmd.exe /c .devcontainer/build.sh --tag matter-dev-environment:local --version 22
'.devcontainer' is not recognized as an internal or external command,
operable program or batch file.
It looks like the problem might be that it’s using cmd.exe and not the bash shell to execute the command.
How can I get the devcontainer.json to launch the commands in a bash shell, rather than using cmd.exe?
2
Answers
The official documentation for building with VSCode describes how to set the shell used for
initializerCommand
, but it seems outdated:The
terminal.integrated.shell.windows
setting is no longer valid. I tried using the alternatives with no success:and
For both,
initializerCommand
still usescmd
. The only working solution I came up with is to edit.devcontainerdevcontainer.json
manually by addingbash
to the command:"initializeCommand": "bash .devcontainer/build.sh --tag matter-dev-environment:local --version 22"
Although this is not the best way, because you have to change the upstream configuration, it seems to be the only solution for now. Remember to follow the steps in the official documentation,
bash
is only available for Windows when installing Ubuntu for WSL, and you have to clone the repository using LF instead of CLRF.To execute a bash command in a VS Code devcontainer, you can use the postCreateCommand configuration option. This option allows you to specify a command that is executed after the container is created.
Here’s an example of how you can use it in your .devcontainer/devcontainer.json file.
{
"name": "Your Dev Container",
"dockerFile": "Dockerfile",
"postCreateCommand": "/bin/bash -c ‘echo Hello from the postCreateCommand’",
// … other configurations
}
In this example, the postCreateCommand is set to run a simple bash command that echoes "Hello from the postCreateCommand". You can replace this with any other bash command you need.
Make sure to adjust the command based on your specific requirements. If your command is more complex, you might want to create a separate script and execute that script as the post-create command.
After making changes to the devcontainer configuration, reopen the project in VS Code, and it should execute the specified bash command after creating the container.