{ "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python", "version": "3.6.6", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" } }, "nbformat_minor": 2, "nbformat": 4, "cells": [ { "cell_type": "markdown", "source": "![Microsoft](https://raw.githubusercontent.com/microsoft/azuredatastudio/master/src/sql/media/microsoft-small-logo.png)\r\n## Run SQL Server 2017 container images with Docker\r\nThis notebook will use Docker to pull and run the SQL Server 2017 container image and connect to it in Azure Data Studio\r\n\r\n### Dependencies\r\n- Docker Engine. For more information, see [Install Docker](https://docs.docker.com/engine/installation/).", "metadata": {} }, { "cell_type": "markdown", "source": "### Check dependencies", "metadata": {} }, { "cell_type": "code", "source": "def run_command():\r\n print(\"Executing: \" + cmd)\r\n !{cmd}\r\n if _exit_code != 0:\r\n raise SystemExit(f'Shell command:\\n\\n\\t{cmd}\\n\\nreturned non-zero exit code: ' + str(_exit_code) + '.\\n')\r\n print(f'Successfully executed: {cmd}')\r\n\r\ncmd = 'docker version'\r\nrun_command()", "metadata": {}, "outputs": [], "execution_count": 1 }, { "cell_type": "markdown", "source": "### List existing containers\r\nYou can view the ports that have been used by existing containers", "metadata": {} }, { "cell_type": "code", "source": "cmd = f'docker ps -a'\r\nrun_command()", "metadata": {}, "outputs": [], "execution_count": 2 }, { "cell_type": "markdown", "source": "### Required information", "metadata": {} }, { "cell_type": "code", "source": "import getpass\r\npassword_name = 'SQL Server sa account password'\r\nsql_password = getpass.getpass(prompt = password_name)\r\npassword_confirm = getpass.getpass(prompt = f'Confirm {password_name}')\r\nif sql_password != password_confirm:\r\n raise SystemExit(f'{password_name} does not match the confirmation password')\r\nprint(f'{password_name}: ******')\r\nsql_port = input('SQL Server port, default value is 1433')\r\nif len(sql_port) == 0:\r\n sql_port = '1433'\r\nprint(f'Port: {sql_port}')", "metadata": {}, "outputs": [], "execution_count": 3 }, { "cell_type": "markdown", "source": "### Pull the container image", "metadata": {} }, { "cell_type": "code", "source": "cmd = f'docker pull mcr.microsoft.com/mssql/server:2017-latest'\r\nrun_command()", "metadata": {}, "outputs": [], "execution_count": 4 }, { "cell_type": "markdown", "source": "### Start a new container", "metadata": {} }, { "cell_type": "code", "source": "import time\r\ncontainer_name = 'sql2017-' + time.strftime(\"%Y%m%d%H%M%S\", time.localtime())\r\nprint('New container name: ' + container_name)\r\ncmd = f'docker run -e ACCEPT_EULA=Y -e \"SA_PASSWORD={sql_password}\" -p {sql_port}:1433 --name {container_name} -d mcr.microsoft.com/mssql/server:2017-latest'\r\nrun_command()", "metadata": {}, "outputs": [], "execution_count": 5 }, { "cell_type": "markdown", "source": "### List all the containers", "metadata": {} }, { "cell_type": "code", "source": "cmd = f'docker ps -a'\r\nrun_command()", "metadata": {}, "outputs": [], "execution_count": 6 }, { "cell_type": "markdown", "source": "### Connect to SQL Server in Azure Data Studio\r\nIt might take a couple minutes for SQL Server to launch", "metadata": {} }, { "cell_type": "code", "source": "from IPython.display import *\r\ndisplay(HTML(\"

Click here to connect to SQL Server

\"))", "metadata": {}, "outputs": [], "execution_count": 7 }, { "cell_type": "markdown", "source": "### Stop and remove the container", "metadata": {} }, { "cell_type": "code", "source": "stop_container_command = f'docker stop {container_name}'\r\nremove_container_command = f'docker rm {container_name}'\r\ndisplay(HTML(\"Use this link to: open the terminal window in Azure Data Studio and use the links below to paste the command to the terminal.\"))\r\ndisplay(HTML(\"Stop the container: \" + stop_container_command + \"\"))\r\ndisplay(HTML(\"Remove the container: \" + remove_container_command + \"\"))", "metadata": {}, "outputs": [], "execution_count": 8 } ] }