0

I want to create a devcontainer.json file that does the following:

  1. Installs .NET SDK, Nodejs, Git and Git LFS.
  2. Installs some custom extensions to VS code.
  3. Runs some commands on the terminal when the container is created to add user secrets to a .NET project and to create a .env.development file for a react project.

So far, I can complete 1 and 2 with no issues, however, I cannot figure out a way to do number 3. I tried using the "postCreateCommand" and even the "postStartCommand" but it doesn't work. When I open the codespace, I can see the user-secrets for the .NET project are still empty and the .env.development doesn't exist.

I cannot figure out why this is happening. I can run each command if the json file manually in the terminal and they work fine. I also thought that perhaps the commands where being run at a different directory but if the starting directory is /workspace/<repo_name> then I don't see that as a problem. I'm also not sure if the problem is because of racing conditions (e.g., using the dotnet cli before it's available) and the logs I get from GitHub are very limited.

Is it possibleto run commands that will add user-secrets through the dotnet cli and create a text file?

This is my current devcontainer.json configuration:

{
  // Name for the dev container displayed in the UI
  "name": "MyBlog Default Dev Container",
  // Base image meant to serve as a starting point for creating development container images for various programming languages and tools
  "image": "mcr.microsoft.com/devcontainers/base:ubuntu",
  // Features to add to the dev container. More info: https://containers.dev/features.
  "features": {
    "ghcr.io/devcontainers/features/dotnet:1": {
      "version": "latest"
    },
    "ghcr.io/devcontainers/features/node:1": {
      "version": "latest"
    },
    "ghcr.io/devcontainers/features/git:1": {
      "version": "latest",
      "ppa": "false"
    },
    "ghcr.io/devcontainers/features/git-lfs:1": {
      "version": "latest"
    }
  },
  // Configure tool-specific properties.
  "customizations": {
    // Configure properties specific to VS Code.
    "vscode": {
      // Add the IDs of extensions you want installed when the container is created.
      "extensions": [
        "ms-dotnettools.csharp",
        "yzhang.markdown-all-in-one",
        "sibiraj-s.vscode-scss-formatter",
        "dbaeumer.vscode-eslint",
        "dsznajder.es7-react-js-snippets",
        "adrianwilczynski.user-secrets"
      ]
    }
  },
  // Use 'postStartCommand' to run commands after the container is started.
  "postStartCommand": [
    "export ASPNETCORE_ENVIRONMENT=Development",
    "dotnet user-secrets set ConnectionStrings:MYBLOG_SQLITE ${CONNSTR_MYBLOG_SQLITE_LOCAL} --project ./src/MyBlog.WebApi/MyBlog.WebApi.csproj",
    "dotnet user-secrets set AzureAd:Instance https://login.microsoftonline.com/ --project ./src/MyBlog.WebApi/MyBlog.WebApi.csproj",
    "dotnet user-secrets set AzureAd:ClientId ${AZURE_AD_CLIENT_ID} --project ./src/MyBlog.WebApi/MyBlog.WebApi.csproj",
    "dotnet user-secrets set AzureAd:TenantId ${AZURE_AD_TENANT_ID} --project ./src/MyBlog.WebApi/MyBlog.WebApi.csproj",
    "echo REACT_APP_SERVER_URL=http://localhost:5000 > ./src/react-myblog/.env.development",
    "echo REACT_APP_REDIRECT_URL=http://localhost:3000 >> ./src/react-myblog/.env.development",
    "echo REACT_APP_CLIENT_ID=${AZURE_AD_CLIENT_ID} >> ./src/react-myblog/.env.development",
    "echo REACT_APP_TENANT_ID=${AZURE_AD_TENANT_ID} >> ./src/react-myblog/.env.development"
  ]
  // This ensures that the commands you run within the container execute with the correct permissions, and the file ownership and permissions align with the "codespace" user
  //"remoteUser": "codespace",
  //"containerUser": "codespace",
  // Specify a list of ports that should be forwarded from the Dev Container to the local machine
  //"forwardPorts": [3000, 5000,5001]
}
1
  • Do you have a solution or csproj file in the root of the repo? dotnet user-secrets does things on that level and will return errors if it can't load the context. I do the same thing but with a postCreateCommand that runs a script file: postCreateCommand": "pwsh -File ./.devcontainer/postCreateCommand.ps1 Don't forget to run the dotnet user-secrets init command first! Commented May 29, 2024 at 22:10

1 Answer 1

0

Adding as an anwser because I think these two items are part of the solution:

  1. Do you have a solution or csproj file in the root of the repo? dotnet user-secrets does things on that level and will return errors if it can't load the context.
  2. Don't forget to run the dotnet user-secrets init command first!

I do the same thing but with a postCreateCommand that runs a script file: postCreateCommand": "pwsh -File ./.devcontainer/postCreateCommand.ps1

postCreateCommand:

Write-Host "Runing dotnet build to restore all packages"
cd ./src

Write-Host "Init users secrets"
dotnet user-secrets init

# Setup the users secrets with the info from the codespace secrets
dotnet user-secrets set "GitHub:appId" $env:GH_APPID
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.