Docker for MacでSQLServerを建てる際にハマったのでメモ（永続化）

# はじめに
Azureでサービスを作ることになり、ローカル環境にDockerでSQLServerを立てて永続化させる際に行ったことをメモとして残します。

# 普通に書いてみる
MySQLと同じ感覚で書いてみて起動してみる。

```dockerfile:docker-compose.yaml

version: "3"
services:
  sqlserver:
    image: microsoft/mssql-server-linux:2017-latest
    container_name: mssql
    hostname: mssql
    volumes:
      - ./.db:/var/opt/mssql/data
    ports:
      - 14330:1433
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: SQLServer2017
```
実行すると以下のようなエラーが発生する。

```bash

〉 docker-compose up
Recreating mssql ... done
Attaching to mssql
mssql        | 2020-07-05 22:00:46.51 Server      Setup step is copying system data file 'C:\templatedata\master.mdf' to '/var/opt/mssql/data/master.mdf'.
2020-07-05 22:00:46.86 Server      Did not find an existing master data file /var/opt/mssql/data/master.mdf, copying the missing default master and other system database files. If you have moved the database location, but not moved the database files, startup may fail. To repair: shutdown SQL Server, move the master database to configured location, and restart.
2020-07-05 22:00:46.88 Server      Setup step is copying system data file 'C:\templatedata\mastlog.ldf' to '/var/opt/mssql/data/mastlog.ldf'.
2020-07-05 22:00:46.94 Server      Setup step is copying system data file 'C:\templatedata\model.mdf' to '/var/opt/mssql/data/model.mdf'.
2020-07-05 22:00:47.53 Server      Setup step is copying system data file 'C:\templatedata\modellog.ldf' to '/var/opt/mssql/data/modellog.ldf'.
2020-07-05 22:00:48.26 Server      Setup step is copying system data file 'C:\templatedata\msdbdata.mdf' to '/var/opt/mssql/data/msdbdata.mdf'.
2020-07-05 22:00:49.40 Server      Setup step is copying system data file 'C:\templatedata\msdblog.ldf' to '/var/opt/mssql/data/msdblog.ldf'.
2020-07-05 22:00:49.56 Server      Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64) 
mssql   Nov 30 2018 12:57:58 
mssql   Copyright (C) 2017 Microsoft Corporation
mssql   Developer Edition (64-bit) on Linux (Ubuntu 16.04.5 LTS)
2020-07-05 22:00:49.56 Server      UTC adjustment: 0:00
2020-07-05 22:00:49.56 Server      (c) Microsoft Corporation.
2020-07-05 22:00:49.57 Server      All rights reserved.
2020-07-05 22:00:49.57 Server      Server process ID is 4120.
2020-07-05 22:00:49.57 Server      Logging SQL Server messages in file '/var/opt/mssql/log/errorlog'.
2020-07-05 22:00:49.57 Server      Registry startup parameters: 
mssql    -d /var/opt/mssql/data/master.mdf
mssql    -l /var/opt/mssql/data/mastlog.ldf
mssql    -e /var/opt/mssql/log/errorlog
2020-07-05 22:00:49.59 Server      Error: 17113, Severity: 16, State: 1.
2020-07-05 22:00:49.59 Server      Error 87(The parameter is incorrect.) occurred while opening file '/var/opt/mssql/data/master.mdf' to obtain configuration information at startup. An invalid startup option might have caused the error. Verify your startup options, and correct or remove them if necessary.
mssql exited with code 1
`````

# 解決方法
調べてみると、Docker for Macだとvolumn mappingはサポートされていないらしい。
https://github.com/microsoft/mssql-docker/issues/12

ただデータを永続化できないと色々面倒な問題があったので、書き方を調べてみた結果、以下のような書き方で動作することが確認できた。

```docker:docker-compose.yaml

version: "3"
services:
  sqlserver:
    image: microsoft/mssql-server-linux:2017-latest
    container_name: mssql
    hostname: mssql
    volumes:
      - ./.db:/var/opt/mssql/
      - /var/opt/mssql/data
    ports:
      - 14330:1433
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: SQLServer2017
```

# おわりに
もし間違いがあればご指摘いただけると助かります。