|
| 1 | +--- |
| 2 | +title: Run SQL Server Machine Learning Services in a Container | Microsoft Docs |
| 3 | +description: This tutorial show how to use SQL Server Machine Learning Services in a Linux container running on Docker. |
| 4 | +author: uc-msft |
| 5 | +ms.author: umajay |
| 6 | +manager: craigg |
| 7 | +ms.date: 06/26/2019 |
| 8 | +ms.topic: conceptual |
| 9 | +ms.prod: sql |
| 10 | +ms.technology: linux |
| 11 | +ms.collection: linux-container |
| 12 | +moniker: ">= sql-server-linux-ver15 || =sqlallproducts-allversions" |
| 13 | +--- |
| 14 | +# Run SQL Server Machine Learning Services in a Container |
| 15 | + |
| 16 | +[!INCLUDE[appliesto-ss-xxxx-xxxx-xxx-md-linuxonly](../includes/appliesto-ss-xxxx-xxxx-xxx-md-linuxonly.md)] |
| 17 | + |
| 18 | +This tutorial demonstrates how to build a Docker container with SQL Server Machine Learning Services & run Machine Learning Scripts from Transact-SQL. |
| 19 | + |
| 20 | +> [!div class="checklist"] |
| 21 | +> * Clone the mssql-docker repository. |
| 22 | +> * Build SQL Server Linux container image with Machine Learning Services. |
| 23 | +> * Run SQL Server Linux container image with Machine Learning Services. |
| 24 | +> * Run R or Python scripts using Transact-SQL statements. |
| 25 | +> * Stop and remove the SQL Server Linux container. |
| 26 | +
|
| 27 | +## Pre-requisites |
| 28 | + |
| 29 | +* Git command-line interface. |
| 30 | +* Docker Engine 1.8+ on any supported Linux distribution or Docker for Mac/Windows. For more information, see [Install Docker](https://docs.docker.com/engine/installation/). |
| 31 | +* Minimum of 2 GB of disk space |
| 32 | +* Minimum of 2 GB of RAM |
| 33 | +* [System requirements for SQL Server on Linux](sql-server-linux-setup.md#system). |
| 34 | + |
| 35 | +## Clone the mssql-docker repository |
| 36 | + |
| 37 | +1. Open a bash terminal on Linux/Mac or WSL terminal on Windows. |
| 38 | + |
| 39 | +1. Create a local directory to hold a copy of the mssql-docker repository locally. |
| 40 | +1. Run the git clone command to clone the mssql-docker repository. |
| 41 | + |
| 42 | + ```bash |
| 43 | + git clone https://github.com/microsoft/mssql-docker mssql-docker |
| 44 | + ``` |
| 45 | + |
| 46 | +## Build SQL Server Linux container image with Machine Learning Services |
| 47 | + |
| 48 | +1. Change directory to the mssql-mlservices directory. |
| 49 | + |
| 50 | + ```bash |
| 51 | + cd mssql-docker/linux/preview/examples/mssql-mlservices |
| 52 | + ``` |
| 53 | + |
| 54 | +1. Execute build.sh script. |
| 55 | + |
| 56 | + ```bash |
| 57 | + ./build.sh |
| 58 | + ``` |
| 59 | + |
| 60 | + > [!NOTE] |
| 61 | + > Building the docker image requires installing packages that are several GBs in size. The script may take up to 20 minutes to complete depending on network bandwidth. |
| 62 | + |
| 63 | +## Run SQL Server Linux container image with Machine Learning Services |
| 64 | + |
| 65 | +1. Set environment variables before running the container. Set PATH_TO_MSSQL environment variable to a host directory. |
| 66 | + |
| 67 | + ```bash |
| 68 | + export MSSQL_PID='Developer' |
| 69 | + export ACCEPT_EULA='Y' |
| 70 | + export ACCEPT_EULA_ML='Y' |
| 71 | + export PATH_TO_MSSQL='/home/mssql/' |
| 72 | + ``` |
| 73 | + |
| 74 | +1. Execute run.sh script. |
| 75 | + |
| 76 | + ```bash |
| 77 | + ./run.sh |
| 78 | + ``` |
| 79 | + |
| 80 | + This command creates a SQL Server container with Machine Learning Services with the Developer edition (default). SQL Server port **1433** is exposed on the host as port **1401**. |
| 81 | + |
| 82 | + > [!NOTE] |
| 83 | + > The process for running production SQL Server editions in containers is slightly different. For more information, see [Run production container images](sql-server-linux-configure-docker.md#production). If you use the same container names and ports, the rest of this walk-through still works with production containers. |
| 84 | + |
| 85 | +1. To view your Docker containers, use the `docker ps` command. |
| 86 | + |
| 87 | + ```bash |
| 88 | + sudo docker ps -a |
| 89 | + ``` |
| 90 | + |
| 91 | +1. If the **STATUS** column shows a status of **Up**, then SQL Server is running in the container and listening on the port specified in the **PORTS** column. If the **STATUS** column for your SQL Server container shows **Exited**, see the [Troubleshooting section of the configuration guide](sql-server-linux-configure-docker.md#troubleshooting). |
| 92 | + |
| 93 | + ```bash |
| 94 | + $ sudo docker ps -a |
| 95 | + ``` |
| 96 | + |
| 97 | +Output: |
| 98 | + |
| 99 | + ``` |
| 100 | + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 101 | + 941e1bdf8e1d mcr.microsoft.com/mssql/server/mssql-server-linux "/bin/sh -c /opt/m..." About an hour ago Up About an hour 0.0.0.0:1401->1433/tcp sql1 |
| 102 | + ``` |
| 103 | + |
| 104 | +## Change the SA password |
| 105 | + |
| 106 | +[!INCLUDE [Change docker password](../includes/sql-server-linux-change-docker-password.md)] |
| 107 | + |
| 108 | +## Execute R / Python scripts from Transact-SQL |
| 109 | + |
| 110 | +1. Connect to SQL Server in the container and enable the external script configuration option by running the following T-SQL statement. |
| 111 | + |
| 112 | + ```sql |
| 113 | + EXEC sp_configure 'external scripts enabled', 1 |
| 114 | + RECONFIGURE WITH OVERRIDE |
| 115 | + go |
| 116 | + ``` |
| 117 | + |
| 118 | +1. Verify Machine Learning Services is working by running the following simple R/Python sp_execute_external_script. |
| 119 | + |
| 120 | + ```sql |
| 121 | + execute sp_execute_external_script |
| 122 | + @language = N'R', |
| 123 | + @script = N' |
| 124 | + print("Hello World!") |
| 125 | + print(R.version) |
| 126 | + print(Revo.version) |
| 127 | + OutputDataSet <- InputDataSet', |
| 128 | + @input_data_1 = N'select 1' |
| 129 | + with result sets((i int)); |
| 130 | + go |
| 131 | + ``` |
| 132 | + |
| 133 | + ```sql |
| 134 | + execute sp_execute_external_script |
| 135 | + @language = N'Python', |
| 136 | + @script = N' |
| 137 | + import sys |
| 138 | + print(sys.version) |
| 139 | + print("Hello World!") |
| 140 | + OutputDataSet = InputDataSet', |
| 141 | + @input_data_1 = N'select 1' |
| 142 | + with result sets((i int)); |
| 143 | + go |
| 144 | + ``` |
| 145 | + |
| 146 | +## Next steps |
| 147 | + |
| 148 | +In this tutorial, you learned to do the following: |
| 149 | + |
| 150 | +> [!div class="checklist"] |
| 151 | +> * Clone the mssql-docker repository. |
| 152 | +> * Build SQL Server Linux container image with Machine Learning Services. |
| 153 | +> * Run SQL Server Linux container image with Machine Learning Services. |
| 154 | +> * Run R or Python scripts using Transact-SQL statements. |
| 155 | +> * Stop and remove the SQL Server Linux container. |
| 156 | + |
| 157 | +Next, review other Docker configuration and troubleshooting scenarios: |
| 158 | + |
| 159 | +> [!div class="nextstepaction"] |
| 160 | +>[Configuration guide for SQL Server on Docker](sql-server-linux-configure-docker.md) |
0 commit comments