Sql Server on docker: error duplicating volume

Simone Demuro 50 Reputation points
2023-08-31T12:50:55.5566667+00:00

I started to run my database from docker using a volume to persist the data. The idea around this was to easily create disposable versions of my database to run disruptive tests against them.

However, after I clone my volume, I get the following error starting a Sql Server container with it:

2023-08-31 14:05:06 /opt/mssql/bin/sqlservr: Error: The system directory [/.system] could not be created. File: LinuxDirectory.cpp:420 [Status: 0xC0000022 Access Denied errno = 0xD(13) Permission denied]
2023-08-31 14:05:06 SQL Server 2022 will run as non-root by default.
2023-08-31 14:05:06 This container is running as user mssql.
2023-08-31 14:05:06 Your master database file is owned by mssql.
2023-08-31 14:05:06 To learn more visit https://go.microsoft.com/fwlink/?linkid=2099216.

Reproduction Steps:

  1. Run Sql Server from within docker with a volume attached:
    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<pass>" -p 1402:1433 -v sqlvolume:/var/opt/mssql -d --name dockersql mcr.microsoft.com/mssql/server:2022-latest
  2. Import database, by Import data-tier application in SSMS
  3. Clone the volume:
    As suggested in [this article]:
    docker run --rm -i -t -v sqlvolume:/origen -v sqlvolume_clone:/destino alpine sh -c "cp -avr /origen/* /destino"
  4. Run again Sql Server with the cloned volume
    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<pass>" -p 1402:1433 -v sqlvolume_clone:/var/opt/mssql -d --name dockersql mcr.microsoft.com/mssql/server:2022-latest

The attempt to run Sql Server using the cloned volume will lead to the attached above error log. Runnig the database from the original volume will correctly start the database.

Thanks & Best Regards,
Simone

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,716 questions
{count} vote

Accepted answer
  1. Magnus Ahlkvist 76 Reputation points MVP
    2023-08-31T22:54:26.1033333+00:00

    Your problem is that the alpine container will run as root, thus creating all the files with root as owner, while the SQL Server container runs as the user mssql with uid=10001. So when the SQL Server container starts, running as the user mssql, it can't access the files in /var/opt/mssql because they are owned by root.

    I'm glad I saw this question, because it got me to invest a little time in solving the issue, as I've been facing the same thing.

    Two small additions to step 3 are needed:

    • create a user named mssql with uid=10001.
    • after copying the files, do chown to let mssql own all the files on the destination volume.

    Like so:

    docker run --rm -i -t -v sqlvolume:/origen -v sqlvolume_clone:/destino alpine sh -c "adduser -u 10001 mssql; cp -avr /origen/* /destino; chown mssql -R destino"
    

    I just tested it, and indeed the new container can now read contents from sqlvolume_clone


0 additional answers

Sort by: Most helpful