dbatools with SQL on Docker and running SQL queries

I had a question from my good friend Andrew Pruski dbafromthecold on twitter or SQL Container Man as I call him 🙂

How do you guys run SQL Commands in dbatools

I will answer that at the bottom of this post, but during our discussion Andrew said he wanted to show the version of the SQL running in the Docker Container.

Thats easy I said. Here’s how to do it

You need to have installed Docker first see this page You can switch to using Windows containers right-clicking on the icon in the taskbar and choosing the command. If you have not already, then pull the SQL 2017 SQL image using

This may take a while to download and extract the image but its worth it, you will be able to spin up new SQL instances in no time

You can create a new SQL Docker container like this


In only a few seconds you have a SQL 2017 instance up and running (Take a look at Andrews blog at dbafromthecold.com for a great container series with much greater detail)

Now that we have our container we need to connect to it. We need to gather the IPAddress. We can do this using docker command docker inspect but I like to make things a little more programmatical. This works for my Windows 10 machine for Windows SQL Containers. There are some errors with other machines it appears but there is an alternative below


Those two lines of code (and several lines of comments) puts the results of the docker inspect command into a variable and then uses regex to pull out the IP Address

If you are getting errors with that you can also use

Thanks Andrew 🙂

Now we just need our credentials to connect to the instance


and we can connect to our SQL container

and get the version

and many many other properties, just run

to see them. At the bottom, you will see a ScriptMethod called Query, which means that you can do things like


Which looks like

It’s slightly different with a Linux SQL container. Switch Docker to run Linux containers by right-clicking on the icon in the taskbar and choosing the command to switch.
If you haven’t already pull the Linux SQL image

and then create a container


Now we just need to connect with localhost and the port number which we have specified already and we can connect again


Of course, this isn’t restricted just Connect-DbaInstance you can do this with any dbatools commands

Go and explore your Docker SQL conatiners with dbatools 🙂

You can get it using

and find commands with

Don’t forget to use Get-Help with the name of the command to get information about how to use it

Enjoy 🙂

One thought on “dbatools with SQL on Docker and running SQL queries

  1. Pingback: SQL On Docker And Running SQL Queries – Curated SQL

Please feel free to comment on this post. All comments are moderated first before appearing on the site