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.
目录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开发笔记:使用ActionFilterAttribute实现非侵入式的参数校验
用ActionFilterAttribute搞非侵入校验,省得重复写代码。支持Query、Body、Route、Header,HttpContext共享数据,但OAuth2.0边学边做,项目乱糟糟,功能凑合。
2024-05-17 · Asp-Net-Core开发笔记 · 约 1 分钟
Asp-Net-Core开发笔记:Oracle调优
Supported Connection String Attributes To obtain an OracleConnection object in a .NET stored procedure that represents the implicit database connection, set the ConnectionString property of the Oracle
2023-03-05 · 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 分钟
讨论
共 0 条加载评论中…