Youtube video link
the password Root@123, container name mssql, and port number 1433 used in the article are just examples and can be changed to whatever you prefer.
Pull and Download SQL Server image
The first step is to pull and run the SQL Server Linux container image. You can do this by running the following command:
sudo docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Root@123" -p 1433:1433 --name mssql -d mcr.microsoft.com/mssql/server
This command downloads the SQL Server Linux container image from the Microsoft container registry and runs it as a container with the name “mssql”. The
-e
option specifies environment variables for the container. In this case, we setACCEPT_EULA
to "Y" to accept the SQL Server license terms, andSA_PASSWORD
to "Root@123" to set the system administrator (SA) password.The
-p
option maps the host port 1433 to the container port 1433, which is the default SQL Server port. This allows you to connect to SQL Server from outside the container using the host IP address and port 1433.The
-d
option runs the container in detached mode, meaning it runs in the background and doesn't attach to the console.
Check and Remove Your Container
# Show container list
sudo docker ps -a
# Stop, Start and Remove mssql container
sudo docker stop mssql
sudo docker start mssql
sudo docker rm mssql
You can check the status of your container using the
docker ps -a
command. This shows all containers, including stopped ones. To stop the container, rundocker stop mssql
. To start it again, rundocker start mssql
. To remove the container, rundocker rm mssql
.
Connect to SQL Server
To connect to SQL Server running inside the container, you need to open a bash shell inside the container using the docker exec -it mssql “bash” command. Once you’re inside the container, you can use the sqlcmd command-line tool to execute SQL commands. Here’s an example:
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Root@123"
This command connects to SQL Server on localhost using the SA login and password “Root@123”. Once connected, you can execute SQL commands as you would with a regular SQL Server instance.
Connect from Outside the Container
To connect to SQL Server from outside the container, you need to get the container IP address. You can do this using the docker inspect command:
sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mssql;
This command shows the IP address of the “mssql” container. You can use this IP address to connect to SQL Server from outside the container using your favorite SQL client such as dbeaver or HeidiSQL.
Create and Query Data
Once you’re connected to SQL Server, you can create databases and tables, and insert and query data as you would with a regular SQL Server instance. Here’s an example:
CREATE DATABASE TestDB;
SELECT Name from sys.databases;
Go
USE TestDB;
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT);
INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
Go
SELECT * FROM Inventory WHERE quantity > 152;
Go
QUIT
This code creates a database called “TestDB”, creates a table called “Inventory”, inserts two rows into the table, and queries