diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index b8bd5b9..0468bd9 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -29,7 +29,7 @@ RUN apt-get update \ && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \ # # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed - && apt-get -y install git iproute2 procps apt-transport-https gnupg2 curl lsb-release \ + && apt-get -y install git openssh-client less iproute2 procps apt-transport-https gnupg2 curl lsb-release \ # # Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user. && groupadd --gid $USER_GID $USERNAME \ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 24bca70..bc90ee4 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,46 +1,50 @@ +// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/dotnetcore-3.1 { - "name": ".NET Core Sample", - "dockerFile": "Dockerfile", + "name": "C# Sample", + "build": { + "dockerfile": "Dockerfile", + "args": { + "INSTALL_NODE": "false", + "NODE_VERSION": "lts/*", + "INSTALL_AZURE_CLI": "false" + } + }, - // Use 'appPort' to create a container with published ports. If the port isn't working, be sure - // your server accepts connections from all interfaces (0.0.0.0 or '*'), not just localhost. - "appPort": [5000, 5001], - - // Use 'settings' to set *default* container specific settings.json values on container create. - // You can edit these settings after create using File > Preferences > Settings > Remote. - "settings": { + // Set *default* container specific settings.json values on container create. + "settings": { "terminal.integrated.shell.linux": "/bin/bash" }, - // Uncomment the next line to run commands after the container is created. - // "postCreateCommand": "dotnet restore", + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "ms-dotnettools.csharp" + ], - // [Optional] To reuse of your local HTTPS dev cert, first export it locally using this command: + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + "forwardPorts": [5000, 5001], + + // [Optional] To reuse of your local HTTPS dev cert, first export it locally using this command: // * Windows PowerShell: - // dotnet dev-certs https --trust; dotnet dev-certs https -ep "$env:USERPROFILE/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" + // dotnet dev-certs https --trust; dotnet dev-certs https -ep "$env:USERPROFILE/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" // * macOS/Linux terminal: // dotnet dev-certs https --trust; dotnet dev-certs https -ep "${HOME}/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" // - // Next, after running the command above, uncomment lines in the 'mounts' and 'remoteEnv' lines below, + // Next, after running the command above, uncomment lines in the 'mounts' and 'remoteEnv' lines below, // and open / rebuild the container so the settings take effect. // "mounts": [ // "source=${env:HOME}${env:USERPROFILE}/.aspnet/https,target=/home/vscode/.aspnet/https,type=bind" ], "remoteEnv": { - //Override the default HTTP endpoints - need to listen to '*' for appPort to work - "ASPNETCORE_Kestrel__Endpoints__Http__Url": "http://*:5000" - - // "ASPNETCORE_Kestrel__Endpoints__Https__Url": "https://*:5001", // "ASPNETCORE_Kestrel__Certificates__Default__Password": "SecurePwdGoesHere", - // "ASPNETCORE_Kestrel__Certificates__Default__Path": "/home/vscode/.aspnet/https/aspnetapp.pfx" + // "ASPNETCORE_Kestrel__Certificates__Default__Path": "/home/vscode/.aspnet/https/aspnetapp.pfx", }, - // Comment out the next line to run as root - "remoteUser": "vscode", + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "dotnet restore", - // Add the IDs of extensions you want installed when the container is created in the array below. - "extensions": [ - "ms-vscode.csharp" - ] + // Comment out to connect as root user. See https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "vscode" } diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 02264bf..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "razor.disabled": true -} diff --git a/README.md b/README.md index 98d9ee3..cfa4e13 100644 --- a/README.md +++ b/README.md @@ -10,18 +10,18 @@ Follow these steps to open this sample in a container: 1. If this is your first time using a development container, please follow the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started). -2. To use this repository, you can either open a locally cloned copy of the code: - - - Clone this repository to your local filesystem. - - Press F1 and select the **Remote-Containers: Open Folder in Container...** command. - - Select the cloned copy of this folder, wait for the container to start, and try things out! - - Or open the repository in an isolated Docker volume: +2. To use this repository, you can either open the repository in an isolated Docker volume: - Press F1 and select the **Remote-Containers: Try a Sample...** command. - Choose the ".NET Core" sample, wait for the container to start and try things out! > **Note:** Under the hood, this will use **Remote-Containers: Open Repository in Container...** command to clone the source code in a Docker volume instead of the local filesystem. + Or open a locally cloned copy of the code: + + - Clone this repository to your local filesystem. + - Press F1 and select the **Remote-Containers: Open Folder in Container...** command. + - Select the cloned copy of this folder, wait for the container to start, and try things out! + 3. If you want to enable **HTTPS**, see [enabling HTTPS](#enabling-https) to reuse your local development cert in the container. ## Things to try @@ -49,13 +49,8 @@ Some things to try: 5. **Forward another port:** - Stop debugging and remove the breakpoint. - - Open `launch.json` - - Add `"ASPNETCORE_Kestrel__Endpoints__Http__Url": "http://*:9000",` to the `"env"` property. - - > **Note:** By default, ASP.NET Core only listens to localhost. If you use the `appPort` property in `.devcontainer/devcontainer.json`, the port is [published](https://docs.docker.com/config/containers/container-networking/#published-ports) rather than forwarded. Unfortunately, this means that ASP.NET Core only listens to localhost inside the container itself, not externally. It needs to listen to `*` or `0.0.0.0` instead to resolve the issue. - > - > This container solves that problem by setting the environment variable `ASPNETCORE_Kestrel__Endpoints__Http__Url` to `http://*:5000` in `.devcontainer/devcontainer.json`. By using an environment variable to override this setting in the container only, you can leave your actual application config as-is for use when running locally. - + - Open `appsettings.Development.json` + - Update `"Url": "http://localhost:5000"` to `"Url": "http://localhost:9000"`. - Press F5 to launch the app in the container. - Press F1 and run the **Forward a Port** command. - Select port 9000. @@ -63,35 +58,47 @@ Some things to try: ### Enabling HTTPS -To enable HTTPS for this sample, you can mount an exported copy of your local dev certificate. First, export it using the following command: +To enable HTTPS for this sample, you can mount an exported copy of your local dev certificate. -**Windows PowerShell** +1. Enable HTTPS in the sample by updating the `env` property in `.vscode/launch.json` as follows: -```powershell -dotnet dev-certs https --trust; dotnet dev-certs https -ep "$env:USERPROFILE/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" -``` + ```json + "env": { + "ASPNETCORE_ENVIRONMENT": "HttpsDevelopment" + } + ``` -**macOS/Linux terminal** +2. Now, locally export the HTTPS certificate using the following command: -```powershell -dotnet dev-certs https --trust; dotnet dev-certs https -ep "${HOME}/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" -``` + **Windows PowerShell** -Next, update the following properties in `.devcontainer/devcontainer.json`: + ```powershell + dotnet dev-certs https --trust; dotnet dev-certs https -ep "$env:USERPROFILE/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" + ``` + + **macOS/Linux terminal** + + ```powershell + dotnet dev-certs https --trust; dotnet dev-certs https -ep "${HOME}/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" + ``` + +3. Next, update the following properties in `.devcontainer/devcontainer.json`: ```json "mounts": [ "source=${env:HOME}${env:USERPROFILE}/.aspnet/https,target=/home/vscode/.aspnet/https,type=bind" ], "remoteEnv": { - "ASPNETCORE_Kestrel__Endpoints__Http__Url": "http://*:5000", - "ASPNETCORE_Kestrel__Endpoints__Https__Url": "https://*:5001", "ASPNETCORE_Kestrel__Certificates__Default__Password": "SecurePwdGoesHere", "ASPNETCORE_Kestrel__Certificates__Default__Path": "/home/vscode/.aspnet/https/aspnetapp.pfx" } ``` -Finally, rebuild the container using the **Remote-Containers: Rebuild Container** command from the Command Palette (F1) if you've already opened your folder in a container so the settings take effect. +> **Note:** See [here for an alternative](https://github.com/microsoft/vscode-dev-containers/blob/v0.42.0/containers/dotnetcore-2.1/README.md#enabling-https-in-aspnet-core) when using an extension version below v0.98.0 as the `forwardPorts` property is not available. + +4. Finally, rebuild the container using the **Remote-Containers: Rebuild Container** command from the Command Palette (F1) if you've already opened your folder in a container so the settings take effect. + +Next time you debug using VS Code (F5), you'll be able to use HTTPS! Note that you will need to specifically navigate to `https://localhost:5001` to get the certificate to work (**not** `https://127.0.0.1:5001`). ## Contributing diff --git a/appsettings.Development.json b/appsettings.Development.json index 8f41d8b..87ad008 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -1,4 +1,11 @@ { + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://localhost:5000" + } + } + }, "Logging": { "LogLevel": { "Default": "Debug", diff --git a/appsettings.HttpsDevelopment.json b/appsettings.HttpsDevelopment.json new file mode 100644 index 0000000..8f41d8b --- /dev/null +++ b/appsettings.HttpsDevelopment.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} \ No newline at end of file