Automated Deployment of ASP.NET Core Applications with Jenkins, Docker, and IIS
In my previous attempts to automate the deployment of ASP.NET Core applications on IIS using Jenkins, I encountered several challenges. Jenkins, as a continuous integration and continuous deployment (CI/CD) tool, is designed to automate the build, test, and deployment of software projects. However, in my case, the process of getting the code from the Git repository to the IIS server was not smooth.
Initially, I used Jenkins on Ubuntu, which allowed me to leverage Docker at the beginning of the deployment process. However, due to the need to stop and start IIS sites remotely, I had to use PowerShell to operate the server. Despite the use of WinRM and PowerShell scripts, I encountered issues with the script stopping the site occasionally entering a fake state, and the copy document process failing due to file occupation. This forced me to manually stop the site, which defeated the purpose of automation.
To overcome these challenges, I decided to use Docker to mirror my ASP.NET Core application. Docker provides strong support for .NET Core, and I had previously admired the tools available for Docker, which enabled automatic completion of safety and environmental configuration without effort. After the release of .NET Core, I always tried to run my Core applications in Docker, but I was hesitant to use it in a formal environment due to the deployment issues.
Using Docker to Mirror ASP.NET Core Applications
To use Docker to mirror my ASP.NET Core application, I followed these steps:
- Jenkins Installation: I installed Jenkins on my system.
- Docker Mounting: I mounted the Docker container directly from the Jenkins store using the following command:
docker run -d -p 8080:8080 --name jenkins jenkins/jenkins:lts
This command starts a new Jenkins container named “jenkins” and maps port 8080 on the host machine to port 8080 in the container.
3. Jenkins Job: After the container starts, I created a new Jenkins job to automate the deployment process. The job consists of the following steps:
* Source Code Control: I configured the job to retrieve the source code from our Git repository.
* Build Command Execution: I executed a shell command to build the application using the following command:
dotnet build -c Release
This command builds the application in release mode.
* Image Creation: I created a new image using the following command:
docker build -t myapp .
This command creates a new image named “myapp” based on the Dockerfile in the current directory.
* Image Push: I pushed the newly created image to a remote repository using the following command:
docker tag myapp:latest myaccount/myapp:latest
docker push myaccount/myapp:latest
This command tags the image with the latest version and pushes it to the remote repository.
4. Dockerfile: The Dockerfile for my ASP.NET Core 2.1 application is as follows:
FROM mcr.microsoft.com/dotnet/core/sdk:2.1
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet build -c Release
EXPOSE 80
CMD ["dotnet", "run"]
This Dockerfile creates a new image based on the .NET Core SDK 2.1 image and configures the application to run on port 80.
Challenges and Solutions
One of the challenges I faced was the presence of useless instructions in the Dockerfile, which resulted in the image being larger than necessary. To solve this issue, I removed the unnecessary instructions from the Dockerfile.
In conclusion, using Jenkins, Docker, and IIS to automate the deployment of ASP.NET Core applications is possible. By following the steps outlined above, developers can create a smooth and automated deployment process that minimizes manual intervention and ensures the efficient deployment of applications to production environments.