How to attach an .MDF file in a host directory data volume from within Docker mssql-server-linux container?

I am mounting a host directory as a data volume with the following:

docker run -v c:\volumes:/var/opt/mssql/data2 
           -e "ACCEPT_EULA=Y" 
           -e "SA_PASSWORD=QWEqwe1!" 
           -e 'MSSQL_PID=Express'
           -p 1433:1433 
           -d microsoft/mssql-server-linux:latest

I created the volume with the instructions in the configuration topic here: https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-docker#persist.

My host directory contains an .mdf file and an empty .ldf file. When attempting to attach the .mdf file using the following TSQL:

USE [master]
GO
CREATE DATABASE [MY_DATABASE] ON 
( FILENAME = N'/var/opt/mssql/data2/MY_DATABASE.mdf' ),
( FILENAME = N'/var/opt/mssql/data2/MY_DATABASE_log.ldf' )
FOR ATTACH
GO

I receive the following ERROR:

"Error: 824, Severity: 24, State: 6.

2018-04-19 19:37:31.29 spid57 SQL Server detected a logical consistency-based I/O error: Insufficient bytes transferred. Common causes are backup configuration, insufficient disk space, or other problems with the storage subsystem such as corruption or hardware failure. Check errorlogs/application-logs for detailed messages and correct error conditions.. It occurred during a read of page (0:0) in database ID 0 at offset 0000000000000000 in file 'C:\var\opt\mssql\data\SS18SV_CORE_log.ldf'. Additional messages in the SQL Server error log or operating system error log may provide more detail. This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online."

The error.log contains:

"ERROR 5105 A file activation error occurred. The physical file name '\var\opt\mssql\data2\SS18SV_CORE.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.

2018-04-19 19:00:14.79 spid54 Error: 824, Severity: 24, State: 6."

Any ideas how I can attach the .mdf file and .ldf file from my data volume successfully?

I am using Docker Community Edition Version 18.03.0-ce-win59 (16762) for Windows. My OS is Windows 10 Enterprise.

Just had the issue myself. In case any one else comes across this, the solution is to change the backslashes (from the attach script generated by SSMS) to forwarded slashes:`
USE [master]
GO
CREATE DATABASE [EnergyManagement] ON
( FILENAME = N’/sql/NameOfMDFFile.mdf’ ),
( FILENAME = N’/sql/NameOfLogFile.ldf’ )
FOR ATTACH
GO

`
In the above example I have /sql mounted as a folder on my host Windows machine machine

1 Like

Thanks @sytaub , you saved my time.