曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Asp-Net-Core开发笔记

Asp-Net-Core Development Notes: Framework-Dependent Deployment Paired with Docker

Previously, I wrote an article about deploying AspNetCore applications using Docker. This approach is very convenient when combined with CICD. After the build, you push the image to a private DockerHub, and on the production server, you can pull the image and run it directly. However, sometimes a more traditional deployment method is needed, such as packaging the executable locally and then placing it directly on the server to run. Once this method is configured, each update only requires re-publishing locally and then uploading the executable.

曦远 (DealiAxy)约 13 分钟
目录7 个章节

Preface

Previously, I wrote an article about deploying an AspNetCore application using Docker. This method, combined with CICD, is very convenient—after building, you push to a private DockerHub, and on the production server, you simply pull the image and run it directly.

However, sometimes a more traditional deployment method is needed, such as packaging an executable locally and then placing it directly on the server to execute.

There are two ways to package an executable in .NetCore:

  • Self-contained executable - Does not depend on any runtime environment; it can be started directly upon execution.
  • Framework-dependent - Requires the corresponding version of the dotnet-runtime to be installed first to run.

While the former is convenient, the packaged file size is relatively large. It is said that there will be optimizations in .NET 8, but we’ll deal with that later. This article records the second method, framework-dependent, combined with Docker deployment, so there is no need to install the runtime on the server.

(PS: It feels a bit like Java deployment)

After configuring this method, each update only requires re-publishing locally, uploading the executable, and overwriting the version on the server. It’s very convenient~

Do Not Generate PDB

Each build generates a bunch of PDB debug files, but we don't need these files during deployment. You can disable them by configuring the .csproj file.

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
  <DebugType>none</DebugType>
  <DebugSymbols>false</DebugSymbols>
</PropertyGroup>

A better approach is to create a common.props file in the solution directory, place the above configuration in this file, and then have each .csproj file reference it. The specific path may vary depending on your actual situation.

<Import Project="../../common.props" />

Packaging

Use the following command to package

dotnet publish -f net7.0 -c Release -p:PublishSingleFile=true

After packaging, it generates an executable file that needs to depend on a runtime to run. Next, prepare the Docker configuration.

dockerfile

The official Dockerfile includes the entire workflow from source code building to deployment. Here we only need base and final, which means placing the executable into Microsoft's base image to run.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM base AS final
WORKDIR /app
COPY . .
ENTRYPOINT ["./AIHub.Blazor"]

docker-compose

If you don't want to type a bunch of docker commands every time, or prefer not to write bash scripts, docker-compose is the best choice. It also makes it very convenient to use with other container orchestration tools.

version: '3.6'

services:
  web:
    image: ${DOCKER_REGISTRY-}web
    container_name: aihub
    restart: always
    environment:
      - ASPNETCORE_ENVIRONMENT=Production
      - ASPNETCORE_URLS=http://+:80
    build:
      context: .
    volumes:
      - .:/app
    ports:
      - "12001:80"
    networks:
      - default
      - swag

networks:
  swag:
    name: swag
    external: true
  default:
    name: aihub

Additional notes:

  • The ports section maps port 80 inside the container.
  • I am using swag to provide the web service, so technically, there's no need to use ports for port mapping.
  • I will later try using candy / traefik to replace nginx (this project does not use nginx; it uses kestrel directly).

Start!

Start using the command line!

docker compose up

If ports are not mapped, you will also need to configure a reverse proxy.

Swag Configuration

Here's the configuration

/path/to/swag/config/nginx/proxy-confs/aihub.subdomain.conf

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name aihub.*;

    include /config/nginx/ssl.conf;

    client_max_body_size 0;

    # enable for ldap auth, fill in ldap details in ldap.conf
    #include /config/nginx/ldap.conf;

    # enable for Authelia
    #include /config/nginx/authelia-server.conf;

    location / {
        # enable the next two lines for http auth
        #auth_basic "Restricted";
        #auth_basic_user_file /config/nginx/.htpasswd;

        # enable the next two lines for ldap auth
        #auth_request /auth;
        #error_page 401 =200 /ldaplogin;

        # enable for Authelia
        #include /config/nginx/authelia-location.conf;

        include /config/nginx/proxy.conf;
        include /config/nginx/resolver.conf;

        set $upstream_app aihub;
        set $upstream_port 80;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;

        proxy_hide_header X-Frame-Options; # Possibly not needed after Portainer 1.20.0
    }
}

_EOF_

Asp-Net-Core Development Notes: Framework-Dependent Deployment Paired with Docker

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Asp-Net-Core开发笔记:在docker部署时遇到一个小坑

哦吼 之前刚说了尝试了使用docker来部署AspNetCore应用,结果这才刚上班就遇到问题了= =… 我这项目用的数据库是Oracle,之前直接运行没啥问题,但放在docker里运行就报了这个错误: ORA-01882: timezone region not found 看这错误信息是在说时区不对,分别在服务器和docker里看一下时间: [root@localhost ~]# date -

2022-02-16 · Asp-Net-Core开发笔记 · 约 1 分钟

Asp-Net-Core开发笔记:快速在已有项目中引入EFCore

很多项目一开始选型的时候没有选择EFCore,不过EFCore确实好用,也许由于种种原因后面还是需要用到,这时候引入EFCore也很方便。 本文以 StarBlog 为例,StarBlog 目前使用的 ORM 是 FreeSQL ,引入 EFCore 对我来说最大的好处是支持多个数据库,如果是 FreeSQL 的话,服务注册的时候是单例模式,只能连接一个数据库,如果需要使用 FreeSQL 同时

2023-10-08 · Asp-Net-Core开发笔记 · 约 1 分钟

Asp-Net-Core开发笔记:使用NPM和gulp管理前端静态文件

前言 本文介绍的是AspNetCore的MVC项目,WebApi+独立前端这种前后端分离的项目就不需要多此一举了~默认前端小伙伴是懂得使用前端工具链的。 为啥要用MVC这种服务端渲染技术呢? 简单项目不需要强行分离增加复杂度(如:我正在开发的博客项目) 后端渲染利于SEO,对博客网站友好 OK,虽然MVC的技术老了点,但依然可以结合现代前端工具链来提高效率 本文的食用需要先安装好Node.js环境

2022-02-17 · Asp-Net-Core开发笔记 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…