skip to content
Khalid Abuhakmeh

SQL Server in Docker: Run Queries From the Command Line

Run SQL Server in Docker: start a container, attach a shell session, open sqlcmd, and run SQL queries straight from your command line, step by step.

4 min read Updated

Running SQL Server in Docker gets you a disposable, local SQL Server instance in one command, and from there you can attach a shell and run queries with sqlcmd without installing anything else. In this post, you’ll see the commands necessary to start a SQL Server container instance, connect to a shell instance within the container, and execute some SQL commands.

Before getting started, you will need Docker running in your development environment. I’m also assuming you have command line access to your environment, whether through SSH or some editor.

Starting A SQL Server Container

The official SQL Server docker image is available on Docker Hub, and a single command can get you up and running. I highly recommend naming your running instances using the --name flag.

Terminal window
docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=Pass123!" -p 1433:1433 --name sqlserver -d mcr.microsoft.com/mssql/server:2019-latest

The previous command will call our container sqlserver, in addition to accepting the end-user license agreement, initializing the sa user account, and exposing the SQL Server port to 1433 so apps can access the database remotely. We also start our container detached (-d), which will keep your terminal free for further user inputs. Feel free to change any values to meet your specific needs.

Updated October 2026: this post was written against the 2019 image. Microsoft now documents MSSQL_SA_PASSWORD instead of SA_PASSWORD for setting the sa password, so use that variable name on current images. Newer images also ship the command-line tools in /opt/mssql-tools18/bin rather than /opt/mssql-tools/bin, and that version of sqlcmd may require -C to trust the container’s self-signed certificate. Adjust the paths and flags below if the tools aren’t where this post says they are.

Next, let’s attach to the running container.

Attaching to Our SQL Server Container

Our first goal is to attach or current command line session to our SQL Server container instance. We immediately connect to the sqlserver named container and start a bash session.

Terminal window
docker exec -it sqlserver "bash"

You can also start Shell instead of bash. Your choice really.

Terminal window
docker exec -it sqlserver /bin/sh

Both will get the job done.

Running SQLCMD in a Docker Container

Now that we’re in the context of our container, we can run the tools bundled with the SQL Server image.

Terminal window
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Pass123!'

Running the above command will start an instance of SQLCMD, letting us write queries into the command-line session. After the command, if everything goes correctly, you will see the following output.

Terminal window
$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Pass123!'
1>

Great! Let’s write a simple query.

SQL Queries in SQLCMD

The first query I typically write is to determine what databases are running in my container instance.

select physical_database_name from sys.databases

Enter the query, followed by a GO on the second line. Your output should look something similar to the following.

Terminal window
1> select physical_database_name from sys.databases
2> Go
physical_database_name
-------------------------------------------------------
master
tempdb
model
msdb
(4 rows affected)

You’ll need to remember to include the GO keyword when you’ve completed writing your SQL statements. Now you can write all the SQL queries you like!

Exiting SQL CMD and Docker Session

Hopefully, you’ve been able to write all the SQL Queries your heart desires, and now you need to get back out of the command line session. To exit the SQLCMD session, type the keyword exit. To exit the command-line session, you need to type exit again. Now you should be out of your Docker container and back in your remote environment.

Terminal window
1> exit
$ exit
(base) ~ via 🅒 base at ☸️ docker-desktop took 8m 20s

Conclusion

Containerization has made our lives managing dependencies more straightforward, but it also means we have to adapt to take advantage of the current technologies available to us. If you’re a .NET developer, like me, you’ll find yourself using the SQL Server container more than a few times a day.

I hope this short tutorial helps you jump-start your remote development journey. If you found this post helpful, please share it with others.

References

Comments

Related Posts