简体   繁体   English

Docker for windows sql express卷数据不会持久存在

[英]Docker for windows sql express volume data does not persist

I'm having difficulty getting my mssql-server-windows-express containerized database to retain any created rows after restarting my computer. 我很难让我的mssql-server-windows-express容器化数据库在重新启动计算机后保留任何创建的行。

It resides in a project that I created with Visual Studio 2017 Community Edition's default "Enable Docker Support" checkbox. 它驻留在我使用Visual Studio 2017 Community Edition的默认“启用Docker支持”复选框创建的项目中。 I am using Entity Framework Core 2 migrations to create and update the database with ASP.NET Core 2.1. 我正在使用Entity Framework Core 2迁移来使用ASP.NET Core 2.1创建和更新数据库。

I feel like the problem might have to do with Docker for Windows having an usual path syntax, but that's just a hunch. 我觉得问题可能与Docker for Windows具有通常的路径语法有关,但这只是一种预感。

I have tried so many different Stackoverflow and blog suggestions and paths, but the data never persists. 我尝试了很多不同的Stackoverflow和博客建议和路径,但数据永远不会存在。 I have to ask for help at this point. 我现在必须要求帮助。

Here are two out of the many docker-compose files and ideas I have tried: 以下是我尝试过的众多docker-compose文件和想法中的两个:

version: '3.4'

services:
  webpresentation:
    image: webpresentation
    build:
      context: .
      dockerfile: WebPresentation\Dockerfile

  db:
    image: microsoft/mssql-server-windows-express
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: Test1

    ports:
      - "1433:1433"
    volumes:
      - "C:\\ProgramData\\Docker\\volumes\\dockercompose4363425345347741_sqlvolume:c:\\var/opt/mssql"

volumes:
  sqlvolume:

This file compiles, but the rows don't persist after restarting. 此文件编译,但重新启动后行不会保留。

version: '3.4'

services:
  webpresentation:
    image: webpresentation
    build:
      context: .
      dockerfile: WebPresentation\Dockerfile

  db:
    image: microsoft/mssql-server-windows-express
    container_name: myDbSqlExpress
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: Test1
      'attach_dbs={"dbName":"MyDb","dbFiles":"C:\\ContainerData\\MyDb.mdf","C:\\ContainerData\\MyDb.ldf"}'


    ports:
      - "1433:1433"
    volumes:
      - "C:\\DockerData:c:\\ContainerData"

volumes:
  sqlvolume:
     name: myDb-sqlvolume

This file gives an error: 该文件出错:

Error MSB4018 The "GetServiceReferences" task failed unexpectedly. 错误MSB4018“GetServiceReferences”任务意外失败。 Microsoft.Docker.Utilities.CommandLineClientException: yaml.scanner.ScannerError: while scanning a simple key in "C:\\Projects\\MyProject\\MyApp\\docker-compose.yml", line 16, column 7 could not find expected ':' Microsoft.Docker.Utilities.CommandLineClientException:yaml.scanner.ScannerError:在“C:\\ Projects \\ MyProject \\ MyApp \\ docker-compose.yml”中扫描一个简单的键时,第16行,第7列找不到预期的':'

What am I doing wrong here? 我在这做错了什么? Can anyone help me correct either file so the newly created rows persist after restarting the computer? 任何人都可以帮我纠正任何一个文件,以便重新启动计算机后新创建的行仍然存在吗?

Thanks very much! 非常感谢!

The docker-compose.yml file that eventually allowed me to persist data after system restarts is as follows: 最终允许我在系统重启后保留数据的docker-compose.yml文件如下:

version: '3.4'

services:
  webpresentation:
    image: webpresentation
    build:
      context: .
      dockerfile: WebPresentation\Dockerfile

  db:
    image: microsoft/mssql-server-windows-express
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: Test1
      attach_dbs: '[{"dbName":"MyProject","dbFiles":["C:\\DockerDbData\\MyProject.mdf","C:\\DockerDbData\\MyProject_log.ldf"]}]'

    ports:
      - "1433:1433"
    volumes:
      - "C:\\Projects\\MyProject\\DockerDbDataVolume:C:\\DockerDbData"

This is not a real answer but too long for a comment, so... 这不是一个真正的答案,但评论太长,所以...

There are two versions of mssql docker container: windows and Linux. mssql docker容器有两个版本:windows和Linux。 You are using the windows container while Linux containers are more common. 您正在使用Windows容器,而Linux容器更常见。 This is why many examples in the Internet will not work as expected. 这就是为什么互联网上的许多例子不能按预期工作的原因。

In your first example it seem that you're using the path used by mssql for Linux (/var/opt is a typical Linux path). 在您的第一个示例中,您似乎正在使用mssql for Linux使用的路径(/ var / opt是典型的Linux路径)。 So 1) find the path that is used inside your container to store db data. 因此1)找到容器内用于存储db数据的路径。

Your second example is simply no valid yaml. 你的第二个例子就是没有效的yaml。 The syntax is key: value so your attach_db line is invalid. 语法是key:value,因此您的attach_db行无效。

From docs: 来自docs:

ENV attach_dbs='[{"dbName":"mydb","dbFiles":["C:\\temp\\mydb.mdf","C:\\temp\\mydb.ldf"]}]'

Here we see that it's an environment variable. 在这里,我们看到它是一个环境变量。 You set env vars like this: 你设置这样的env vars:

env:
  attach_dbs: '[{"dbName":"mydb","dbFiles":["C:\\temp\\mydb.mdf","C:\\temp\\mydb.ldf"]}]'

This should fix your parse error. 这应该可以解决您的解析错误。

Than you should read about volumes here: https://docs.docker.com/compose/compose-file/#volumes 你应该在这里阅读有关卷的内容: https//docs.docker.com/compose/compose-file/#volumes

If you specify a named volume with a separate "volumes" section, the syntax is name:path_inside_container. 如果指定具有单独“卷”部分的命名卷,则语法为name:path_inside_container。

One more note: I'd go with Linux containers. 还有一点需要注意:我会使用Linux容器。 You'll find a menu item "Switch to Linux containers..." in you docker for windows menu (tray icon). 您可以在Docker中找到一个菜单项“切换到Linux容器...”(Windows菜单)(托盘图标)。 If this is disabled, you may have to install Linux from Microsoft store (simply search for ubuntu). 如果禁用此功能,则可能必须从Microsoft商店安装Linux(只需搜索ubuntu)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM