{"id":25898084,"url":"https://github.com/sowad12/dockerize-.net-core-web-api-and-sql-server","last_synced_at":"2026-05-05T10:33:53.587Z","repository":{"id":280175234,"uuid":"941023362","full_name":"sowad12/Dockerize-.NET-Core-Web-Api-And-SQL-Server","owner":"sowad12","description":"Dockerize .NET Core 8 AND SQL Server ","archived":false,"fork":false,"pushed_at":"2025-03-01T18:17:45.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T19:25:29.517Z","etag":null,"topics":["asp-net-core","docker","sql-server"],"latest_commit_sha":null,"homepage":"","language":"C#","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/sowad12.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":"2025-03-01T09:48:44.000Z","updated_at":"2025-03-01T18:17:48.000Z","dependencies_parsed_at":"2025-03-01T19:25:32.561Z","dependency_job_id":"b59a58e5-ea87-4db0-b219-2391ab8d899a","html_url":"https://github.com/sowad12/Dockerize-.NET-Core-Web-Api-And-SQL-Server","commit_stats":null,"previous_names":["sowad12/dockerize-.net-core-web-api-and-sql-server"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sowad12%2FDockerize-.NET-Core-Web-Api-And-SQL-Server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sowad12%2FDockerize-.NET-Core-Web-Api-And-SQL-Server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sowad12%2FDockerize-.NET-Core-Web-Api-And-SQL-Server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sowad12%2FDockerize-.NET-Core-Web-Api-And-SQL-Server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sowad12","download_url":"https://codeload.github.com/sowad12/Dockerize-.NET-Core-Web-Api-And-SQL-Server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241587845,"owners_count":19986628,"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":["asp-net-core","docker","sql-server"],"created_at":"2025-03-03T00:16:49.544Z","updated_at":"2026-05-05T10:33:53.539Z","avatar_url":"https://github.com/sowad12.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Table of Contents\n- [Requirements](#Requirements)\n- [Short_brief](#Short_brief)\n- [Web API](#Web_API)\n- [Dockerfile](#Dockerfile)\n- [Docker_Compose](#Docker_Compose)\n- [Docker_Repository](#Docker_Repository)\n- [SQL_SERVER_Connection](#SQL_SERVER_Connection)\n- [SQL_SERVER_Connection_Issue](#SQL_SERVER_Connection_Issue)\n  \n\n# Requirements\n.NET 8.0,Microsoft SQL Server 2022 And Docker Desktop\n\n# Short_brief\n\nI containerized a .NET Core Web API and Microsoft SQL Server 2022 using Docker Compose for an efficient, portable development environment. I created Dockerfiles to package both services and configured them to work together within a shared Docker network. Finally, I pushed the built images to a Docker repository for easy deployment and scalability.\n\n# Web_API\n\nI created a simple Web API with two API endpoints. One retrieves all data from the database, and the other seeds (inserts) data.\n\n![swagger-api](https://github.com/user-attachments/assets/01831d8f-3c30-40b9-8d1c-9d4e1cbaa18f)\n\n# Dockerfile\nWhen creating a Web API with 'Enable Container Support' selected, Visual Studio automatically generates a Dockerfile containing the necessary instructions to build your image.\n\n![dockerfileCreation](https://github.com/user-attachments/assets/1d9f0db3-ada6-4b0f-9151-e8b6bf2cc416)\n\n**Dockerfile**\n```dockerfile\n # See https://aka.ms/customizecontainer...\n# Provides a link to documentation for customizing the container and debugging with Visual Studio.\n\n# This stage is used when running from VS in fast mode...\n# Introduces the base stage used for fast debugging (default in Debug configuration).\nFROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base\n# Uses the ASP.NET 8.0 runtime image as the base image, labeling this stage as base.\n\nUSER app\n# Switches the user context to app for better security.\n\nWORKDIR /app\n# Sets the working directory inside the container to /app.\n\nEXPOSE 8080\n# Exposes port 8080 for HTTP traffic.\n\nEXPOSE 443\n# Exposes port 443 for HTTPS traffic.\n\n# This stage is used to build the service project\n# Introduces the build stage for compiling the Web API project.\nFROM mcr.microsoft.com/dotnet/sdk:8.0 AS build\n# Uses the .NET SDK 8.0 image as the build environment, labeling this stage as build.\n\nARG BUILD_CONFIGURATION=Release\n# Defines a build argument for the configuration, defaulting to Release.\n\nWORKDIR /src\n# Sets the working directory to /src in the build container.\n\nCOPY [\"main/main.csproj\", \"main/\"]\n# Copies the project file from the local main folder to the container’s main folder.\n\nRUN dotnet restore \"./main/main.csproj\"\n# Restores NuGet packages for the project.\n\nCOPY . .\n# Copies the entire source code into the container.\n\nWORKDIR \"/src/main\"\n# Changes the working directory to /src/main where the project is located.\n\nRUN dotnet build \"./main.csproj\" -c $BUILD_CONFIGURATION -o /app/build\n# Builds the project using the specified configuration, outputting binaries to /app/build.\n\n# This stage is used to publish the service project...\n# Introduces the publish stage that creates the deployable output.\nFROM build AS publish\n# Starts a new stage named publish based on the build stage.\n\nARG BUILD_CONFIGURATION=Release\n# Re-declares the build configuration argument for this stage.\n\nRUN dotnet publish \"./main.csproj\" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false\n# Publishes the project for deployment, outputting to /app/publish and disabling the use of an app host.\n\n# This stage is used in production or when running from VS in regular mode...\n# Introduces the final stage for production or standard Visual Studio runs (non-Debug mode).\nFROM base AS final\n# Begins the final stage using the runtime image from the base stage, labeling it final.\n\nWORKDIR /app\n# Sets the working directory in the final container to /app.\n\nCOPY --from=publish /app/publish .\n# Copies the published output from the publish stage into the final image.\n\nENTRYPOINT [\"dotnet\", \"main.dll\"]\n# Specifies the command to run when the container starts (launching the Web API).\n\n```\n\n# Docker_Compose\nTo add a docker-compose.yml file from Visual Studio, right-click on your Web API project and select Add → Container Orchestrator Support. The default options will be pre-selected in the following steps.\n\n![selection](https://github.com/user-attachments/assets/5b11c2ad-29f8-4153-bec6-1c87014bf07f)\n![docker-compose-01](https://github.com/user-attachments/assets/fc6f920f-cdab-43c8-ab66-216697c25d27)\n\n![docker-compose-02](https://github.com/user-attachments/assets/2e346053-fbc8-4b50-99a5-558791e2955b)\n\n**docker-compose.yml**\n```yml\nversion: '3.4'  # Defines the Docker Compose file format version.\n\nservices:\n  SqlServerDb:  # Defines the SQL Server service.\n    container_name: sqlserverdb  # Sets a custom name for the container.\n    image: mcr.microsoft.com/mssql/server:2022-latest  # Uses the official SQL Server 2022 image.\n    ports:\n      - 8002:1433  # Maps port 1433 inside the container to port 8002 on the host.\n    environment:\n      - ACCEPT_EULA=Y  # Accepts the SQL Server license agreement.\n      - MSSQL_SA_PASSWORD=ewww@20230302  # Sets the SA (admin) password for SQL Server.\n    volumes:\n      - sqlserver_data:/var/opt/mssql  # Creates a persistent volume for storing database files(it works as a backup if we remove container).\n    networks:\n      - myAppNetwork  # Connects this service to the custom network.\n\n  main:  # Defines the Web API service.\n    container_name: webApiContainer  # Sets a custom name for the Web API container.\n    image: ${DOCKER_REGISTRY-}dockerintro  # Uses an optional registry prefix and the \"dockerintro\" image.\n    environment:\n      - ASPNETCORE_ENVIRONMENT=Development  # Sets the ASP.NET Core environment to Development.\n      - ConnectionStrings__DefaultConnection=Server=sqlserverdb,1433; Database=dockerintro; User Id=sa; Password=ewww@20230302; TrustServerCertificate=True;  # Database connection string.\n    ports:\n      - 8001:8080  # Maps port 8080 inside the container to port 8001 on the host.\n    build:\n      context: .  # Uses the current directory as the build context.\n      dockerfile: ./main/Dockerfile  # Specifies the path to the Dockerfile for building the Web API.\n    depends_on:\n      - SqlServerDb  # Ensures the database container starts before this service.\n    networks:\n      - myAppNetwork  # Connects the Web API container to the same network as the database.\n\nnetworks:\n  myAppNetwork:  # Defines a custom network for the services to communicate.\n\nvolumes:\n  sqlserver_data:  # Defines a named volume to persist SQL Server data.\n\n```\nNow in this project directory run this command for Creation of respective docker images and containers\n```cmd\ndocker-compose -f docker-compose.yml up -d\n```\nthen got images and containers\n\n![docker-images](https://github.com/user-attachments/assets/4571a7d6-af27-46d2-b73c-101e05498c6a)\n\n![docker-containers](https://github.com/user-attachments/assets/852ec16e-27aa-4fc7-8c5a-9d58b92bb719)\n\n# Docker_Repository\nFor Push this image need to follow some steps \n\n**Step-1: At first login docker desktop**\n\n**Step-2: Now go the windows powershell or cmd**\n\n**Step-3: Run Docker login command for access repository**\n```cmd\ndocker login\n```\n**Step-4: Create new image tag for push this image(use account name ensuring the image is associated with your account and to avoid naming conflicts.)**\n\n```docker tag \u003ctarget image name\u003e:\u003ctag\u003e \u003caccount name\u003e/\u003canyname for latest image\u003e:\u003ctag\u003e``` \n\nMy Host machine:\n```cmd\ndocker tag dockerintro:latest sowad/dockerintro:1.0\n```\n\n![newlyTag](https://github.com/user-attachments/assets/a2e3a849-9cca-4ade-93d7-11da7404be59)\n\n**Step-5: At last push newly tagged image to the repository**\n\n```docker push \u003cnewly tagged image name\u003e:\u003ctag\u003e``` \n\nMy Host machine:\n```cmd\ndocker tag dockerintro:latest dockerintro:1.0\n```\n\n![dockerRepo](https://github.com/user-attachments/assets/22eacd01-bdca-4e39-98d8-7f5ef8b20024)\n\nFor pulling image:\n\n```cmd\ndocker push \u003cacc_name\u003e/\u003cimage_name\u003e:\u003ctagname\u003e\n```\n\n# SQL_SERVER_Connection\nIn this demo mssql server Port Mapping is 8002:1433 where\n- **8002 (Host Machine Port):** The port on your local machine where you can access the SQL Server container.  \n- **1433 (Container Port):** The default port SQL Server uses inside the container.\n\nFor MS SQL Server, we need to define two connection strings: one for use outside Docker and another for use inside Docker.\n  \n**Outside Docker (Local Development):** Connection Between Host Machine and SQL Server Container.\n\n**In appsettings.json:**\n\n ```\"Server=localhost,\u003chost port\u003e; Database=\u003cdatabse name\u003e; User Id=sa; Password=\u003cpassword\u003e; TrustServerCertificate=True;\"```\n \n My Host machine:\n```json\n   \"ConnectionStrings\": {\n       \"DefaultConnection\": \"Server=localhost,8002; Database=dockerintro; User Id=sa; Password=ewww@20230302; TrustServerCertificate=True;\"\n  }\n```\n**Inside Docker:** services within the same Docker network communicate via container names.In this way mssql server connection works inside docker\n\n**In docker-compose.yml:**\n\nServer=\u003cmssql_container_name\u003e,1433; Database=\u003cdatabse_name\u003e; User Id=sa; Password=\u003cpassword\u003e; TrustServerCertificate=True;\n\nFor my setup, I use:\n```yml\n  environment:\n   - ConnectionStrings__DefaultConnection=Server=sqlserverdb,1433; Database=dockerintro; User Id=sa; Password=ewww@20230302; TrustServerCertificate=True;\n```\n\n**Mssql Server Connection check wtih container:**\n\nBefore testing the connection, first start the MSSQL Server container if it is stopped.\n\n![mssqlServerConnection](https://github.com/user-attachments/assets/6cd1c489-6111-44e6-9535-0e1d4d740549)\n\n\n# SQL_SERVER_Connection_Issue\n\nIf you face tcp or network related issue.open Sql server configuration management.Then enable TCP/IP\n\n![tcpconnection](https://github.com/user-attachments/assets/9c56d1a3-d686-4f02-8b5c-3f08dcba0186)\n\nThen Click windows+R write services.msc \n\n![msc](https://github.com/user-attachments/assets/b83efcb7-92d2-4815-a429-12ecb605918c)\n\nAfter this it's better restart your pc.\n\nIf this solution does not work, try inspecting the MSSQL Server container to check if there is any issue with the Dockerfile or docker-compose.yml.\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsowad12%2Fdockerize-.net-core-web-api-and-sql-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsowad12%2Fdockerize-.net-core-web-api-and-sql-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsowad12%2Fdockerize-.net-core-web-api-and-sql-server/lists"}