Run SQL Server Linux container images with Docker

pull and run the SQL Server Linux container image
Reading Time: 2 minutes

In this article i will explain how you can Pull and run the SQL Server Linux container image with commonly used parameter.

To install on Docker, first download the images related to SQL Server:

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

To install on Docker, it is recommended to store the Data, Log, Backup, and Dump files on the Host instead of the Container. Therefore, create folders with paths similar to the following on the Host, and then apply the necessary permissions as follows:

# Create directories (Replace <ContainerName> with your instance or container name)
sudo mkdir -p /var/opt/mssql/<ContainerName>/data
sudo mkdir -p /var/opt/mssql/<ContainerName>/log
sudo mkdir -p /var/opt/mssql/<ContainerName>/dump
sudo mkdir -p /var/opt/mssql/<ContainerName>/backup

cd /var/opt/mssql/<ContainerName>
ls -ll  //checks current permissions

# Added the root group to directories
sudo chgrp -R 0 /var/opt/mssql/<ContainerName>
sudo chmod -R g=u /var/opt/mssql/<ContainerName>

# Added non-root user to directories
sudo chown -R 10001:0 /var/opt/mssql/<ContainerName>  

To install on Docker, you can use the following script that contains the necessary parameters for installation:
Before executing the script, replace the following variables with appropriate values. Note that regarding the license, if you are using a serial number, each license can only be applied to one container on a single host. Using the license more than once while creating another container will result in an error:

<ContainerName>: Your nuique container name, Like: db1
<Licence>: Developer,Express or Licence code, Developer
<SaStrongPassword>: SA user password, this should be strong. Like: !@#$%^&*()A
<Collation>: SQL Instance collation. Like: SQL_Latin1_General_CP1_CI_AS or ARABIC_CI_AI
<MaxMemorySizeMB>: MSSQL Max Server Memory size in MB, Like: 4096

docker run --name <ContainerName> --hostname <ContainerName> \
-e 'ACCEPT_EULA=Y' \
-e 'MSSQL_PID=<Licence>' \
-e 'MSSQL_SA_PASSWORD=<SaStrongPassword> ' \
-e 'MSSQL_COLLATION=<Collation> ' \
-e 'MSSQL_MEMORY_LIMIT_MB=<MaxMemorySizeMB>' \
-e 'MSSQL_AGENT_ENABLED=true' \
-e 'MSSQL_TCP_PORT=1433' \
-e 'MSSQL_DATA_DIR=/var/opt/mssql/data' \
-e 'MSSQL_LOG_DIR=/var/opt/mssql/log' \
-e 'MSSQL_DUMP_DIR=/var/opt/mssql/dump' \
-e 'MSSQL_BACKUP_DIR=/var/opt/mssql/backup' \
-p 2433:1433 \
-v /var/opt/mssql/<ContainerName>/data:/var/opt/mssql/data \
-v /var/opt/mssql/<ContainerName>/log:/var/opt/mssql/log \
-v /var/opt/mssql/<ContainerName>/dump:/var/opt/mssql/dump \
-v /var/opt/mssql/<ContainerName>/backup:/var/opt/mssql/backup \
-d mcr.microsoft.com/mssql/server:2022-latest

After running the container, it is essential to change the sa password because it is stored in the environment variable: $MSSQL_SA_PASSWORD and is accessible (use echo $MSSQL_SA_PASSWORD).

docker exec -it db1 /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}")\""

 

Author Profile

I make SQL Server faster and more reliable. Database administration isn’t about passing a certified exam, or about pointing and clicking your way through a crisis. Database administration is about applying the right solution at the right time, avoiding risk, and making robust choices that get you home each night in time for dinner with your family .

My areas of specialty are:

• SQL Server 2019-2008 performance tuning.

• Designing high availability and disaster recovery solutions.

• Securing SQL Server

• SSIS , SSAS

• Designing Data warehouse

Comments (0)
Add Comment