如何避免Docker容器重启时app.db数据被持久化?
问题描述
我创建了一个ASP.NET Core MVC应用并勾选了“启用容器支持”,该应用有五种启动方式:http、https、IIS Express、Container (Dockerfile)、WSL。我原本以为使用“Container (Dockerfile)”启动时,容器内的数据不会被持久化,但实际情况并非如此——我的app.db文件仍保留着创建的测试用户。我尝试过清理解决方案、停止并删除容器、在Windows的Docker Desktop中移除镜像和构建文件,但都没有效果。我知道自己遗漏了一些基础内容,但已经卡了几个晚上了。以下是我认为相关的文件:
Dockerfile
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. # This stage is used when running from VS in fast mode (Default for Debug configuration) FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base USER app WORKDIR /app EXPOSE 8080 EXPOSE 8081 # This stage is used to build the service project FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["DockerTest2/DockerTest2.csproj", "DockerTest2/"] RUN dotnet restore "./DockerTest2/DockerTest2.csproj" COPY . . WORKDIR "/src/DockerTest2" RUN dotnet build "./DockerTest2.csproj" -c $BUILD_CONFIGURATION -o /app/build # This stage is used to publish the service project to be copied to the final stage FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./DockerTest2.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false # This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration) FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DockerTest2.dll"]
.dockerignore
**/.classpath **/.dockerignore **/.env **/.git **/.gitignore **/.project **/.settings **/.toolstarget **/.vs **/.vscode **/*.*proj.user **/*.dbmdl **/*.jfm **/azds.yaml **/bin **/charts **/docker-compose* **/Dockerfile* **/node_modules **/npm-debug.log **/obj **/secrets.dev.yaml **/values.dev.yaml LICENSE README.md !**/.gitignore !.git/HEAD !.git/config !.git/packed-refs !.git/refs/heads/** **/app.db <<<<<<<<< added by me **/app.db-shm **/app.db-wal
launchSettings.json
{ "profiles": { "http": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": true, "applicationUrl": "http://localhost:5106" }, "https": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": true, "applicationUrl": "https://localhost:7201;http://localhost:5106" }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "Container (Dockerfile)": { "commandName": "Docker", "launchBrowser": true, "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", "environmentVariables": { "ASPNETCORE_HTTPS_PORTS": "8081", "ASPNETCORE_HTTP_PORTS": "8080" }, "publishAllPorts": true, "useSSL": true }, "WSL": { "commandName": "WSL2", "launchBrowser": true, "launchUrl": "https://localhost:7201", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_URLS": "https://localhost:7201;http://localhost:5106" }, "distributionName": "" } }, "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:53919", "sslPort": 44308 } } }
内容的提问来源于stack exchange,提问作者PBG
相关产品推荐
相关产品推荐

