Published .Net Core Application (Alipay) to Docker

For Demo Alipay branch has just been completed, and I want to deploy it to Docker go, I'll show you the steps below.

Create a profile

The most important configuration file is Dockerfile, he reads as follows:

# 第一部分是编译并发布项目
# 以微软.Net Core SDK作为基础镜像, 并且以build作为别名
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
# 切换build镜像工作目录到/app
WORKDIR /app
# 拷贝sln和csproj项目文件
COPY *.sln .
COPY AliPay/*.csproj ./AliPay/
# Restore项目用到的包
RUN dotnet restore
# 拷贝项目文件到镜像里面相应到目录
COPY AliPay/. ./AliPay/
# 切换build镜像工作目录到/app/AliPay
WORKDIR /app/AliPay
# 以Release模式发布应用到out文件夹
RUN dotnet publish -c Release -o out

# 第二部分是启动项目
# 以微软.Net Core运行环境作为基础镜像, 并且以runtime作为别名
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
# 切换runtime镜像工作目录到/app
WORKDIR /app
# 把build镜像里面编译出来的文件拷贝到runtime镜像
COPY --from=build /app/AliPay/out ./
# 暴漏端口
EXPOSE 8000
# 启动应用
# ENTRYPOINT ["dotnet", "AliPay.dll", "--server.urls", "http://*:8000"]
ENTRYPOINT ["dotnet", "AliPay.dll"]

It may also be a more .dockerignore, as follows:

bin\
obj\

Packaged mirror

After creating Dockerfile, it will be very easy to create a mirror:

docker build -t alipayimg .

Inside the two parameters:

  • -t: Specifies the name of the target image to be created
  • .: Dockerfile file directory, you can specify the absolute path Dockerfile

Start container

Finally boot image:

docker run -d -p 8666:8000 --name alipay alipayimg

Parameter Description:

  • -d: After the image up and running in the background
  • -p 8666: 8000: mapping between port, host port: Container Port
  • Name starts containers: --name alipay
  • alipayimg: image name used

Programming summary

At first, I follow the online presentation, use the following command has been designated ports:

dotnet AliPay.dll --server.urls http://*:8000

And it has been ineffective.

Github said the need to add Microsoft.Extensions.Configuration.CommandLine, but still finished adding does not work, no way, ultimately, the port-coded in the code:

Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.ConfigureAppConfiguration(config =>
        {
            config.AddJsonFile("alipay.json");
            // config.AddCommandLine(args);
        });
        webBuilder.UseUrls("http://*:8000");
        webBuilder.UseStartup<Startup>();
    });

Access Application

Opening the browser and go to http: // localhost: 8666, you're done.

Guess you like

Origin www.cnblogs.com/jerryqi/p/11778645.html