如何防止在 docker 容器重新启动期间保留 app.db?

问题描述 投票:0回答:1

我创建了一个 ASP.NET Core MVC 应用程序,并选中了“启用容器支持”。这为我提供了 5 种启动应用程序的方法:http、https、IIS Express、Container (dockerfile)、WSL。

我假设当使用“容器(dockerfile)”启动选项时,容器中的任何数据都不会被持久化,但事实似乎并非如此。我的“app.db”文件挂在我创建的测试用户上。我尝试清理解决方案,我尝试停止并删除容器,我厌倦了从 Windows 上的“docker 桌面”工具删除图像和构建,但它们都不起作用。

我知道我错过了一些非常基本的东西,但我已经花了几个晚上了,但我陷入了困境。

以下是我认为可能相关的文件。

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
    }
  }
}
docker asp.net-core asp.net-core-mvc
1个回答
0
投票

请确保您没有选择

Debug
模式。在调试模式下,VS2022将挂载您的项目文件夹以加速调试过程并支持热重载。请务必使用Release模式。

© www.soinside.com 2019 - 2024. All rights reserved.