| title | Secure SQL Server Docker containers |
|---|---|
| description | Understand the different ways to secure SQL Server Docker containers and how you can run containers as different non-root user on the host |
| author | vin-yu |
| ms.author | vinsonyu |
| ms.reviewer | vanto |
| ms.date | 09/04/2020 |
| ms.topic | conceptual |
| ms.prod | sql |
| ms.technology | linux |
| moniker | >= sql-server-linux-2017 || >= sql-server-2017 || =sqlallproducts-allversions |
[!INCLUDE SQL Server - Linux]
SQL Server 2017 containers start up as the root user by default. This can cause some security concerns. This article talks about security options that you have when running SQL Server Docker containers, and how to build a SQL Server container as a non-root user.
Follow the steps below to build a SQL Server 2017 container that starts up as the mssql(non-root) user.
Note
SQL Server 2019 containers automatically start up as non-root, so the following steps only apply to SQL Server 2017 containers, which start as root by default.
-
Download the sample dockerfile for non-root SQL Server Container and save it as
dockerfile. -
Run the following command in the context of the dockerfile directory to build the non-root SQL Server container:
cd <path to dockerfile> docker build -t 2017-latest-non-root .
-
Start the container.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword@" --cap-add SYS_PTRACE --name sql1 -p 1433:1433 -d 2017-latest-non-root
[!NOTE] The
--cap-add SYS_PTRACEflag is required for non-root SQL Server containers to generate dumps for troubleshooting purposes. -
Check that the container is running as non-root user:
docker exec -it sql1 bashRun
whoami, which will return the user running within the container.whoami
To run the SQL Server container as a different non-root user, add the -u flag to the docker run command. The non-root container has the restriction that it must run as part of the root group unless a volume is mounted to /var/opt/mssql that the non-root user can access. The root group doesn’t grant any extra root permissions to the non-root user.
You can start SQL Server with a custom UID. For example, the command below starts SQL Server with UID 4000:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" --cap-add SYS_PTRACE -u 4000:0 -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latestWarning
Ensure that the SQL Server container has a named user such as 'mssql' or 'root' or SQLCMD will not be able to run within the container. You can check if the SQL Server container is running as a named user by running whoami within the container.
You can run the non-root container as the root user if necessary. This would also grant all file permissions automatically to the container because it has higher privilege.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" -u 0:0 -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latestYou can start SQL Server with an existing user on the host machine with the following command:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" --cap-add SYS_PTRACE -u $(id -u myusername):0 -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latestYou can start SQL Server with a custom user and group. In this example, the mounted volume has permissions configured for the user or group on the host machine.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" --cap-add SYS_PTRACE -u (id -u myusername):(id -g myusername) -v /path/to/mssql:/var/opt/mssql -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latestTo allow the non-root user to access database files that are on mounted volumes, ensure that the user or group you run the container under can read/write the persistent file storage.
You can get the current ownership of the database files with this command.
ls -ll <database file dir>Run one of the following commands if SQL Server does not have access to persisted database files.
Grant the root group permissions to the following directories so that the non-root SQL Server container has access to database files.
chgrp -R 0 <database file dir>
chmod -R g=u <database file dir>This can be the default non-root user, or any other non-root user you’d like to specify. In this example, we set UID 10001 as the non-root user.
chown -R 10001:0 <database file dir>::: moniker range="= sql-server-linux-2017 || = sql-server-2017"
- Get started with SQL Server 2017 container images on Docker by going through the quickstart
::: moniker-end
::: moniker range=">= sql-server-linux-ver15 || >= sql-server-ver15 || =sqlallproducts-allversions"
- Get started with SQL Server 2019 container images on Docker by going through the quickstart
::: moniker-end