Skip to content

Latest commit

 

History

History
1209 lines (768 loc) · 54.5 KB

quickstart-install-connect-docker.md

File metadata and controls

1209 lines (768 loc) · 54.5 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic ms.custom zone_pivot_groups monikerRange
Docker: Install containers for SQL Server on Linux
This quickstart shows how to use Docker to run the SQL Server Linux container images. You connect to a database and run a query.
amitkh-msft
amitkh
vanto, randolphwest
08/20/2024
sql
linux
quickstart
intro-quickstart
kr2b-contr-experiment
linux-related-content
cs1-command-shell
>=sql-server-linux-2017 || >=sql-server-2017

Quickstart: Run SQL Server Linux container images with Docker

[!INCLUDE SQL Server - Linux]

::: moniker range="= sql-server-linux-2017 || = sql-server-2017"

In this quickstart, you use Docker to pull and run the [!INCLUDE sssql17-md] Linux container image, mssql-server-linux. Then you can connect with sqlcmd to create your first database and run queries.

For more information on supported platforms, see Release notes for SQL Server 2017 on Linux.

Warning

When you stop and remove a container, your [!INCLUDE ssnoversion-md] data in the container is permanently deleted. For more information on preserving your data, create and copy a backup file out of the container or use a container data persistence technique.

This quickstart creates [!INCLUDE sssql17-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the versions of this article for [[!INCLUDE sssql19-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver15&preserve-view=true#pullandrun2019) or [[!INCLUDE sssql22-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver16&preserve-view=true#pullandrun2022) versions of this article.

::: moniker-end

::: moniker range="= sql-server-linux-ver15 || = sql-server-ver15"

In this quickstart, you use Docker to pull and run the [!INCLUDE sssql19-md] Linux container image, mssql-server-linux. Then you can connect with sqlcmd to create your first database and run queries.

For more information on supported platforms, see Release notes for SQL Server 2019 on Linux.

Warning

When you stop and remove a container, your [!INCLUDE ssnoversion-md] data in the container is permanently deleted. For more information on preserving your data, create and copy a backup file out of the container or use a container data persistence technique.

This quickstart creates [!INCLUDE sssql19-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql17-md]](quickstart-install-connect-docker.md?view=sql-server-linux-2017&preserve-view=true#pullandrun2017) or [[!INCLUDE sssql22-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver16&preserve-view=true#pullandrun2022) versions of this article.

::: moniker-end

::: moniker range=">= sql-server-linux-ver16 || >= sql-server-ver16"

In this quickstart, you use Docker to pull and run the [!INCLUDE sssql22-md] Linux container image, mssql-server-linux. Then you can connect with sqlcmd to create your first database and run queries.

For more information on supported platforms, see Release notes for SQL Server 2022 on Linux.

Warning

When you stop and remove a container, your [!INCLUDE ssnoversion-md] data in the container is permanently deleted. For more information on preserving your data, create and copy a backup file out of the container or use a container data persistence technique.

This quickstart creates [!INCLUDE sssql22-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql17-md]](quickstart-install-connect-docker.md?view=sql-server-linux-2017&preserve-view=true#pullandrun2017) or [[!INCLUDE sssql19-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver15&preserve-view=true#pullandrun2019) versions of this article.

::: moniker-end

This image consists of [!INCLUDE ssnoversion-md] running on Linux based on Ubuntu. It can be used with the Docker Engine 1.8+ on Linux.

Starting with [!INCLUDE sssql22-md] CU 14 and [!INCLUDE sssql19-md] CU 28, the container images include the new mssql-tools18 package. The previous directory /opt/mssql-tools/bin is being phased out. The new directory for Microsoft ODBC 18 tools is /opt/mssql-tools18/bin, aligning with the latest tools offering. For more information about changes and security enhancements, see ODBC Driver 18.0 for SQL Server Released.

The examples in this article use the docker command. However, most of these commands also work with Podman. Podman provides a command-line interface similar to the Docker Engine. You can find out more about Podman.

Important

sqlcmd doesn't currently support the MSSQL_PID parameter when creating containers. If you use the sqlcmd instructions in this quickstart, you create a container with the Developer edition of [!INCLUDE ssnoversion-md]. Use the command line interface (CLI) instructions to create a container using the license of your choice. For more information, see Deploy and connect to SQL Server Linux containers.

Prerequisites

  • Docker Engine 1.8+ on any supported Linux distribution. For more information, see Install Docker.

::: moniker range="= sql-server-linux-2017 || = sql-server-2017"

::: moniker-end

::: moniker range="= sql-server-linux-ver15 || = sql-server-ver15"

::: moniker-end

::: moniker range=">= sql-server-linux-ver16 || >= sql-server-ver16"

::: moniker-end

::: moniker range="= sql-server-linux-2017 || = sql-server-2017"

Pull and run the SQL Server Linux container image

Before starting the following steps, make sure that you select your preferred shell (bash, PowerShell, or cmd) at the top of this article.

::: zone pivot="cs1-bash" For the bash commands in this article, sudo is used. If you don't want to use sudo to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux. ::: zone-end

Pull the container from the registry

Pull the [!INCLUDE sssql17-md] Linux container image from the Microsoft Container Registry.

::: zone pivot="cs1-bash"

sudo docker pull mcr.microsoft.com/mssql/server:2017-latest

::: zone-end

::: zone pivot="cs1-powershell"

docker pull mcr.microsoft.com/mssql/server:2017-latest

::: zone-end

::: zone pivot="cs1-cmd"

docker pull mcr.microsoft.com/mssql/server:2017-latest

::: zone-end

This quickstart creates [!INCLUDE sssql17-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql19-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver15&preserve-view=true#pullandrun2019) or [[!INCLUDE sssql22-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver16&preserve-view=true#pullandrun2022) versions of this article.

The previous command pulls the latest [!INCLUDE sssql17-md] Linux container image. If you want to pull a specific image, you add a colon and the tag name, such as mcr.microsoft.com/mssql/server:2017-GA-ubuntu. To see all available images, see the Microsoft Artifact Registry.

Run the container

To run the Linux container image with Docker, you can use the following command from a bash shell or elevated PowerShell command prompt.

Important

The SA_PASSWORD environment variable is deprecated. Use MSSQL_SA_PASSWORD instead.

::: zone pivot="cs1-bash"

sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2017-latest

::: zone-end

::: zone pivot="cs1-powershell"

If you're using PowerShell Core, replace the double quotes with single quotes.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" `
   -p 1433:1433 --name sql1 --hostname sql1 `
   -d `
   mcr.microsoft.com/mssql/server:2017-latest

::: zone-end

::: zone pivot="cs1-cmd"

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" `
   -p 1433:1433 --name sql1 --hostname sql1 `
   -d `
   mcr.microsoft.com/mssql/server:2017-latest

::: zone-end

Your password should follow the [!INCLUDE ssnoversion-md] default password policy, otherwise the container can't set up [!INCLUDE ssnoversion-md], and stops working. By default, the password must be at least eight characters long and contain characters from three of the following four sets: uppercase letters, lowercase letters, base-10 digits, and symbols. You can examine the error log by using the docker logs command.

By default, this quickstart creates a container with the Developer edition of [!INCLUDE ssnoversion-md]. The process for running production editions in containers is slightly different. For more information, see Run production container images.

The following table provides a description of the parameters in the previous docker run example:

Parameter Description
-e "ACCEPT_EULA=Y" Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the [!INCLUDE ssnoversion-md] image.
-e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" Specify your own strong password that is at least eight characters and meets the Password Policy. Required setting for the [!INCLUDE ssnoversion-md] image.
-e "MSSQL_COLLATION=<SQL_Server_collation>" Specify a custom [!INCLUDE ssnoversion-md] collation, instead of the default SQL_Latin1_General_CP1_CI_AS.
-p 1433:1433 Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, [!INCLUDE ssnoversion-md] is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you can't reuse this same name.
--hostname sql1 Used to explicitly set the container hostname. If you don't specify the hostname, it defaults to the container ID, which is a randomly generated system GUID.
-d Run the container in the background (daemon).
mcr.microsoft.com/mssql/server:2017-latest The [!INCLUDE ssnoversion-md] Linux container image.

Pull and run the container

Pull and run the [!INCLUDE sssql17-md] Linux container image from the Microsoft Container Registry.

::: zone pivot="cs1-bash"

sudo sqlcmd create mssql --tag 2017-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd create mssql --tag 2017-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd create mssql --tag 2017-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

This quickstart creates [!INCLUDE sssql17-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql19-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver15&preserve-view=true#pullandrun2019) or [[!INCLUDE sssql22-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver16&preserve-view=true#pullandrun2022) versions of this article.

The previous command uses the latest [!INCLUDE sssql17-md] Linux container image. If you want to pull a specific image, change the tag name, such as 2017-GA-ubuntu. To see all available images, run the following command:

::: zone pivot="cs1-bash"

sudo sqlcmd create mssql get-tags

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd create mssql get-tags

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd create mssql get-tags

::: zone-end

The following table provides a description of the parameters in the previous sqlcmd create mssql example:

Parameter Description
--ACCEPT-EULA Include the ACCEPT-EULA flag to confirm your acceptance of the End-User Licensing Agreement. Required setting for the [!INCLUDE ssnoversion-md] image.
--port 1433 Map a TCP port on the host environment and a TCP port in the container. In this example, [!INCLUDE ssnoversion-md] is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you can't reuse this same name.
--hostname sql1 Used to explicitly set the container hostname. If you don't specify the hostname, it defaults to the container ID, which is a randomly generated system GUID.
--tag 2017-latest The [!INCLUDE ssnoversion-md] Linux container image.

View list of containers

  1. To view your Docker containers, use the docker ps command.

    ::: zone pivot="cs1-bash"

    sudo docker ps -a

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    docker ps -a

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    docker ps -a

    ::: zone-end

    You should see output similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMES
    d4a1999ef83e   mcr.microsoft.com/mssql/server:2017-latest   "/opt/mssql/bin/perm..."   2 minutes ago   Up 2 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   sql1
    
  2. If the STATUS column shows a status of Up, then [!INCLUDE ssnoversion-md] is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your [!INCLUDE ssnoversion-md] container shows Exited, see Troubleshoot SQL Server Docker containers. The server is ready for connections once the [!INCLUDE ssnoversion-md] error logs display the message: SQL Server is now ready for client connections. This is an informational message; no user action is required. You can review the [!INCLUDE ssnoversion-md] error log inside the container using the command:

    sudo docker exec -t sql1 cat /var/opt/mssql/log/errorlog | grep connection

    The --hostname parameter, as discussed previously, changes the internal name of the container to a custom value. This value is the name you see returned in the following Transact-SQL query:

    SELECT @@SERVERNAME,
        SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),
        SERVERPROPERTY('MachineName'),
        SERVERPROPERTY('ServerName');

    Setting --hostname and --name to the same value is a good way to easily identify the target container.

  3. As a final step, change your SA password in a production environment, because the MSSQL_SA_PASSWORD is visible in ps -eax output and stored in the environment variable of the same name.

::: moniker-end

::: moniker range="= sql-server-linux-ver15 || = sql-server-ver15"

Pull and run the SQL Server Linux container image

Before starting the following steps, make sure that you select your preferred shell (bash, PowerShell, or cmd) at the top of this article.

::: zone pivot="cs1-bash" For the bash commands in this article, sudo is used. If you don't want to use sudo to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux. ::: zone-end

Pull the container from the registry

Pull the [!INCLUDE sssql19-md] Linux container image from the Microsoft Container Registry.

::: zone pivot="cs1-bash"

docker pull mcr.microsoft.com/mssql/server:2019-latest

::: zone-end

::: zone pivot="cs1-powershell"

docker pull mcr.microsoft.com/mssql/server:2019-latest

::: zone-end

::: zone pivot="cs1-cmd"

docker pull mcr.microsoft.com/mssql/server:2019-latest

::: zone-end

This quickstart creates [!INCLUDE sssql19-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql17-md]](quickstart-install-connect-docker.md?view=sql-server-linux-2017&preserve-view=true#pullandrun2017) or [[!INCLUDE sssql22-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver16&preserve-view=true#pullandrun2022) versions of this article.

The previous command pulls the latest [!INCLUDE sssql19-md] Linux container image. If you want to pull a specific image, you add a colon and the tag name, such as mcr.microsoft.com/mssql/server:2019-GA-ubuntu. To see all available images, see the Microsoft Artifact Registry.

Run the container

To run the Linux container image with Docker, you can use the following command from a bash shell or elevated PowerShell command prompt.

Important

The SA_PASSWORD environment variable is deprecated. Use MSSQL_SA_PASSWORD instead.

::: zone pivot="cs1-bash"

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2019-latest

::: zone-end

::: zone pivot="cs1-powershell"

If you're using PowerShell Core, replace the double quotes with single quotes.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" `
   -p 1433:1433 --name sql1 --hostname sql1 `
   -d `
   mcr.microsoft.com/mssql/server:2019-latest

::: zone-end

::: zone pivot="cs1-cmd"

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" `
   -p 1433:1433 --name sql1 --hostname sql1 `
   -d `
   mcr.microsoft.com/mssql/server:2019-latest

::: zone-end

Your password should follow the [!INCLUDE ssnoversion-md] default password policy, otherwise the container can't set up [!INCLUDE ssnoversion-md], and stops working. By default, the password must be at least eight characters long and contain characters from three of the following four sets: uppercase letters, lowercase letters, base-10 digits, and symbols. You can examine the error log by using the docker logs command.

By default, this quickstart creates a container with the Developer edition of [!INCLUDE ssnoversion-md]. The process for running production editions in containers is slightly different. For more information, see Run production container images.

The following table provides a description of the parameters in the previous docker run example:

Parameter Description
-e "ACCEPT_EULA=Y" Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the [!INCLUDE ssnoversion-md] image.
-e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" Specify your own strong password that is at least eight characters and meets the Password Policy. Required setting for the [!INCLUDE ssnoversion-md] image.
-e "MSSQL_COLLATION=<SQL_Server_collation>" Specify a custom [!INCLUDE ssnoversion-md] collation, instead of the default SQL_Latin1_General_CP1_CI_AS.
-p 1433:1433 Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, [!INCLUDE ssnoversion-md] is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you can't reuse this same name.
--hostname sql1 Used to explicitly set the container hostname. If you don't specify the hostname, it defaults to the container ID, which is a randomly generated system GUID.
-d Run the container in the background (daemon).
mcr.microsoft.com/mssql/server:2019-latest The [!INCLUDE ssnoversion-md] Linux container image.

Pull and run the container

Pull and run the [!INCLUDE sssql19-md] Linux container image from the Microsoft Container Registry.

::: zone pivot="cs1-bash"

sudo sqlcmd create mssql --tag 2019-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd create mssql --tag 2019-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd create mssql --tag 2019-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

This quickstart creates [!INCLUDE sssql19-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql17-md]](quickstart-install-connect-docker.md?view=sql-server-linux-2017&preserve-view=true#pullandrun2017) or [[!INCLUDE sssql22-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver16&preserve-view=true#pullandrun2022) versions of this article.

The previous command pulls the latest [!INCLUDE sssql19-md] Linux container image. If you want to pull a specific image, change the tag name, such as 2019-GA-ubuntu-16.04. To see all available images, run the following command:

::: zone pivot="cs1-bash"

sudo sqlcmd create mssql get-tags

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd create mssql get-tags

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd create mssql get-tags

::: zone-end

By default, this quickstart creates a container with the Developer edition of [!INCLUDE ssnoversion-md]. The process for running production editions in containers is slightly different. For more information, see Run production container images.

The following table provides a description of the parameters in the previous docker run example:

Parameter Description
--ACCEPT_EULA Include the ACCEPT_EULA flag to confirm your acceptance of the End-User Licensing Agreement. Required setting for the [!INCLUDE ssnoversion-md] image.
--port 1433 Map a TCP port on the host environment and a TCP port in the container. In this example, [!INCLUDE ssnoversion-md] is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you can't reuse this same name.
--hostname sql1 Used to explicitly set the container hostname. If you don't specify the hostname, it defaults to the container ID, which is a randomly generated system GUID.
--tag 2019-latest The [!INCLUDE ssnoversion-md] Linux container image.

View list of containers

  1. To view your Docker containers, use the docker ps command.

    ::: zone pivot="cs1-bash"

    docker ps -a

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    docker ps -a

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    docker ps -a

    ::: zone-end

    You should see output similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMES
    d4a1999ef83e   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm..."   2 minutes ago   Up 2 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   sql1
    
  2. If the STATUS column shows a status of Up, then [!INCLUDE ssnoversion-md] is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your [!INCLUDE ssnoversion-md] container shows Exited, see Troubleshoot SQL Server Docker containers. The server is ready for connections once the [!INCLUDE ssnoversion-md] error logs display the message: SQL Server is now ready for client connections. This is an informational message; no user action is required. You can review the [!INCLUDE ssnoversion-md] error log inside the container using the command:

    docker exec -t sql1 cat /var/opt/mssql/log/errorlog | grep connection

    The --hostname parameter, as discussed previously, changes the internal name of the container to a custom value. This value is the name you see returned in the following Transact-SQL query:

    SELECT @@SERVERNAME,
        SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),
        SERVERPROPERTY('MachineName'),
        SERVERPROPERTY('ServerName');

    Setting --hostname and --name to the same value is a good way to easily identify the target container.

  3. As a final step, change your SA password in a production environment, because the MSSQL_SA_PASSWORD is visible in ps -eax output and stored in the environment variable of the same name.

::: moniker-end

::: moniker range=">= sql-server-linux-ver16 || >= sql-server-ver16"

Pull and run the SQL Server Linux container image

Before starting the following steps, make sure that you select your preferred shell (bash, PowerShell, or cmd) at the top of this article.

::: zone pivot="cs1-bash" For the bash commands in this article, sudo is used. If you don't want to use sudo to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux. ::: zone-end

Pull the container from the registry

Pull the [!INCLUDE sssql22-md] Linux container image from the Microsoft Container Registry.

::: zone pivot="cs1-bash"

docker pull mcr.microsoft.com/mssql/server:2022-latest

::: zone-end

::: zone pivot="cs1-powershell"

docker pull mcr.microsoft.com/mssql/server:2022-latest

::: zone-end

::: zone pivot="cs1-cmd"

docker pull mcr.microsoft.com/mssql/server:2022-latest

::: zone-end

This quickstart creates [!INCLUDE sssql22-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql17-md]](quickstart-install-connect-docker.md?view=sql-server-linux-2017&preserve-view=true#pullandrun2017) or [[!INCLUDE sssql19-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver15&preserve-view=true#pullandrun2019) versions of this article.

The previous command pulls the latest [!INCLUDE sssql22-md] Linux container image. If you want to pull a specific image, you add a colon and the tag name, such as mcr.microsoft.com/mssql/server:2022-GA-ubuntu. To see all available images, see the Microsoft Artifact Registry.

Run the container

To run the Linux container image with Docker, you can use the following command from a bash shell or elevated PowerShell command prompt.

Important

The SA_PASSWORD environment variable is deprecated. Use MSSQL_SA_PASSWORD instead.

::: zone pivot="cs1-bash"

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2022-latest

::: zone-end

::: zone pivot="cs1-powershell"

If you're using PowerShell Core, replace the double quotes with single quotes.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" `
   -p 1433:1433 --name sql1 --hostname sql1 `
   -d `
   mcr.microsoft.com/mssql/server:2022-latest

::: zone-end

::: zone pivot="cs1-cmd"

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" `
   -p 1433:1433 --name sql1 --hostname sql1 `
   -d `
   mcr.microsoft.com/mssql/server:2022-latest

::: zone-end

Your password should follow the [!INCLUDE ssnoversion-md] default password policy, otherwise the container can't set up [!INCLUDE ssnoversion-md], and stops working. By default, the password must be at least eight characters long and contain characters from three of the following four sets: uppercase letters, lowercase letters, base-10 digits, and symbols. You can examine the error log by using the docker logs command.

By default, this quickstart creates a container with the Developer edition of [!INCLUDE ssnoversion-md]. The process for running production editions in containers is slightly different. For more information, see Run production container images.

The following table provides a description of the parameters in the previous docker run example:

Parameter Description
-e "ACCEPT_EULA=Y" Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the [!INCLUDE ssnoversion-md] image.
-e "MSSQL_SA_PASSWORD=<YourStrong@Passw0rd>" Specify your own strong password that is at least eight characters and meets the Password Policy. Required setting for the [!INCLUDE ssnoversion-md] image.
-e "MSSQL_COLLATION=<SQL_Server_collation>" Specify a custom [!INCLUDE ssnoversion-md] collation, instead of the default SQL_Latin1_General_CP1_CI_AS.
-p 1433:1433 Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, [!INCLUDE ssnoversion-md] is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you can't reuse this same name.
--hostname sql1 Used to explicitly set the container hostname. If you don't specify the hostname, it defaults to the container ID, which is a randomly generated system GUID.
-d Run the container in the background (daemon).
mcr.microsoft.com/mssql/server:2022-latest The [!INCLUDE ssnoversion-md] Linux container image.

Change the system administrator password

The SA account is a system administrator on the [!INCLUDE ssnoversion-md] instance that gets created during setup. After you create your [!INCLUDE ssnoversion-md] container, the MSSQL_SA_PASSWORD environment variable you specified is discoverable by running echo $MSSQL_SA_PASSWORD in the container. For security purposes, you should change your SA password in a production environment.

  1. Choose a strong password to use for the SA user.

  2. Use docker exec to run sqlcmd to change the password using Transact-SQL. In the following example, the old and new passwords are read from user input.

    ::: zone pivot="cs1-bash"

    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd \
    -S localhost -U SA \
     -P "$(read -sp "Enter current SA password: "; echo "${REPLY}")" \
     -Q "ALTER LOGIN SA WITH PASSWORD=\"$(read -sp "Enter new SA password: "; echo "${REPLY}")\""

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd `
       -S localhost -U SA -P "<YourStrong@Passw0rd>" `
       -Q "ALTER LOGIN SA WITH PASSWORD='<YourNewStrong@Passw0rd>'"

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd `
       -S localhost -U SA -P "<YourStrong@Passw0rd>" `
       -Q "ALTER LOGIN SA WITH PASSWORD='<YourNewStrong@Passw0rd>'"

    ::: zone-end

    Recent versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the -No option to sqlcmd to specify that encryption is optional, not mandatory.

Disable the sa account as a best practice

Important

You'll need these credentials for later steps. Be sure to write down the user ID and password that you enter here.

[!INCLUDE connect-with-sa]

Pull and run the container

Pull and run the [!INCLUDE sssql22-md] Linux container image from the Microsoft Container Registry.

::: zone pivot="cs1-bash"

sudo sqlcmd create mssql --tag 2022-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd create mssql --tag 2022-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd create mssql --tag 2022-latest --hostname sql1 --name sql1 --port 1433 --accept-eula

::: zone-end

This quickstart creates [!INCLUDE sssql22-md] containers. If you prefer to create Linux containers for different versions of [!INCLUDE ssnoversion-md], see the [[!INCLUDE sssql17-md]](quickstart-install-connect-docker.md?view=sql-server-linux-2017&preserve-view=true#pullandrun2017) or [[!INCLUDE sssql19-md]](quickstart-install-connect-docker.md?view=sql-server-linux-ver15&preserve-view=true#pullandrun2019) versions of this article.

The previous command pulls the latest [!INCLUDE sssql22-md] Linux container image. If you want to pull a specific image, change the tag name, such as 2022-CU11-ubuntu-22.04. To see all available images, run the following command:

::: zone pivot="cs1-bash"

sudo sqlcmd create mssql get-tags

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd create mssql get-tags

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd create mssql get-tags

::: zone-end

By default, this quickstart creates a container with the Developer edition of [!INCLUDE ssnoversion-md]. The process for running production editions in containers is slightly different. For more information, see Run production container images.

The following table provides a description of the parameters in the previous docker run example:

Parameter Description
--ACCEPT-EULA Include the --ACCEPT-EULA flag to confirm your acceptance of the End-User Licensing Agreement. Required setting for the [!INCLUDE ssnoversion-md] image.
--port 1433 Map a TCP port on the host environment and a TCP port in the container. In this example, [!INCLUDE ssnoversion-md] is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you can't reuse this same name.
--hostname sql1 Used to explicitly set the container hostname. If you don't specify the hostname, it defaults to the container ID, which is a randomly generated system GUID.
--tag 2022-latest The [!INCLUDE ssnoversion-md] Linux container image.

sqlcmd disables the SA password and creates a new login based on the current user when it creates a container. Use the following command to view your login information. You need it in later steps.

::: zone pivot="cs1-bash"

sudo sqlcmd config view --raw

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd config view --raw

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd config view --raw

::: zone-end


View list of containers

  1. To view your Docker containers, use the docker ps command.

    ::: zone pivot="cs1-bash"

    docker ps -a

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    docker ps -a

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    docker ps -a

    ::: zone-end

    You should see output similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMES
    d4a1999ef83e   mcr.microsoft.com/mssql/server:2022-latest   "/opt/mssql/bin/perm..."   2 minutes ago   Up 2 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   sql1
    
  2. If the STATUS column shows a status of Up, then [!INCLUDE ssnoversion-md] is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your [!INCLUDE ssnoversion-md] container shows Exited, see Troubleshoot SQL Server Docker containers. The server is ready for connections once the [!INCLUDE ssnoversion-md] error logs display the message: SQL Server is now ready for client connections. This is an informational message; no user action is required. You can review the [!INCLUDE ssnoversion-md] error log inside the container using the command:

    docker exec -t sql1 cat /var/opt/mssql/log/errorlog | grep connection

    The --hostname parameter, as discussed previously, changes the internal name of the container to a custom value. This value is the name you see returned in the following Transact-SQL query:

    SELECT @@SERVERNAME,
        SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),
        SERVERPROPERTY('MachineName'),
        SERVERPROPERTY('ServerName');

    Setting --hostname and --name to the same value is a good way to easily identify the target container.

::: moniker-end

Connect to SQL Server

The following steps use the [!INCLUDE ssnoversion-md] command-line tool, sqlcmd utility, inside the container to connect to [!INCLUDE ssnoversion-md].

  1. Use the docker exec -it command to start an interactive bash shell inside your running container. In the following example, sql1 is name specified by the --name parameter when you created the container.

    ::: zone pivot="cs1-bash"

    docker exec -it sql1 "bash"

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    docker exec -it sql1 "bash"

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    docker exec -it sql1 "bash"

    ::: zone-end

::: moniker range="= sql-server-linux-2017 || = sql-server-2017"

  1. Once inside the container, connect locally with sqlcmd, using its full path.

    /opt/mssql-tools/bin/sqlcmd -S localhost -U <userid> -P "<YourNewStrong@Passw0rd>"

    Recent versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the -No option to sqlcmd to specify that encryption is optional, not mandatory.

    You can omit the password on the command-line to be prompted to enter it. For example:

    /opt/mssql-tools/bin/sqlcmd -S localhost -U <userid>

::: moniker-end

::: moniker range="= sql-server-linux-ver15 || = sql-server-ver15"

  1. Once inside the container, connect locally with sqlcmd, using its full path.

    /opt/mssql-tools18/bin/sqlcmd -S localhost -U <userid> -P "<YourNewStrong@Passw0rd>"

    Recent versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the -No option to sqlcmd to specify that encryption is optional, not mandatory.

    You can omit the password on the command-line to be prompted to enter it. For example:

    /opt/mssql-tools18/bin/sqlcmd -S localhost -U <userid>

::: moniker-end

::: moniker range="= sql-server-linux-ver16 || = sql-server-ver16"

  1. Once inside the container, connect locally with sqlcmd, using its full path.

    /opt/mssql-tools18/bin/sqlcmd -S localhost -U <userid> -P "<YourNewStrong@Passw0rd>"

    Recent versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the -No option to sqlcmd to specify that encryption is optional, not mandatory.

    You can omit the password on the command-line to be prompted to enter it. For example:

    /opt/mssql-tools18/bin/sqlcmd -S localhost -U <userid>

::: moniker-end

  1. If successful, you should get to a sqlcmd command prompt: 1>.

Create and query data

The following sections walk you through using sqlcmd and Transact-SQL to create a new database, add data, and run a query.

Create a new database

The following steps create a new database named TestDB.

  1. From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

    CREATE DATABASE TestDB;
  2. On the next line, write a query to return the name of all of the databases on your server:

    SELECT Name from sys.databases;
  3. The previous two commands weren't run immediately. Type GO on a new line to run the previous commands:

    GO

Insert data

Next create a new table, Inventory, and insert two new rows.

  1. From the sqlcmd command prompt, switch context to the new TestDB database:

    USE TestDB;
  2. Create new table named Inventory:

    CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT);
  3. Insert data into the new table:

    INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
  4. Type GO to run the previous commands:

    GO

Select data

Now, run a query to return data from the Inventory table.

  1. From the sqlcmd command prompt, enter a query that returns rows from the Inventory table where the quantity is greater than 152:

    SELECT * FROM Inventory WHERE quantity > 152;
  2. Run the command:

    GO

Exit the sqlcmd command prompt

  1. To end your sqlcmd session, type QUIT:

    QUIT
  2. To exit the interactive command-prompt in your container, type exit. Your container continues to run after you exit the interactive bash shell.

Connect from outside the container

You can also connect to the [!INCLUDE ssnoversion-md] instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections. The external tool uses the IP address for the host machine.

The following steps use sqlcmd outside of your container to connect to [!INCLUDE ssnoversion-md] running in the container. These steps assume that you already have the [!INCLUDE ssnoversion-md] command-line tools installed outside of your container. The same principles apply when using other tools, but the process of connecting is unique to each tool.

  1. Find the IP address for your container's host machine, using ifconfig or ip addr.

  2. For this example, install the sqlcmd tool on your client machine. For more information, see sqlcmd utility or Install the SQL Server command-line tools sqlcmd and bcp on Linux.

  3. Run sqlcmd specifying the IP address and the port mapped to port 1433 in your container. In this example, the port is the same as port 1433 on the host machine. If you specified a different mapped port on the host machine, you would use it here. You also need to open the appropriate inbound port on your firewall to allow the connection.

    Recent versions of sqlcmd are secure by default. If the connection doesn't succeed, and you're using version 18 or higher, you can add the -No option to sqlcmd to specify that encryption is optional, not mandatory.

    ::: zone pivot="cs1-bash"

    sudo sqlcmd -S <ip_address>,1433 -U <userid> -P "<YourNewStrong@Passw0rd>"

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    sqlcmd -S <ip_address>,1433 -U <userid> -P "<YourNewStrong@Passw0rd>"

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    sqlcmd -S <ip_address>,1433 -U <userid> -P "<YourNewStrong@Passw0rd>"

    ::: zone-end

  4. Run Transact-SQL commands. When finished, type QUIT.

You can also connect to the [!INCLUDE ssnoversion-md] instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections. The external tool uses the IP address for the host machine.

The following steps use sqlcmd outside of your container to connect to [!INCLUDE ssnoversion-md] running in the container. The same principles apply when using other tools, but the process of connecting is unique to each tool.

  1. Run sqlcmd in the same session you used to create your container. It keeps track of the connection information via contexts so you can easily connect at any time. sqlcmd config view can be used to view your available contexts.

    ::: zone pivot="cs1-bash"

    sudo sqlcmd

    ::: zone-end

    ::: zone pivot="cs1-powershell"

    sqlcmd query

    ::: zone-end

    ::: zone pivot="cs1-cmd"

    sqlcmd query

    ::: zone-end

  2. Run Transact-SQL commands. When finished, type QUIT.


Other common tools to connect to [!INCLUDE ssnoversion-md] include:

Remove your container

If you want to remove the [!INCLUDE ssnoversion-md] container used in this tutorial, run the following commands:

::: zone pivot="cs1-bash"

docker stop sql1
docker rm sql1

::: zone-end

::: zone pivot="cs1-powershell"

docker stop sql1
docker rm sql1

::: zone-end

::: zone pivot="cs1-cmd"

docker stop sql1
docker rm sql1

::: zone-end

If you want to remove the [!INCLUDE ssnoversion-md] container used in this tutorial, run the following command:

::: zone pivot="cs1-bash"

sudo sqlcmd delete --force

::: zone-end

::: zone pivot="cs1-powershell"

sqlcmd delete --force

::: zone-end

::: zone pivot="cs1-cmd"

sqlcmd delete --force

::: zone-end


Docker demo

After you finish using the [!INCLUDE ssnoversion-md] Linux container image for Docker, you might want to know how Docker is used to improve development and testing. The following video shows how Docker can be used in a continuous integration and deployment scenario.

[!VIDEO https://channel9.msdn.com/Events/Connect/2017/T152/player]

Related tasks

Related content

[!INCLUDE contribute-to-content]