Mssql-docker: Mount data directory in Windows container

Created on 27 Feb 2018  路  2Comments  路  Source: microsoft/mssql-docker

I'm trying to mount a local directory to the container to be used as the data directory (as we do with the Linux container), but I just can't get it to work.

In my docker-compose.yml file I have:
.\mount\mssql:C:Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL

But I get the error "Cannot create container for service db: invalid volume specification". Maybe this is because of the spaces in the path, but anyway I couldn't find anyone with a similar situation.

How should this be done?
Should I change the default data directory?

Edit: I know that you can pass a JSON as the database to attach, but I was wondering if it was possible to mount instead.

Most helpful comment

@hangy thanks for the answer.
It's been a while now and I forgot to close this issue. I managed to get this working by doing the following:

  • First of all, the directory you will be mounting should have a master.mdf file, otherwise SQL Server will fail to start. You can get an empty master.mdf by starting a new container and copying it over to the host via docker cp.
  • Second, in the Dockerfile you need to remove the DATA folder, otherwise it will fail to mount. Plus, before doing that you need to kill the sqlservr process for it to release the handle. This what I added to my Dockerfile:
    RUN Stop-Process -Name sqlservr -Force ; Remove-Item -Force -Recurse 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA'
  • Third, I added the directories to be mounted to my docker-compose.yml file:
volumes:
  - 'S:\Storage\SqlServer\data:C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA'
  - 'S:\Storage\SqlServer\backup:C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup'

In my case, S:\Storage\SqlServer\data contains that master.mdf file I wrote about in the first step.

This way I didn't need to pass any JSON when calling docker-compose run, databases are automatically attached and I can easily copy to the folders in the host .bak files that I want to restore and SQL Management Studio will see them.

Hope that helps!

All 2 comments

I was trying the same with docker run. The volume definition probably needs spaces. HoweverI, also then found this: https://github.com/docker/for-win/issues/676#issuecomment-357685164

@hangy thanks for the answer.
It's been a while now and I forgot to close this issue. I managed to get this working by doing the following:

  • First of all, the directory you will be mounting should have a master.mdf file, otherwise SQL Server will fail to start. You can get an empty master.mdf by starting a new container and copying it over to the host via docker cp.
  • Second, in the Dockerfile you need to remove the DATA folder, otherwise it will fail to mount. Plus, before doing that you need to kill the sqlservr process for it to release the handle. This what I added to my Dockerfile:
    RUN Stop-Process -Name sqlservr -Force ; Remove-Item -Force -Recurse 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA'
  • Third, I added the directories to be mounted to my docker-compose.yml file:
volumes:
  - 'S:\Storage\SqlServer\data:C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA'
  - 'S:\Storage\SqlServer\backup:C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup'

In my case, S:\Storage\SqlServer\data contains that master.mdf file I wrote about in the first step.

This way I didn't need to pass any JSON when calling docker-compose run, databases are automatically attached and I can easily copy to the folders in the host .bak files that I want to restore and SQL Management Studio will see them.

Hope that helps!

Was this page helpful?
0 / 5 - 0 ratings