i have a web api and want create docker image, so i created docker file and trying to run but it is throwing exception when creating IIS web site
here is docker file
# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
# configure the new site in IIS.
RUN `
New-Item -Path "C:\" -Name "Apigateway" -ItemType "directory";`
Import-module Webadministration;`
New-IISSite -Name "AxApigateway" -BindingInformation "*:8080:" -PhysicalPath 'C:\Apigateway';`
# This instruction tells the container to listen on port 83.
EXPOSE 83
# The final instruction copies the site you published earlier into the container.
COPY /Published/ C:\Apigateway

Mayooran NavamanyRank #438 · 5.7K reputationPosted Dec 22, 2023, 12:23 AM EST
Use of Forward Slash in COPY command: The path separator in Docker is always forward slash ("/"), regardless of the host operating system. Therefore, in the COPY command, you should use forward slashes instead of backslashes.
Change:
COPY /Published/ C:\Apigatewayto:
COPY /Published/ /ApigatewayIncorrect Site Name in New-IISSite: In the
New-IISSitecommand, the site name you specified is "AxApigateway," but when creating the directory and copying files, you used "Apigateway." Make sure they match.Change:
New-IISSite -Name "AxApigateway" -BindingInformation "*:8080:" -PhysicalPath 'C:\Apigateway';`to:
New-IISSite -Name "Apigateway" -BindingInformation "*:8080:" -PhysicalPath 'C:\Apigateway';`With these changes, your Dockerfile should look like this:
# escape=` FROM mcr.microsoft.com/windows/servercore:ltsc2019 SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] # configure the new site in IIS. RUN ` New-Item -Path "C:\" -Name "Apigateway" -ItemType "directory";` Import-module Webadministration;` New-IISSite -Name "Apigateway" -BindingInformation "*:8080:" -PhysicalPath 'C:\Apigateway';` # This instruction tells the container to listen on port 83. EXPOSE 83 # The final instruction copies the site you published earlier into the container. COPY /Published/ /ApigatewayMake these changes and try building and running the Docker image again.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 22, 2023, 12:51 AM EST
To deploy a C# Web API on an IIS server using Docker, you can use the following Dockerfile:
# escape=` FROM mcr.microsoft.com/windows/servercore:ltsc2019 SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] # Install IIS RUN Install-WindowsFeature -name Web-Server -IncludeManagementTools # Create a directory for the API RUN New-Item -Path "C:\" -Name "Apigateway" -ItemType "directory" # Copy the published API files to the container COPY /Published/ C:\Apigateway # Configure the new site in IIS RUN Import-Module WebAdministration; ` New-IISSite -Name "AxApigateway" -BindingInformation "*:8080:" -PhysicalPath 'C:\Apigateway' # Expose the port EXPOSE 8080 # Start the IIS service CMD ["C:\\ServiceMonitor.exe", "w3svc"]In this Dockerfile, we start with the
mcr.microsoft.com/windows/servercore:ltsc2019base image, which provides the Windows Server Core operating system. We then install IIS using theInstall-WindowsFeaturecommand.Next, we create a directory named "Apigateway" in the container's C drive using the
New-Itemcommand. We copy the published API files from the/Published/directory on the host machine to theC:\Apigatewaydirectory in the container using theCOPYcommand.Finally, we configure the new site in IIS using the
New-IISSitecommand, specifying the site name, binding information, and physical path. We expose port 8080 using theEXPOSEinstruction. TheCMDinstruction starts the IIS service using theServiceMonitor.exeutility.Make sure to replace
/Published/with the actual path to your published API files on the host machine.With this Dockerfile, you can build the Docker image using the
docker buildcommand and run it using thedocker runcommand. The API will be accessible on port 8080 of the container.