Docker learning notes – deploy the. Net core 3.1 project to the docker container and use nginx reverse proxy (centos7) (1)

Time:2020-11-27

The previous section shows how to install docker. Link:Docker learning notes – installing docker in CentOS

This section demonstrates the deployment of. Net core 3.1 into the docker container, and the deployment platform CentOS 7.8 using nginx reverse proxy

1. Install nginx support (CentOS 7.8 does not include nginx by default)(If you want to use the nginx container reverse proxy, you can skip the first step)

Step 1: install nginx

Unlike the Ubuntu system, CentOS can be installed directly. You need to add the source first

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

The figure above shows that the addition is successful. Next, execute the installation command

sudo yum install -y nginx

Installation completed:

Start nginx service

sudo systemctl start nginx.service

After startup, verify whether the installation is successful through the following script. The IP address here is the IP address of your CentOS system. How to view the IP address can see the previous chapter

curl http://172.18.237.116/

The following indicates that the installation is successful.

Attached is a reference document:https://www.cnblogs.com/tenghao510/p/11990353.html 

Step 2: publish the project

Next, start the body, compile your. Net project, add docker support, right-click the project, and select Add docker support (this part is required)

Then choose Linux, mainly to publish to CentOS

After confirmation, a dockerfile file will be added to the project

The default content is:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["myAPI/myAPI.csproj", "myAPI/"]
RUN dotnet restore "myAPI/myAPI.csproj"
COPY . .
WORKDIR "/src/myAPI"
RUN dotnet build "myAPI.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "myAPI.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myAPI.dll"]

In fact, the production environment only needs the following.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
COPY . /app
EXPOSE 80
ENTRYPOINT ["dotnet", "myAPI.dll"]

Explanation:

mcr.microsoft.com/dotnet/core/aspnet : 3.1-buster-slim basic image, that is. Net core runtime;

Expose 80 refers to the exposed port of the container;

Workdir / app is the working directory of the container;

Copy. / app is to copy the current directory (where.It represents the current directory) to the / APP directory in the container;

Entrypoint specifies the running object of the container

Right click Project > publish > configure publishing content

Configuration content: select “dependent framework” for deployment mode, linux-x64 for runtime, click, save and publish

After publishing successfully, the project is uploaded to the server through the software xftp

Create a project folder in the CentOS service root directory to store the project to be uploaded,

It is very convenient to complete through xftp. Right click to create a new folder: netcoredemo, and upload the file on the left

The published project does not contain dockerfile, which needs to be uploaded separately. The dockerfile is also uploaded to the netcoredemo folder of the project

Verify whether the upload is successful through the command ls. As shown in the figure below, the project has been uploaded successfully

Now that the project is uploaded to the server, the next step is to deploy it to docker!

It’s a bit too long after all the demonstrations, so I’ll show them in the next section!

Docker learning notes 1 – deploy the. Net core 3.1 project to the docker container and use nginx reverse proxy (centos7) (2)