MSSQL on Linux using Docker for Mac
Docker and the macOS filesystems: the eternal struggle
I'm working on a project that requires a MS stack but I really don't want to run Windows on a VM, plus we have a part of the project that isn't dependent from such a stack.
To code C# using .NET Core has being rather interesting. It works but you can feel it isn't mature at all. You should be aware of the project files hell that's happening as they still wondering what is best: csproj, json, xproj, and the list goes on.
TL:DR
There is a problem between Docker and the macOS filesystem which prevents state to get saved.
Use the following setup on your docker-compose.yml
file:
The problem
Every time you need to stop your compose or you restart the mssql service you lose your data and that is annoying.
The issue is because when you mount the mssql volume using the following lines, the way Docker gets it created has some limitations that impact MSSQL.
But why?
SQL Server requires underlying filesystems to support the O_DIRECT file option so that we can use asynchronous I/O. It appears that Docker’s OS X volume mapper doesn’t support this. We also require O_DIRECT because SQL Server expects unbuffered I/O to the disk. — @twright-msft
Solution
As mentined before on the TL;DR, when you choose to create the volumes on that way you'll find out your data stored.
I needed it to work on a docker-compose
file, but they offered a solution for those that want to run it without docker-compose
.
docker create -v /var/opt/mssql --name mssql \
microsoft/mssql-server-linux \
/bin/truedocker run \
-e ‘ACCEPT_EULA=Y’ \
-e ‘SA_PASSWORD=Test@123’ \
-p 1433:1433 \
--volumes-from mssql \
-d \
--name sql-server \
microsoft/mssql-server-linux
One last tip
When testing with your but running the service alone, without using docker-compose up
remember to always add the -p "1433:1433"
ports configuration because by design it ignores the ports declared on your docker-compose.yml
when you run
Just changed the code below to include
--service-ports
as Leonardo Saraiva said on his comment. Thanks man!
docker-compose run --rm --service-ports mssql
Have fun!