Today I am going to explain the procedure for connecting to a Microsoft SQL Server 2019 Express edition docker image
Pre-requisites
- Docker Desktop
- Windows OS
- Powershell/command prompt
Procedure
Get and run docker image
> docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<Your_Password>" -e "MSSQL_PID=Express" --name "<Your_SQL_Server_Name>" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
Command information
Remove the "MSSQL_PID=Express" to run other version of SQL Server
Replace 2019 with required SQL Server version
Password should be atleast 8 characters with capital, small, numeric, special character combination
Use different port if you already have a local sql server
Name should not contain spaces
Check for docker container
> docker ps
You should able to see the container with your SQL Server name at the end in running status
Connect to SQL Server
docker exec -it "<Your_SQL_Server_Name>" /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "<Your_Password>"
You should be able to see "1>" prompt
Command information
Use the SQL Server name or user the container ID
Use database and play around with table(s)
Important
Multiple commands can be entered one after one, but to execute the set of command(s), you need give "GO" command.
Create Database
CREATE DATABASE SampleDB
GO
List all databases
SELECT Name from sys.Databases
GO
Start using the database
USE SampleDB
GO
Create table
CREATE TABLE UserInfo ( Id INT, Name VARCHAR(64))
GO
Insert values
INSERT INTO UserInfo (1, 'ABC')
GO
Retrieve table contents
SELECT * FROM UserInfo
GO
Exit from SQL Server
QUIT
Hope you are able to run the SQL Server Docker image.
Please send your feedback and comments to psrdotcom@gmail.com
No comments:
Post a Comment