# Run Microsoft SQL Server Containerized in Docker

## Installing Docker

1. Log into the Linux host and run the following commands in a terminal window
   # install prerequisites
   sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y
   # add docker gpg key
   curl -fsSL https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release)/gpg | sudo apt-key add -
   # add docker software repository
   sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release) $(lsb_release -cs) stable"
   # install docker
   sudo apt install docker-ce docker-compose containerd.io -y
   # enable and start docker service
   sudo systemctl enable docker && sudo systemctl start docker
   # add the current user to the docker group
   sudo usermod -aG docker $USER
   # reauthenticate for the new group membership to take effect
   su - $USER

## Running MS SQL Server Container

1. Continue with the following commands in a terminal window
   # create working directory structure
   mkdir ~/docker/mssql -p
   # set owner of working directories
   sudo chown "$USER":"$USER" ~/docker -R
   # allow the container to write to working directories
   sudo chmod a+rwx -R ~/docker/mssql
   # run the sql server docker container with persistent data
   docker run -d --name mssql -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD='Something$ecur3!' -v ~/docker/mssql:/var/opt/mssql -p 1433:1433 --restart unless-stopped mcr.microsoft.com/mssql/server
2. At this point the SQL Server instance is running and can be interacted with via the sqlcmd command line interface
   # connect to the database via sqlcmd
   # authenticate with the sa password set in the docker run command
   docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -U SA
   # output the SQL version
   select @@version;
   go

## Installing SQL Server Management Studio (optional)

NOTE: SQL Server Management Studio (SSMS) is currently only available for Windows hosts

1. Log into a Microsoft Windows host
2. Download SQL Server Management Studio (SSMS) Download
3. Navigate to the download directory and execute the downloaded SSMS Setup installer > Click Install
4. Once the installation completes, click Close
5. Launch SSMS from the Start menu
6. Complete the Connect to Server form with the following

   Server type: Database Engine
   Server name: <%Docker host DNS or IP%>
   Authentication: SQL Server Authentication
   Login: sa
   Password: <%MSSQL_SA_PASSWORD%>

Documentation: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-docker-container-deployment