🔥
Dockerコンテナのタイムゾーンを日本にする(.NET Core)
DateTime.Nowで日本時間にならない
閲覧いただき、ありがとうございます。
VisualStudio2022で mvcプロジェクトを作成するとdockerfileを自動生成してくれます。
そのまま使用してしまうと日本時間にはならないので、以下のように修正します。
・修正前
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["TimeStampTestApp/TimeStampTestApp.csproj", "TimeStampTestApp/"]
RUN dotnet restore "TimeStampTestApp/TimeStampTestApp.csproj"
COPY . .
WORKDIR "/src/TimeStampTestApp"
RUN dotnet build "TimeStampTestApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TimeStampTestApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY /app/publish .
ENTRYPOINT ["dotnet", "TimeStampTestApp.dll"]
・修正後
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
+ ENV TZ=Asia/Tokyo
+ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["TimeStampTestApp/TimeStampTestApp.csproj", "TimeStampTestApp/"]
RUN dotnet restore "TimeStampTestApp/TimeStampTestApp.csproj"
COPY . .
WORKDIR "/src/TimeStampTestApp"
RUN dotnet build "TimeStampTestApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TimeStampTestApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TimeStampTestApp.dll"]
Discussion