--- title: Configure and customize SQL Server Docker containers description: Understand the different ways to customize SQL Server Docker Containers and how you can configure it based on your requirement author: vin-yu ms.author: vinsonyu ms.reviewer: vanto ms.custom: contperfq1 ms.date: 09/07/2020 ms.topic: conceptual ms.prod: sql ms.technology: linux moniker: ">= sql-server-linux-2017 || >= sql-server-2017 || =sqlallproducts-allversions" zone_pivot_groups: cs1-command-shell --- # Configure and customize SQL Server Docker containers [!INCLUDE [SQL Server - Linux](../includes/applies-to-version/sql-linux.md)] This article explains how you can configure and customize SQL Server Docker containers, such as persisting your data, moving files from and to containers, and changing default settings. ## Create a customized container It is possible to create your own [Dockerfile](https://docs.docker.com/engine/reference/builder/#usage) to create a customized SQL Server container. For more information, see [a demo that combines SQL Server and a Node application](https://github.com/twright-msft/mssql-node-docker-demo-app). If you do create your own Dockerfile, be aware of the foreground process, because this process controls the life of the container. If it exits, the container will shut down. For example, if you want to run a script and start SQL Server, make sure that the SQL Server process is the right-most command. All other commands are run in the background. The following command illustrates this inside a Dockerfile: ```bash /usr/src/app/do-my-sql-commands.sh & /opt/mssql/bin/sqlservr ``` If you reversed the commands in the previous example, the container would shut down when the do-my-sql-commands.sh script completes. ## Persist your data Your SQL Server configuration changes and database files are persisted in the container even if you restart the container with `docker stop` and `docker start`. However, if you remove the container with `docker rm`, everything in the container is deleted, including SQL Server and your databases. The following section explains how to use **data volumes** to persist your database files even if the associated containers are deleted. > [!IMPORTANT] > For SQL Server, it is critical that you understand data persistence in Docker. In addition to the discussion in this section, see Docker's documentation on [how to manage data in Docker containers](https://docs.docker.com/engine/tutorials/dockervolumes/). ### Mount a host directory as data volume The first option is to mount a directory on your host as a data volume in your container. To do that, use the `docker run` command with the `-v :/var/opt/mssql` flag. This allows the data to be restored between container executions. ::: moniker range="= sql-server-linux-2017 || = sql-server-2017" ::: zone pivot="cs1-bash" ```bash docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1433:1433 -v /data:/var/opt/mssql/data -v /log:/var/opt/mssql/log -v /secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v /data:/var/opt/mssql/data -v /log:/var/opt/mssql/log -v /secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v /data:/var/opt/mssql/data -v /log:/var/opt/mssql/log -v /secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: moniker-end ::: moniker range=">= sql-server-linux-ver15 || >= sql-server-ver15 || =sqlallproducts-allversions" ::: zone pivot="cs1-bash" ```bash docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1433:1433 -v /data:/var/opt/mssql/data -v /log:/var/opt/mssql/log -v /secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v /data:/var/opt/mssql/data -v /log:/var/opt/mssql/log -v /secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v /data:/var/opt/mssql/data -v /log:/var/opt/mssql/log -v /secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: moniker-end This technique also enables you to share and view the files on the host outside of Docker. > [!IMPORTANT] > Host volume mapping for **Docker on Windows** does not currently support mapping the complete `/var/opt/mssql` directory. However, you can map a subdirectory, such as `/var/opt/mssql/data` to your host machine. > > Host volume mapping for **Docker on Mac** with the SQL Server on Linux image is not supported at this time. Use data volume containers instead. This restriction is specific to the `/var/opt/mssql` directory. Reading from a mounted directory works fine. For example, you can mount a host directory using -v on Mac and restore a backup from a .bak file that resides on the host. ### Use data volume containers The second option is to use a data volume container. You can create a data volume container by specifying a volume name instead of a host directory with the `-v` parameter. The following example creates a shared data volume named **sqlvolume**. ::: moniker range="= sql-server-linux-2017 || = sql-server-2017" ::: zone pivot="cs1-bash" ```bash docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: moniker-end ::: moniker range=">= sql-server-linux-ver15 || >= sql-server-ver15 || =sqlallproducts-allversions" ::: zone pivot="cs1-bash" ```bash docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1433:1433 -v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: moniker-end > [!NOTE] > This technique for implicitly creating a data volume in the run command does not work with older versions of Docker. In that case, use the explicit steps outlined in the Docker documentation, [Creating and mounting a data volume container](https://docs.docker.com/engine/tutorials/dockervolumes/#creating-and-mounting-a-data-volume-container). Even if you stop and remove this container, the data volume persists. You can view it with the `docker volume ls` command. ```command docker volume ls ``` If you then create another container with the same volume name, the new container uses the same SQL Server data contained in the volume. To remove a data volume container, use the `docker volume rm` command. > [!WARNING] > If you delete the data volume container, any SQL Server data in the container is *permanently* deleted. ### Backup and restore In addition to these container techniques, you can also use standard SQL Server backup and restore techniques. You can use backup files to protect your data or to move the data to another SQL Server instance. For more information, see [Backup and restore SQL Server databases on Linux](sql-server-linux-backup-and-restore-database.md). > [!WARNING] > If you do create backups, make sure to create or copy the backup files outside of the container. Otherwise, if the container is removed, the backup files are also deleted. ## Copy files from a container To copy a file out of the container, use the following command: ```command docker cp : ``` You can get the Container ID by running the command `docker ps -a`. **Example:** ::: zone pivot="cs1-bash" ```bash docker cp d6b75213ef80:/var/opt/mssql/log/errorlog /tmp/errorlog ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker cp d6b75213ef80:/var/opt/mssql/log/errorlog C:\Temp\errorlog ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker cp d6b75213ef80:/var/opt/mssql/log/errorlog C:\Temp\errorlog ``` ::: zone-end ## Copy files into a container To copy a file into the container, use the following command: ```command docker cp : ``` **Example:** ::: zone pivot="cs1-bash" ```bash docker cp /tmp/mydb.mdf d6b75213ef80:/var/opt/mssql/data ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker cp C:\Temp\mydb.mdf d6b75213ef80:/var/opt/mssql/data ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker cp C:\Temp\mydb.mdf d6b75213ef80:/var/opt/mssql/data ``` ::: zone-end ## Configure the timezone To run SQL Server in a Linux container with a specific timezone, configure the `TZ` environment variable. To find the right timezone value, run the `tzselect` command from a Linux bash prompt: ```command tzselect ``` After selecting the timezone, `tzselect` displays output similar to the following: ```output The following information has been given: United States Pacific Therefore TZ='America/Los_Angeles' will be used. ``` You can use this information to set the same environment variable in your Linux container. The following example shows how to run SQL Server in a container in the `Americas/Los_Angeles` timezone: ::: moniker range="= sql-server-linux-2017 || = sql-server-2017" ::: zone pivot="cs1-bash" ```bash sudo docker run -e 'ACCEPT_EULA=Y' -e 'A_PASSWORD=' \ -p 1433:1433 --name sql1 \ -e 'TZ=America/Los_Angeles'\ -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell sudo docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=" ` -p 1433:1433 --name sql1 ` -e "TZ=America/Los_Angeles" ` -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd sudo docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=" ` -p 1433:1433 --name sql1 ^ -e "TZ=America/Los_Angeles" ^ -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: moniker-end ::: moniker range=">= sql-server-linux-ver15 || >= sql-server-ver15 || =sqlallproducts-allversions" ::: zone pivot="cs1-bash" ```bash sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' \ -p 1433:1433 --name sql1 \ -e 'TZ=America/Los_Angeles'\ -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell sudo docker run -e 'ACCEPT_EULA=Y' -e "MSSQL_SA_PASSWORD=" ` -p 1433:1433 --name sql1 ` -e "TZ=America/Los_Angeles" ` -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd sudo docker run -e 'ACCEPT_EULA=Y' -e "MSSQL_SA_PASSWORD=" ` -p 1433:1433 --name sql1 ` -e "TZ=America/Los_Angeles" ` -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: moniker-end ## Change the default file location Add the `MSSQL_DATA_DIR` variable to change your data directory in your `docker run` command, then mount a volume to that location that your container’s user has access to. ::: moniker range="= sql-server-linux-2017 || = sql-server-2017" ::: zone pivot="cs1-bash" ```bash docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStrongPassword' -e 'MSSQL_DATA_DIR=/my/file/path' -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=MyStrongPassword" -e "MSSQL_DATA_DIR=/my/file/path" -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" -e "MSSQL_DATA_DIR=/my/file/path" -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest ``` ::: zone-end ::: moniker-end ::: moniker range=">= sql-server-linux-ver15 || >= sql-server-ver15 || =sqlallproducts-allversions" ::: zone pivot="cs1-bash" ```bash docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStrongPassword' -e 'MSSQL_DATA_DIR=/my/file/path' -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-powershell" ```PowerShell docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=MyStrongPassword" -e "MSSQL_DATA_DIR=/my/file/path" -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: zone pivot="cs1-cmd" ```cmd docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=MyStrongPassword" -e "MSSQL_DATA_DIR=/my/file/path" -v /my/host/path:/my/file/path -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest ``` ::: zone-end ::: moniker-end ## Next steps ::: moniker range="= sql-server-linux-2017 || = sql-server-2017" - Get started with SQL Server 2017 container images on Docker by going through the [quickstart](quickstart-install-connect-docker.md?view=sql-server-2017) ::: 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](quickstart-install-connect-docker.md?view=sql-server-ver15) ::: moniker-end - [Deploy and connect to SQL Server Docker containers](sql-server-linux-docker-container-deployment.md) - [Troubleshooting SQL Server Docker containers](sql-server-linux-docker-container-troubleshooting.md) - [Secure SQL Server Docker containers](sql-server-linux-docker-container-security.md)