{"id":17114300,"url":"https://github.com/anthonychu/aspnet-env-docker","last_synced_at":"2025-04-13T03:54:11.300Z","repository":{"id":50352923,"uuid":"110092872","full_name":"anthonychu/aspnet-env-docker","owner":"anthonychu","description":null,"archived":false,"fork":false,"pushed_at":"2021-10-28T14:58:32.000Z","size":11,"stargazers_count":25,"open_issues_count":1,"forks_count":18,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T03:54:06.191Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anthonychu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-09T09:14:48.000Z","updated_at":"2024-01-23T21:55:49.000Z","dependencies_parsed_at":"2022-08-28T07:24:12.281Z","dependency_job_id":null,"html_url":"https://github.com/anthonychu/aspnet-env-docker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonychu%2Faspnet-env-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonychu%2Faspnet-env-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonychu%2Faspnet-env-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonychu%2Faspnet-env-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anthonychu","download_url":"https://codeload.github.com/anthonychu/aspnet-env-docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661716,"owners_count":21141450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T17:16:28.726Z","updated_at":"2025-04-13T03:54:11.022Z","avatar_url":"https://github.com/anthonychu.png","language":"PowerShell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dockerfile - ASP.NET Windows container with support for web.config overrides at startup\n\nThis image can be used just like [`microsoft/aspnet`](https://hub.docker.com/r/microsoft/aspnet/). At container startup, it'll perform these additional steps, in order:\n\n1. If `C:\\web-config-transform\\transform.config` exists, it'll use this file to transform the Web.config\n1. Override Web.config with environment variables:\n    - Environment variables prefixed with `APPSETTING_` will override the corresponding app setting value (without the prefix)\n    - Environment variables prefixed with `CONNSTR_` will override the corresponding connection string (without the prefix)\n1. Override Web.config values with kubernetes secrets\n    - Secrets prefixed with `APPSETTING_` will override the corresponding app setting value (without the prefix)\n    - Secrets prefixed with `CONNSTR_` will override the corresponding connection string (without the prefix)\n\nMore information: \u003chttps://anthonychu.ca/post/overriding-web-config-settings-environment-variables-containerized-aspnet-apps/\u003e\n\n## Creating a base aspnet-env-docker image\n\nThere are some pre-created base images in [docker hub](https://hub.docker.com/r/anthonychu/aspnet/).\n\nIf the image you want isn't there then it is simple to create your own base image to use.\n\nOnce you have identified the version of the [microsoft/aspnet](https://hub.docker.com/r/microsoft/aspnet/) image that you want to base the image on, update the Dockerfile e.g.\n\n```Dockerfile\nFROM microsoft/aspnet:4.7.2-windowsservercore-1803\n...\n```\n\nThen run docker from the aspnet-env-docker folder to build the image\n\n```posh\nPS\u003e docker build -t yourrepo/aspnet .\n```\n\n## Building an image with your application\n\nTo containerize an existing ASP.NET 4.x application build on either one of the precreated images or the image you just built:\n\n```dockerfile\nFROM anthonychu/aspnet:4.7.1-windowsservercore-1709\nWORKDIR /inetpub/wwwroot\nCOPY sample-aspnet-4x .\n```\n\n## Applying web.config transforms\n\nAssuming `C:\\transform` contains a file named `transform.config`, apply the web.config transformation at container startup:\n\n```posh\nPS\u003e docker run -d -p 80:80 -v C:\\transform:C:\\web-config-transform sample-webforms-app\n```\n\n## Specifying app settings and connection strings with docker\n\nDocker allows you to specify environment variables for a container when you run it.\n\nThis project allows you to override app settings by using environment variables prefixed with `APPSETTING_` and connection strings with environment variables prefixed with `CONNSTR_`.\n\nTo override an app setting named `PageTitle` and a connection string named `DefaultConnection` using environment variables:\n\n```posh\nPS\u003e docker run -d -p 80:80 -e APPSETTING_PageTitle=Foo -e CONNSTR_DefaultConnection=\"connection string from environment\" sample-webforms-app\n```\n\nThis can be combined, so to apply a web.config transform and then override the `PageTitle` app setting with an environment variable:\n\n```posh\nPS\u003e docker run -d -p 80:80 -e APPSETTING_PageTitle=Foo -v C:\\transform:C:\\web-config-transform sample-webforms-app\n```\n\n## Specifying app settings and connection strings with Kubernetes\n\n### Environment variables\n\nWith Kubernetes we can use both environment variables and secrets to override web.config values.\n\nTo use environment variables we can create a deployment with the following YAML\n\n```yaml\napiVersion: extensions/v1beta1\nkind: Deployment\nmetadata:\n    labels:\n        run: sample-webforms-app\n    name: sample-webforms-app\nspec:\n    replicas: 1\n    selector:\n    matchLabels:\n        run: sample-webforms-app\n    template:\n        metadata:\n            labels:\n                run: sample-webforms-app\n        spec:\n            containers:\n                - image: sample-webforms-app\n                  imagePullPolicy: Always\n                  name: sample-webforms-app\n                  ports:\n                  - containerPort: 80\n                    protocol: TCP\n                env:\n                - name: APPSETTING_PageTitle\n                  value: \"Foo\"\n                - name: CONNSTR_DefaultConnection\n                  value: \"connection string from environment\"\n            restartPolicy: Always\n```\n\n### Secrets\n\nYou can use Kubernetes secrets instead of, or in combination with, environment variables.\n\nWhen you create a Kubernetes secret the value has to be base64 encoded. The following PowerShell function provides a quick way to do this .\n\n```posh\nfunction Base64Encode($value){\n    [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value))\n}\n```\n\nE.g.\n\n```posh\nPS\u003e Base64Encode \"Hello World\"\nSGVsbG8gV29ybGQ=\n```\n\nWe can create secrets for app settings and connection strings in a similar way as above\n\n```yaml\napiVersion: v1\nkind: Secret\nmetadata:\n    name: myaspnetapp-secret\ntype: Opaque\ndata:\n    APPSETTING_PageTitle: VGhpcyBpcyB0aGUgdGl0bGUh\n    CONNSTR_DefaultConnection: Q29ubmVjdGlvbiBzdHJpbmcgaGVyZQ==\n```\n\nTo use the secrets with a deployment, mount them into the pod:\n\n```yaml\napiVersion: extensions/v1beta1\nkind: Deployment\nmetadata:\n    labels:\n        run: myaspnetapp\n    name: myaspnetapp\nspec:\n    replicas: 1\n    selector:\n    matchLabels:\n        run: myaspnetapp\n    template:\n        metadata:\n            labels:\n                run: myaspnetapp\n        spec:\n            containers:\n                - image: stuartleeks/myaspnetapp\n                  imagePullPolicy: Always\n                  name: myaspnetapp\n                  ports:\n                  - containerPort: 80\n                    protocol: TCP\n                env:\n                - name: ASPNET_SECRETS_PATH\n                   value: \"c:\\\\secrets\"\n                - name: APPSETTING_PageText\n                   value: \"Hello from an environment variable set in the Kubernetes deployment spec\"\n                volumeMounts:\n                    - name: test-secret\n                      mountPath: \"c:\\\\secrets\"\n            volumes:\n                - name: test-secretsecret:\n                      secretName: myaspnetapp-test-secret\n                      defaultMode: 256\n            restartPolicy: Always\n```\n\nIn the example above we are mounting the previously defined secret in `c:\\secrets` and also specifying an app setting override via an environment variable. If the same override exists in both environment variables and secrets, the secret version is used.\n\nThere is also an additional `ASPNET_SECRETS_PATH` in the example above. This is optional if you mount the secrets in `c:\\secrets` but allows you tell the scripts where to load the secrets from if you want to mount them to a different location.\n\n\n## More information\n\nCheck out these blog posts for more:\n- [Overriding Web.config Settings with Environment Variables in Containerized ASP.NET Applications (with No Code Changes)](https://anthonychu.ca/post/overriding-web-config-settings-environment-variables-containerized-aspnet-apps/)\n- [ASP.NET Web.config Transforms in Windows Containers - Revisited](https://anthonychu.ca/post/aspnet-web-config-transforms-windows-containers-revisited/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonychu%2Faspnet-env-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanthonychu%2Faspnet-env-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonychu%2Faspnet-env-docker/lists"}