返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Asp-Net-Core开发笔记

ASP.NET Core Dev Notes: Using Alpine Image and Adding Health Checks

Use an Alpine image? It just cuts the size in half, adds a health check path, and that's it. wget is already built-in, why install curl? That's pointless. For deployment, choose linux-musl-x64, otherwise it won't run.

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

Introduction

Using Docker to deploy AspNetCore applications has become standard practice. I previously always used images like mcr.microsoft.com/dotnet/aspnet:8.0, which is straightforward, but Alpine-based images can further optimize image size.

Many open-source tools' Docker images include health checks, and I took the opportunity to add them this time as well.

PS: The example project in this article comes from a project that was nearly canceled, "IdentityServerLite". I'll write an article to introduce it later~

Adding Health Checks (健康检查)

Register the service

builder.Services.AddHealthChecks();

Configure the middleware

app.MapHealthChecks("/healthz");

After that, test it. After running locally, you can see the word "Healthy" by visiting http://host:port/healthz.

Modify docker-compose

Add a healthcheck configuration below the application settings.

The image mcr.microsoft.com/dotnet/aspnet comes with wget built-in, so you can use it directly to request the health check endpoint. If you were using curl, you would have to install it.

version: '3.6'

services:
  web:
    image: ${DOCKER_REGISTRY-}web
    container_name: ids-lite
    restart: always
    environment:
      - ASPNETCORE_ENVIRONMENT=Production
      - ASPNETCORE_URLS=http://+:80
    build:
      context: .
    volumes:
      - .:/app
    networks:
      - swag
    healthcheck:
      test: ["CMD-SHELL", "wget --spider http://localhost:80/healthz || exit"]
      interval: 10s
      timeout: 5s
      retries: 5

networks:
  swag:
    name: swag
    external: true

Switching to Alpine Base Image

Before configuring CICD, I used the FrameworkDependent approach to deploy with Docker. For details, see my previous article: Asp-Net-Core开发笔记:FrameworkDependent搭配docker部署

Switching to Alpine can reduce the image size by half, from about 240MB down to around 100MB.

Modify the Dockerfile

Just add -alpine after 8.0

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base
RUN apk add --no-cache icu-libs
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM base AS final
WORKDIR /app
COPY . .
ENTRYPOINT ["./IdsLite.Api"]

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Re-publishing

Previously, when publishing locally, the runtime selected was linux-x64.

The executable published this way depends on glibc.

However, the Alpine base image uses musl libc.

So, it is necessary to select the linux-musl-x64 runtime, then republish.

dotnet publish -r linux-musl-x64

Done!

Reference Materials

_EOF_

ASP.NET Core Dev Notes: Using Alpine Image and Adding Health Checks

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

相关文章

Asp-Net-Core开发笔记:集成Hangfire实现异步任务队列和定时任务

前言 最近把Python写的数据采集平台往.Net Core上迁移,原本的采集任务使用多进程+线程池的方式来加快采集速度,使用Celery作为异步任务队列兼具定时任务功能,这套东西用着还行,但反正就折腾嘛,直接上C#~ 本文记录 Hangfire 在实际应用里的用法,我发现网络上找到的大部分文章都是用 Hangfire 的异步任务输出个 Hello World,然后就没了。我实在不知道这样的文章写

2023-03-09 · Asp-Net-Core开发笔记 · 约 1 分钟

Asp-Net-Core开发笔记:FrameworkDependent搭配docker部署

之前我写过一篇使用 docker 部署 AspNetCore 应用的文章,这种方式搭配 CICD 非常方便, build 之后 push 到私有的 dockerhub ,在生产服务器上 pull 下来镜像就可以直接运行了。 然而,有时需要一种更传统的部署方式,比如在本地打包可执行文件之后直接放到服务器上执行。 这种方式配置完成之后,每次更新只需要重新本地重新 publish 后把可执行文件上传

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

讨论

共 0 条

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

加载评论中…

ASP.NET Core Dev Notes: Using Alpine Image and Adding Health Checks · StarBlog