Reference web address: https://www.cnblogs.com/merray/p/12834242.html
1.VS2019 Create a NETCORE 3.0 project
Select Docker
DockerFile is automatically created when Docker is selected
#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 ["netdemo3.csproj", ""] RUN dotnet restore "./netdemo3.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "netdemo3.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "netdemo3.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "netdemo3.dll"]
#Introducing Mirrors FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base #Set Working Directory WORKDIR /app #Ports used by programs in containers must correspond to ports used by program startup EXPOSE 80 #copy file to mirror working directory Documents will not be found if copy does not run COPY . . #Start Program ENTRYPOINT ["dotnet", "demoapi.dll"]
2.DockerFile settings: The automatically generated dockerfile contains compilation and publishing. Here we upload the published file to linux after compilation on the windows platform, so we directly reduce the compilation and publishing part, leaving only the mirror download copy and the running part
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build WORKDIR /app COPY . . EXPOSE 6001 ENTRYPOINT ["dotnet", "netdemo3.dll"]
Port settings:
DockerFile EXPOSE port 6001 must be consistent with the project's listening port. The netcore port is configured in the appsettings.json file, as follows, using the urls parameter to bind the port. Be sure to use the form'*: port'. This place did not notice at first that the docker's external network access has been failing and it took a long time to find out why.
1 2 3 4 5 6 7 8 9 10 11 12 { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "urls": "http://*:6002;http://*:6001" }
3. Publish the project in VS to a local folder and upload the files in the publish directory to centos after publishing:
The upload path here is/home/www/netdemo, using xftp6
4.xshell logs in to centos, then goes to the upload directory just now/home/www/netdemo, creates the mirror container and starts
The main instructions used are the following:
#Compile to generate image docker build -t netdemo:v1 . #Check if image is successful docker images #Once successful, create a container from the image and run docker run --name=demo1 -p 9000:6001 -d netdemo:v1 docker run -p 9000:6001 demoapi:v1 #Check if the container is running successfully docker container ls #Because it's a website, use curl here to test if the machine is accessible properly curl http://localhost:9000
For the first time use, there are some problems. If the container or image creation fails, you can delete all the containers and images again.
The complete test record is as follows:
# docker build -t netdemo:v1 . Sending build context to Docker daemon 18.68MB Step 1/6 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base 3.1-buster-slim: Pulling from dotnet/core/aspnet 54fec2fa59d0: Pull complete 573788d8ba26: Pull complete d8e35c95ac02: Pull complete e158ea73cf60: Pull complete 5c38381dab2d: Pull complete Digest: sha256:21d9448c98bf4968b72f64c484117f6bf04e27ff3ebc6af6ff7ba3ed1e894f82 Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim ---> 79e79777c3bf Step 2/6 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build 3.1-buster: Pulling from dotnet/core/sdk 90fe46dd8199: Pull complete 35a4f1977689: Pull complete bbc37f14aded: Pull complete 74e27dc593d4: Pull complete caa6ad693f93: Pull complete aae86a99db0a: Pull complete 95f813d5736b: Pull complete Digest: sha256:d706e0545b75615ecd864c6af237cc1fc2ca9001ed25cdd84b83fdb3923e9e54 Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-buster ---> 4aa6a74611ff Step 3/6 : WORKDIR /app ---> Running in 5d52f409826f Removing intermediate container 5d52f409826f ---> eb732de5e9a8 Step 4/6 : COPY . . ---> 7c187a46b754 Step 5/6 : EXPOSE 6001 ---> Running in 9ea0c2171d7c Removing intermediate container 9ea0c2171d7c ---> d2ed487c5b2a Step 6/6 : ENTRYPOINT ["dotnet", "netdemo3.dll"] ---> Running in 8110e32a0ffc Removing intermediate container 8110e32a0ffc ---> 29aa9a8c4637 Successfully built 29aa9a8c4637 Successfully tagged netdemo:v1 # docker images REPOSITORY TAG IMAGE ID CREATED SIZE netdemo v1 29aa9a8c4637 About a minute ago 709MB mcr.microsoft.com/dotnet/core/sdk 3.1-buster 4aa6a74611ff 2 weeks ago 691MB mcr.microsoft.com/dotnet/core/aspnet 3.1-buster-slim 79e79777c3bf 2 weeks ago 207MB # docker run --name=demo1 -p 9000:6001 -d netdemo:v1 95cf9056e3f25e5e9f60cb86a26111e3d5b3623020a06961ccbd643ee37921e2 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95cf9056e3f2 netdemo:v1 "dotnet netdemo3.dll" 7 seconds ago Up 6 seconds 0.0.0.0:9000->6001/tcp demo1 # curl http://localhost:9000 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Home Page - netdemo3</title> <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css" /> <link rel="stylesheet" href="/css/site.css" /> </head> <body> <header> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <div class="container"> <a class="navbar-brand" href="/">netdemo3</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse"> <ul class="navbar-nav flex-grow-1"> <li class="nav-item"> <a class="nav-link text-dark" href="/">Home</a> </li> <li class="nav-item"> <a class="nav-link text-dark" href="/Home/Privacy">Privacy</a> </li> </ul> </div> </div> </nav> </header> <div class="container"> <main role="main" class="pb-3"> <div class="text-center"> <h1 class="display-4">Welcome</h1> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div> </main> </div> <footer class="border-top footer text-muted"> <div class="container"> © 2020 - netdemo3 - <a href="/Home/Privacy">Privacy</a> </div> </footer> <script src="/lib/jquery/dist/jquery.min.js"></script> <script src="/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0"></script> </body> </html>
As shown above, because it's a website, successful curl access means successful publishing.
The above test environment:
Server: CentOS 8+docker 19.06.8+netcore 3.1
Developer: VS2019+win10 1909
In August 2021, a new version of docker desktop 20 was used to test for problems:
The port on which the netcore webapi program runs under 1 windows desktop must be set with the urls parameter or the port configured by default as 5000 5001 dockerfile will not take effect, resulting in failure to map the docker port
So the ports set by the docker file must be consistent with the application ports or you will not be able to access the ports initially suspected to be docker ip issues. It turns out that versions after docker 20 can use the docker directly without any additional installation to perform compilation and publishing
New docker desktop containers, mirrors, and operational status can be displayed on the desktop without instructions
Mirror List: List actions are given on the right side of the diagram to delete or run
Container list: As shown in the following illustration, web programs that start, stop, delete containers can also automatically open browsers for easy access
CLI commands can also be executed automatically: it is very convenient to enter the container directory directly as shown below
Tested windows docker container can directly access the same machine IP as the host, without additional settings, guessing that the network connection mode is the host mode and the host share IP