{"id":18798615,"url":"https://github.com/liteobject/docker.training","last_synced_at":"2025-10-24T22:38:12.186Z","repository":{"id":172864206,"uuid":"649893257","full_name":"LiteObject/Docker.Training","owner":"LiteObject","description":"Writing a Dockerfile for a C# Application","archived":false,"fork":false,"pushed_at":"2023-12-26T15:53:05.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-24T22:38:10.732Z","etag":null,"topics":["containerization","csharp","docker","dockerfile","tutorial"],"latest_commit_sha":null,"homepage":"","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LiteObject.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-05T21:50:16.000Z","updated_at":"2023-12-26T15:53:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"4c7d49de-1883-453b-ae96-b54ddcc3e4ec","html_url":"https://github.com/LiteObject/Docker.Training","commit_stats":null,"previous_names":["liteobject/docker.training"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LiteObject/Docker.Training","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FDocker.Training","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FDocker.Training/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FDocker.Training/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FDocker.Training/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiteObject","download_url":"https://codeload.github.com/LiteObject/Docker.Training/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FDocker.Training/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280878345,"owners_count":26406641,"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","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["containerization","csharp","docker","dockerfile","tutorial"],"created_at":"2024-11-07T22:12:29.203Z","updated_at":"2025-10-24T22:38:12.170Z","avatar_url":"https://github.com/LiteObject.png","language":"Dockerfile","readme":"# Writing a Dockerfile for a C# Application\r\n\r\n## Introduction:\r\nDocker provides an efficient way to package and distribute applications using containers. In this post, we'll explore the process of writing a Dockerfile to containerize a C# application. We'll cover the essential steps, including building the image, copying the application files, and running the application inside a Docker container.\r\n\r\n## Concepts\r\n\r\n```mermaid\r\n%%{\r\n    init: {\r\n        'theme':'base',\r\n        'themeVariables': {\r\n            'primaryColor': '#BB2528',\r\n            'primaryTextColor': '#fff',\r\n            'primaryBorderColor': '#961e20',\r\n            'lineColor': '#F8B229',\r\n            'secondaryColor': '#00ade0',\r\n            'tertiaryColor': '#fff'\r\n        }\r\n    }\r\n}%%\r\n\r\nflowchart LR\r\n    DF(\"\u003cimg src='./images/file.svg' height='24'/\u003e Dockerfile\") --\u003e|build| DI(\"\u003cimg src='./images/layers.svg' height='24'/\u003e Image\")\r\n    DI --\u003e |push| IR(\"\u003cimg src='./images/database.svg' height='24'/\u003e Registry\")\r\n    IR -..-\u003e |pull| DI\r\n    DI --\u003e |run| DC(\"\u003cimg src='./images/box.svg' height='24'/\u003e Container\")\r\n```\r\n\r\n## Prerequisites:\r\nBefore getting started, make sure you have Docker installed on your machine. If it's not installed, then you can download it from [here](https://docs.docker.com/get-docker/).\r\n\r\n## Step 1: Create a new Dockerfile\r\nStart by creating a new text file named \"Dockerfile\" in the root directory of your C# application. By convention, Dockerfiles are typically named \"Dockerfile\" without any file extension.\r\n\r\n## Step 2: Define the base image\r\nIn the Dockerfile, begin by specifying the base image for your C# application. For example, to use the official .NET 7 SDK as the base image, include the following line:\r\n\r\n* `FROM mcr.microsoft.com/dotnet/sdk:7.0 AS base` \r\n\r\n## Step 3: Set the working directory\r\nNext, set the working directory inside the container where your application's files will be copied. Use the following command:\r\n\r\n* `WORKDIR /app`\r\n\r\n## Step 4: Copy the application files\r\nCopy the necessary files from your local machine to the container. For a basic console application, you can copy the entire project folder using the following command:\r\n\r\n* `COPY . ./`\r\n\r\n The \"COPY\" instruction takes two parameters: the _source_ and the _destination_.\r\n\r\n## Step 5: Build the application\r\nNow, build the C# application inside the container using the appropriate build command. For example, for a .NET Core application, you can run the following command:\r\n\r\n* `RUN dotnet build -c Release`\r\n\r\n## Step 6: Run the application\r\nTo run the application inside the container, use the \"CMD\" instruction. This instruction specifies the command to execute when the container starts. For a console application, use the following command:\r\n\r\n* `CMD [\"dotnet\", \"run\"]`\r\n\r\n## Step 7: Build the Docker image\r\nIn your terminal, navigate to the directory containing the Dockerfile and run the following command to build the Docker image:\r\n\r\n* `docker build -t \u003cYOUR-IMAGE-NAME\u003e .`\r\n\r\n\u003eIn the docker build command, the dot (\".\") represents the build context. The build context is the directory or path that contains the files and directories you want to include in the Docker image. The dot (\".\") is often used as the PATH argument to specify the current directory as the build context.\r\n\r\n## Step 8: Run the Docker container\r\nOnce the image is built, you can create and run a Docker container based on that image. Use the following command:\r\n\r\n* `docker run \u003cYOUR-IMAGE-NAME\u003e`\r\n\r\n## A complete `Dockerfile`\r\n\r\n```yml\r\nFROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base\r\nWORKDIR /app\r\n\r\n# Copy everything\r\nCOPY . .\r\n# Restore as distinct layers\r\nRUN dotnet restore\r\n# Build and publish a release\r\nRUN dotnet publish -c Release -o out\r\n\r\n# Build runtime image\r\nFROM base AS final\r\nWORKDIR /app\r\nCOPY --from=build /app/out .\r\nENTRYPOINT [\"dotnet\", \"DotNet.Docker.dll\"]\r\n```\r\n\r\n## Conclusion:\r\nIn this post, we've covered the essential steps for writing a Dockerfile to containerize a C# application. By following these steps, you can easily package and distribute your C# applications using Docker containers, enabling consistent deployment across different environments.\r\n\r\nRemember, Dockerfiles can be customized based on your specific requirements. You can include additional commands to handle dependencies, expose ports, or optimize the container image. Experiment and explore the various possibilities to make the most of Docker and containerization for your C# applications.\r\n\r\n## Links:\r\n* [Get Docker](https://docs.docker.com/get-docker/)\r\n* [Introduction to .NET and Docker](https://learn.microsoft.com/en-us/dotnet/core/docker/introduction)\r\n* [Tutorial: Containerize a .NET app](https://learn.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows)\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Fdocker.training","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliteobject%2Fdocker.training","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Fdocker.training/lists"}