Implementation of one key deployment of asp.net core Jenkins docker

Time:2019-10-16

Written in front

Some time ago, I tried to use Jenkins to automatically deploy asp.net core program on IIS. The general process is that Jenkins gets the code from GIT.

Jenkins was originally placed in the docker of Ubuntu, but because of PowerShell execution, Jenkins was moved to windows. Because the deployment of our website needs to stop the IIS site, so we need PowerShell to remotely operate the server (upload files, stop the site, start the site), and successfully use the winrm + PowerShell script to achieve the above functions. However, in the actual use, it is found that the script stop site occasionally has a false state, and the copy file always fails due to occupation. Therefore, you can only stop the site manually, so you can’t achieve the goal of one click, so you can use docker to image.

Docker Image

. net core strongly supports docker. Before core, I especially envied my experience of using some tools in docker — one line of instructions can make the tools automatic, safe and complete, without laborious environment configuration, and there are very few intrusions to the system. So after the core comes out, I always try to run the core program in docker, but I haven’t dared to go to the formal environment. Now I encounter the problem of automatic deployment, so I plan to start from the test environment and apply the docker environment to the formal environment step by step. The general process is as follows:

Jenkins installation

When installing Jenkins in docker, you can search the image of Jenkins directly on store.docker.com, and then run the container. The only thing you need to pay attention to is that Jenkins in the container share the docker environment of the host. The command is as follows

Copy codeThe code is as follows:
docker run –memory 1.5G  –name ContainerName -p 18181:8080 -p 50000:50000 -u root -d –env JAVA_OPTS=”-Xms256m -Xmx512m  -XX:MaxNewSize=256m”  -v /var/run/docker.sock:/var/run/docker.sock  -v /usr/bin/docker:/usr/bin/docker  -v /home/buxiaoxia/software/jenkins:/var/jenkins_home -v /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 jenkins/jenkins:lts

The following – V is used to specify the file / folder of the host to mount to the specified path of the host. Here, it is mainly used to mount the docker environment of the host. When the container is started, we can use the command


docker exec -it containerid /bin/bash

Enter the container and execute the command of docker PS to see if it can be executed successfully.

Jenkins Job

After the container is running and some initialization work is done, we can start the deployment job.

Configure our git address in source control

And then build the command to execute the shell directly.

#!/bin/bash
#Get the short version number to use the image version number

GITHASH=`git rev-parse --short HEAD`
Docker build - t {imagename}: $Gith - F {dockerfile PWD} {initial directory of image}

This is the building of our image.

Next, push the currently built image to the remote warehouse, and then perform remote connection to the server to be published. According to the version number, get the image to be deployed and run it. Because I am currently testing the environment, I directly run the image locally.

Docker stop {containername} stops the original container
Docker RM {containername} delete the original
docker run -p 9526:80 -d -e ASPNETCORE_ENVIRONMENT='Development' -v /Path/Path/Logs:/app/App_Data/Logs --restart always --name {containername} {imagename}:$GITHASH

In this way, one click deployment is basically realized. Of course, there are still many areas that need to be optimized. Next, if I encounter problems with the use or you have problems in the use process, we will solve them.

Written in the end

DockerFile

At the beginning of building the asp.net core 2.1 image, the dotnet SDK will not be found when running. Finally, only a layer of file content is added to the dockerfile as follows

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app

#Copy all files to the environment and restore, build and release
COPY . .
RUN dotnet restore
RUN dotnet build
WORKDIR /app/src/Path
RUN dotnet publish -c Release -o out
 


FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
WORKDIR /app
Copy -- from = build / APP / SRC / path / out. / copy the published file to the image
Copy -- from = build / APP / SRC / path / bin / debug / netcoreapp2.1/xxx.xml. / copy the XML used by swagger to the directory

EXPOSE 80
Run ln - SF / usr / share / zoneinfo / Asia / Shanghai / etc / Localtime ා localization of time zone
RUN echo 'Asia/Shanghai' >/etc/timezone
Entrypoint ["dotnet", "project. DLL"] (start program)

The problem with this method is that there are some useless Images Sharing instructions to clear it.

Docker PS - a | grep "exited" | awk '{print $1}' | xargs docker stop ා stop the container with exited status
Docker PS - a | grep "exited" | awk '{print $1}' | xargs docker RM ා delete containers with exited status
Docker images | grep none | awk '{print $3}' | xargs docker RMI ා delete the image with tag of none


 Docker RMI $(docker images | grep imagename | awk '{print $3}')) ා delete the image with imagename (only for images that are not used)

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.