dotnet dockerfile 撰寫筆記
dotnet/aspnet:{version} 版本的 dockerfile 調整方式與此相同,因為 os 都是 debain
FROM mcr.microsoft.com/dotnet/aspnet:$dotnetVersion-bullseye-slim AS base
RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /etc/ssl/openssl.cnf
RUN sed -i 's/MinProtocol = TLSv1.2/MinProtocol = TLSv1/g' /etc/ssl/openssl.cnf
RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /usr/lib/ssl/openssl.cnf
RUN sed -i 's/MinProtocol = TLSv1.2/MinProtocol = TLSv1/g' /usr/lib/ssl/openssl.cnf
DEFAULT:@SECLEVEL=1
的部分改為 DEFAULT:@SECLEVEL=0
FROM mcr.microsoft.com/dotnet/aspnet:$dotnetVersion-bullseye-slim AS base
RUN sed -i 's/openssl_conf = openssl_init/#openssl_conf = openssl_init/' /etc/ssl/openssl.cnf
RUN sed -i '1i openssl_conf = default_conf' /etc/ssl/openssl.cnf && \
echo "\n[ default_conf ]\nssl_conf = ssl_sect\n[ssl_sect]\nsystem_default = system_default_sect\n[system_default_sect]\nMinProtocol = TLSv1\nCipherString = DEFAULT:@SECLEVEL=1" >> /etc/ssl/openssl.cnf
在我的網路環境中,alpine 的 sdk image 也需要加上相同設定才可以連線到私有的 nuget server
FROM mcr.microsoft.com/dotnet/aspnet:$dotnetVersion-alpine AS base
RUN sed -i 's/openssl_conf = openssl_init/#openssl_conf = openssl_init/' /etc/ssl/openssl.cnf
RUN sed -i '1i openssl_conf = default_conf' /etc/ssl/openssl.cnf && \
echo -e "\n[ default_conf ]\nssl_conf = ssl_sect\n[ssl_sect]\nsystem_default = system_default_sect\n[system_default_sect]\nMinProtocol = TLSv1\nCipherString = DEFAULT:@SECLEVEL=1" >> /etc/ssl/openssl.cnf
# Build
FROM mcr.microsoft.com/dotnet/sdk:$dotnetVersion-alpine AS build
RUN sed -i 's/openssl_conf = openssl_init/#openssl_conf = openssl_init/' /etc/ssl/openssl.cnf
RUN sed -i '1i openssl_conf = default_conf' /etc/ssl/openssl.cnf && \
echo -e "\n[ default_conf ]\nssl_conf = ssl_sect\n[ssl_sect]\nsystem_default = system_default_sect\n[system_default_sect]\nMinProtocol = TLSv1\nCipherString = DEFAULT:@SECLEVEL=1" >> /etc/ssl/openssl.cnf
FROM mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj" -s https://your-nuget-server.domain.com
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build/net6 -f net6.0
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build/net7 -f net7.0
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish/net6 /p:UseAppHost=false -f net6.0
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish/net7 /p:UseAppHost=false -f net7.0
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
ARG dotnetVersion=8.0
ARG releaseOsType=linux
WORKDIR /src
COPY ["OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins/OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj", "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins/"]
RUN dotnet restore "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins/OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
COPY . .
WORKDIR "/src/OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins"
RUN dotnet build "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
-c Release \
-o /app/build \
-f net${dotnetVersion} \
--os ${releaseOsType} \
--arch x64
FROM build AS publish
ARG dotnetVersion=8.0
ARG releaseOsType=linux
RUN dotnet publish "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
-c Release \
-o /app/publish \
-f net${dotnetVersion} \
--os ${releaseOsType} \
--arch x64
通常用於明確知道 base image 是哪個 OS 與 .net 版本時
RUN dotnet build "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
-c Release \
-o /app/build \
-f net8.0 \
--os linux \
--arch x64
RUN dotnet publish "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
-c Release \
-o /app/publish \
-f net8.0 \
--os linux \
--arch x64
RUN dotnet build "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
-c Release \
-o /app/build \
-f net8.0 \
--os linux-musl \
--arch x64
RUN dotnet publish "OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins.csproj" \
-c Release \
-o /app/publish \
-f net8.0 \
--os linux-musl \
--arch x64
由於 bookworm-slim, jammy, alpine 的 OpenSSL 版本都使用 OpenSSL 3.x,如果想在 dockerfile 中調整 TLS 最低版本的話,要改用以下命令
如果加入以下命令後仍無法存取資料庫或部分 tls1.0 的服務,可以將 DEFAULT:@SECLEVEL=1
的部分改為 DEFAULT:@SECLEVEL=0
RUN sed -i 's/openssl_conf = openssl_init/#openssl_conf = openssl_init/' /etc/ssl/openssl.cnf
RUN sed -i '1i openssl_conf = default_conf' /etc/ssl/openssl.cnf && \
echo "\n[ default_conf ]\nssl_conf = ssl_sect\n[ssl_sect]\nsystem_default = system_default_sect\n[system_default_sect]\nMinProtocol = TLSv1\nCipherString = DEFAULT:@SECLEVEL=1" >> /etc/ssl/openssl.cnf
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up