Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Quickstart: Run SQL Server Linux container images with Docker

Feedback

In this article

Applies to:SQL Server on Linux

In this quickstart, you use Docker to pull and run the SQL Server 2017 (14.x) Linux container image,mssql-server-linux. Then you can connect withsqlcmd to create your first database and run queries.

Note

SQL Server container images are supported only on Linux hosts running onIntel and AMD x86-64 CPUs. Emulation or translation environments (for example, Rosetta 2, Prism, or QEMU) aren't tested or supported. If you want to create a feature request, or report an emulator-related issue, visit theofficial GitHub repository.

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

Warning

When you stop and remove a container, you permanently delete your SQL Server data in the container. For more information on preserving your data,create and copy a backup file out of the container or use acontainer data persistence technique.

This quickstart creates SQL Server 2017 (14.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

In this quickstart, you use Docker to pull and run the SQL Server 2019 (15.x) Linux container image,mssql-server-linux. Then you can connect withsqlcmd to create your first database and run queries.

Note

SQL Server container images are supported only on Linux hosts running onIntel and AMD x86-64 CPUs. Emulation or translation environments (for example, Rosetta 2, Prism, or QEMU) aren't tested or supported. If you want to create a feature request, or report an emulator-related issue, visit theofficial GitHub repository.

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

Warning

When you stop and remove a container, you permanently delete your SQL Server data in the container. For more information on preserving your data,create and copy a backup file out of the container or use acontainer data persistence technique.

This quickstart creates SQL Server 2019 (15.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

In this quickstart, you use Docker to pull and run the SQL Server 2022 (16.x) Linux container image,mssql-server-linux. Then you can connect withsqlcmd to create your first database and run queries.

Note

SQL Server container images are supported only on Linux hosts running onIntel and AMD x86-64 CPUs. Emulation or translation environments (for example, Rosetta 2, Prism, or QEMU) aren't tested or supported. If you want to create a feature request, or report an emulator-related issue, visit theofficial GitHub repository.

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

Warning

When you stop and remove a container, you permanently delete your SQL Server data in the container. For more information on preserving your data,create and copy a backup file out of the container or use acontainer data persistence technique.

This quickstart creates SQL Server 2022 (16.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

In this quickstart, you use Docker to pull and run the SQL Server 2025 (17.x) Linux container image,mssql-server-linux. Then you can connect withsqlcmd to create your first database and run queries.

Note

SQL Server container images are supported only on Linux hosts running onIntel and AMD x86-64 CPUs. Emulation or translation environments (for example, Rosetta 2, Prism, or QEMU) aren't tested or supported. If you want to create a feature request, or report an emulator-related issue, visit theofficial GitHub repository.

For more information on supported platforms, seeRelease notes for SQL Server 2025 on Linux.

Warning

When you stop and remove a container, you permanently delete your SQL Server data in the container. For more information on preserving your data,create and copy a backup file out of the container or use acontainer data persistence technique.

This quickstart creates SQL Server 2025 (17.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

This image consists of SQL Server running on Linux, based on Ubuntu. You can use it with the Docker Engine 1.8+ on Linux.

Starting with SQL Server 2022 (16.x) CU 14 and SQL Server 2019 (15.x) CU 28, the container images include thenew 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, seeODBC Driver 18.0 for SQL Server Released.

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

Important

sqlcmd doesn't currently support theMSSQL_PID parameter when creating containers. If you use thesqlcmd instructions in this quickstart, you create a container with the Developer edition of SQL Server. Use the command line interface (CLI) instructions to create a container using the license of your choice. For more information, seeDeploy and connect to SQL Server Linux containers.

Prerequisites

  • Docker Engine 1.8 or later versions on any supported Linux distribution. For more information, seeInstall Docker.

Pull and run the SQL Server Linux container image

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

For the bash commands in this article, thesudo command is used. If you don't want to usesudo to run Docker, you can configure adocker group and add users to that group. For more information, seePost-installation steps for Linux.

Pull the container image from the registry

Pull the SQL Server 2017 (14.x) Linux container image from the Microsoft Container Registry.

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

This quickstart creates SQL Server 2017 (14.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

The previous command pulls the latest SQL Server 2017 (14.x) Linux container image. To pull a specific image, add a colon and the tag name, such asmcr.microsoft.com/mssql/server:2017-GA-ubuntu. For available images, see theMicrosoft Artifact Registry.

Run the container

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

Important

TheSA_PASSWORD environment variable is deprecated. UseMSSQL_SA_PASSWORD instead.

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

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

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

Caution

Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible. If you don't follow these password requirements, the container can't set up SQL Server, and stops working. You can examine the error log by using thedocker logs command.

By default, this quickstart creates a container with the Developer edition of SQL Server. The process for running production editions in containers is slightly different. For more information, seeRun production container images.

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

ParameterDescription
-e "ACCEPT_EULA=Y"Set theACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
-e "MSSQL_SA_PASSWORD=<password>"Specify your own strong password that is at least eight characters and meets thePassword policy. Required setting for the SQL Server image.
-e "MSSQL_COLLATION=<SQL_Server_collation>"Specify a custom SQL Server collation, instead of the defaultSQL_Latin1_General_CP1_CI_AS.
-p 1433:1433Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1Specify 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 sql1Used 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.
-dRun the container in the background (daemon).
mcr.microsoft.com/mssql/server:2017-latestThe SQL Server Linux container image.

View list of containers

  1. To view your Docker containers, use thedocker ps command.

    sudo docker ps -a
    docker ps -a
    docker ps -a

    The output looks similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMESd4a1999ef83e   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 theSTATUS column shows a status ofUp, then SQL Server is running in the container and listening on the port specified in thePORTS column. If theSTATUS column for your SQL Server container showsExited, seeTroubleshoot SQL Server Docker containers. The server is ready for connections once the SQL Server 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 SQL Server 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 theMSSQL_SA_PASSWORD is visible inps -eax output and stored in the environment variable of the same name.

Pull and run the SQL Server Linux container image

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

For the bash commands in this article, thesudo command is used. If you don't want to usesudo to run Docker, you can configure adocker group and add users to that group. For more information, seePost-installation steps for Linux.

Pull the container from the registry

Pull the SQL Server 2019 (15.x) Linux container image from the Microsoft Container Registry.

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

This quickstart creates SQL Server 2019 (15.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

The previous command pulls the latest SQL Server 2019 (15.x) Linux container image. To pull a specific image, add a colon and the tag name, such asmcr.microsoft.com/mssql/server:2019-GA-ubuntu. For available images, see theMicrosoft Artifact Registry.

Run the container

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

Important

TheSA_PASSWORD environment variable is deprecated. UseMSSQL_SA_PASSWORD instead.

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

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

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

Caution

Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.

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

Caution

Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible. If you don't follow these password requirements, the container can't set up SQL Server, and stops working. You can examine the error log by using thedocker logs command.

By default, this quickstart creates a container with the Developer edition of SQL Server. The process for running production editions in containers is slightly different. For more information, seeRun production container images.

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

ParameterDescription
-e "ACCEPT_EULA=Y"Set theACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
-e "MSSQL_SA_PASSWORD=<password>"Specify your own strong password that is at least eight characters and meets thePassword policy. Required setting for the SQL Server image.
-e "MSSQL_COLLATION=<SQL_Server_collation>"Specify a custom SQL Server collation, instead of the defaultSQL_Latin1_General_CP1_CI_AS.
-p 1433:1433Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1Specify 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 sql1Used 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.
-dRun the container in the background (daemon).
mcr.microsoft.com/mssql/server:2019-latestThe SQL Server Linux container image.

View list of containers

  1. To view your Docker containers, use thedocker ps command.

    docker ps -a
    docker ps -a
    docker ps -a

    The output looks similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMESd4a1999ef83e   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 theSTATUS column shows a status ofUp, then SQL Server is running in the container and listening on the port specified in thePORTS column. If theSTATUS column for your SQL Server container showsExited, seeTroubleshoot SQL Server Docker containers. The server is ready for connections once the SQL Server 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 SQL Server 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 theMSSQL_SA_PASSWORD is visible inps -eax output and stored in the environment variable of the same name.

Pull and run the SQL Server Linux container image

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

For the bash commands in this article, thesudo command is used. If you don't want to usesudo to run Docker, you can configure adocker group and add users to that group. For more information, seePost-installation steps for Linux.

Pull the container image from the registry

Pull the SQL Server 2022 (16.x) Linux container image from the Microsoft Container Registry.

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

This quickstart creates SQL Server 2022 (16.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

The previous command pulls the latest SQL Server 2022 (16.x) Linux container image. To pull a specific image, add a colon and the tag name, such asmcr.microsoft.com/mssql/server:2022-GA-ubuntu. For available images, see theMicrosoft Artifact Registry.

Run the container

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

Important

TheSA_PASSWORD environment variable is deprecated. UseMSSQL_SA_PASSWORD instead.

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

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

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

Caution

Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible. If you don't follow these password requirements, the container can't set up SQL Server, and stops working. You can examine the error log by using thedocker logs command.

By default, this quickstart creates a container with the Developer edition of SQL Server. The process for running production editions in containers is slightly different. For more information, seeRun production container images.

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

ParameterDescription
-e "ACCEPT_EULA=Y"Set theACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
-e "MSSQL_SA_PASSWORD=<password>"Specify your own strong password that is at least eight characters and meets thePassword policy. Required setting for the SQL Server image.
-e "MSSQL_COLLATION=<SQL_Server_collation>"Specify a custom SQL Server collation, instead of the defaultSQL_Latin1_General_CP1_CI_AS.
-p 1433:1433Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1Specify 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 sql1Used 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.
-dRun the container in the background (daemon).
mcr.microsoft.com/mssql/server:2022-latestThe SQL Server Linux container image.

Change the system administrator password

The system administrator account (sa) is created on the SQL Server instance during the setup process. After you create your SQL Server container, runecho $MSSQL_SA_PASSWORD in the container to discover theMSSQL_SA_PASSWORD environment variable you specified. For security purposes, you should change yoursa password in a production environment.

  1. Choose a strong password to use for thesa account. Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.

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

    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}")\""
    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd `   -S localhost -U sa -P "<password>" `   -Q "ALTER LOGIN sa WITH PASSWORD='<new-password>'"
    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd `   -S localhost -U sa -P "<password>" `   -Q "ALTER LOGIN sa WITH PASSWORD='<new-password>'"

    Caution

    Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.

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

Disable the SA account as a best practice

Important

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

When you connect to your SQL Server instance using the system administrator (sa) account for the first time after installation, it's important for you to follow these steps, and then immediately disable thesa account as a security best practice.

  1. Create a new login, and make it a member of thesysadmin server role.

  2. Connect to the SQL Server instance using the new login you created.

  3. Disable thesa account, as recommended for security best practice.

View list of containers

  1. To view your Docker containers, use thedocker ps command.

    docker ps -a
    docker ps -a
    docker ps -a

    The output looks similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMESd4a1999ef83e   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 theSTATUS column shows a status ofUp, then SQL Server is running in the container and listening on the port specified in thePORTS column. If theSTATUS column for your SQL Server container showsExited, seeTroubleshoot SQL Server Docker containers. The server is ready for connections once the SQL Server 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 SQL Server 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.

Pull and run the SQL Server Linux container image

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

For the bash commands in this article, thesudo command is used. If you don't want to usesudo to run Docker, you can configure adocker group and add users to that group. For more information, seePost-installation steps for Linux.

Pull the container image from the registry

Pull the SQL Server 2025 (17.x) Linux container image from the Microsoft Container Registry.

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

This quickstart creates SQL Server 2025 (17.x) containers. If you prefer to create Linux containers for different versions of SQL Server, see:

The previous command pulls the latest SQL Server 2025 (17.x) Linux container image. To pull a specific image, add a colon and the tag name, such asmcr.microsoft.com/mssql/server:2025-GA-ubuntu. For available images, see theMicrosoft Artifact Registry.

Run the container

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

Important

TheSA_PASSWORD environment variable is deprecated. UseMSSQL_SA_PASSWORD instead.

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

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

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

Caution

Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible. If you don't follow these password requirements, the container can't set up SQL Server, and stops working. You can examine the error log by using thedocker logs command.

By default, this quickstart creates a container with the Developer edition of SQL Server. The process for running production editions in containers is slightly different. For more information, seeRun production container images.

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

ParameterDescription
-e "ACCEPT_EULA=Y"Set theACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
-e "MSSQL_SA_PASSWORD=<password>"Specify your own strong password that is at least eight characters and meets thePassword policy. Required setting for the SQL Server image.
-e "MSSQL_COLLATION=<SQL_Server_collation>"Specify a custom SQL Server collation, instead of the defaultSQL_Latin1_General_CP1_CI_AS.
-p 1433:1433Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this container port is then exposed to TCP port 1433 on the host.
--name sql1Specify 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 sql1Used 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.
-dRun the container in the background (daemon).
mcr.microsoft.com/mssql/server:2025-latestThe SQL Server Linux container image.

Change the system administrator password

The system administrator account (sa) is created on the SQL Server instance during the setup process. After you create your SQL Server container, runecho $MSSQL_SA_PASSWORD in the container to discover theMSSQL_SA_PASSWORD environment variable you specified. For security purposes, you should change yoursa password in a production environment.

  1. Choose a strong password to use for thesa account. Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.

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

    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}")\""
    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd `   -S localhost -U sa -P "<password>" `   -Q "ALTER LOGIN sa WITH PASSWORD='<new-password>'"
    docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd `   -S localhost -U sa -P "<password>" `   -Q "ALTER LOGIN sa WITH PASSWORD='<new-password>'"

    Caution

    Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.

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

Disable the SA account as a best practice

Important

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

When you connect to your SQL Server instance using the system administrator (sa) account for the first time after installation, it's important for you to follow these steps, and then immediately disable thesa account as a security best practice.

  1. Create a new login, and make it a member of thesysadmin server role.

  2. Connect to the SQL Server instance using the new login you created.

  3. Disable thesa account, as recommended for security best practice.

View list of containers

  1. To view your Docker containers, use thedocker ps command.

    docker ps -a
    docker ps -a
    docker ps -a

    The output looks similar to the following example:

    CONTAINER ID   IMAGE                                        COMMAND                    CREATED         STATUS         PORTS                                       NAMESd4a1999ef83e   mcr.microsoft.com/mssql/server:2025-latest   "/opt/mssql/bin/perm..."   2 minutes ago   Up 2 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   sql1
  2. If theSTATUS column shows a status ofUp, then SQL Server is running in the container and listening on the port specified in thePORTS column. If theSTATUS column for your SQL Server container showsExited, seeTroubleshoot SQL Server Docker containers. The server is ready for connections once the SQL Server 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 SQL Server 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.

Connect to SQL Server

The following steps use the SQL Server command-line tool,sqlcmd utility, inside the container to connect to SQL Server.

  1. Use thedocker 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.

    docker exec -it sql1 "bash"
    docker exec -it sql1 "bash"
    docker exec -it sql1 "bash"
  1. Once inside the container, connect locally withsqlcmd, using its full path.

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

    Recent versions ofsqlcmd are secure by default. For more information about connection encryption, seesqlcmd utility for Windows, andConnecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the-No option tosqlcmd 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>
  1. Once inside the container, connect locally withsqlcmd, using its full path.

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

    Recent versions ofsqlcmd are secure by default. For more information about connection encryption, seesqlcmd utility for Windows, andConnecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the-No option tosqlcmd 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>
  1. If successful, you reach asqlcmd command prompt:1>.

Create and query data

The following sections show you how to usesqlcmd 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 namedTestDB.

  1. From thesqlcmd 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 nameFROM sys.databases;
  3. The previous two commands aren't run immediately. TypeGO on a new line to run the previous commands:

    GO

Insert data

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

  1. From thesqlcmd command prompt, switch context to the newTestDB database:

    USE TestDB;
  2. Create a new table namedInventory:

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

    INSERT INTO InventoryVALUES (1, 'banana', 150);INSERT INTO InventoryVALUES (2, 'orange', 154);
  4. TypeGO to run the previous commands:

    GO

Select data

Now, run a query to return data from theInventory table.

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

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

    GO

Exit the sqlcmd command prompt

  1. To end yoursqlcmd session, typeQUIT:

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

Connect from outside the container

You can connect to the SQL Server 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 usesqlcmd outside of your container to connect to SQL Server running in the container. These steps assume that you already have the SQL Server 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, usingifconfig orip addr.

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

  3. Runsqlcmd 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, use it here. You also need to open the appropriate inbound port on your firewall to allow the connection.

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

    sudo sqlcmd -S <ip_address>,1433 -U <userid> -P "<password>"
    sqlcmd -S <ip_address>,1433 -U <userid> -P "<password>"
    sqlcmd -S <ip_address>,1433 -U <userid> -P "<password>"

    Caution

    Your password should follow the SQL Server defaultpassword policy. 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. Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.

  4. Run Transact-SQL commands. When finished, typeQUIT.

Other common tools to connect to SQL Server include:

Remove your container

If you want to remove the SQL Server container used in this tutorial, run the following commands:

docker stop sql1docker rm sql1
docker stop sql1docker rm sql1
docker stop sql1docker rm sql1

Docker demo

After you finish using the SQL Server 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.

 

Related tasks

Related content

Contribute to SQL documentation

Did you know that you can edit SQL content yourself? If you do so, not only do you help improve our documentation, but you also get credited as a contributor to the page.

For more information, seeEdit Microsoft Learn documentation.


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?