In this video, we'll look at how to set up an SQL Server database using Docker.
Let's get started.
Here are the high level steps we need to do.
First, we download the Docker application.
Next, we set up an account on Docker Hub.
Then, we run Docker and download the SQL Server image.
Then, we run the SQL Server image.
Finally, we connect to the database and run some SQL.
This video is demonstrated on a Mac but should work in a similar way on Windows.
To get started, we need to download the Docker application.
This is freely available from the Docker website, at www.docker.com.
Once the website has loaded, click on Get Started.
On the Get Started page, there are several options.
Click on Docker Desktop.
The other two options, Docker Hub and Play with Docker, are not needed for our process.
Select the right version for your computer, which is either of the two Mac versions or
a Windows version.
The Docker setup file will then begin downloading.
It's about 580 MB which could take a few minutes to download.
Once the download is complete, install it.
This can be done by opening the file on MacOs and dragging it to the Applications folder,
or following the installation steps on Windows.
It takes a couple of minutes to install.
Once it is installed, you can run it.
This will cause the Docker service to start.
I believe you can also run it from the command line or from elsewhere in Mac, but this is
the approach that has worked for me and will hopefully work for you.
Now we've got Docker running, it's time to get our database files.
The files that Docker uses to run applications are called images, which are a pre-built collection
of files.
These images are available on a website called Docker Hub.
There are a whole range of images there, but the one we're looking for is for SQL Server.
So, we'll need to visit the Docker Hub website, set up an account, find the right image, and
then start using it.
Let's see how we can do that.
First, visit the Docker Hub website at hub.docker.com.
You'll need an account to access the Docker images, so click on the Sign Up button.
Enter your details on the Sign Up page.
Remember the password you enter here as you'll need it later.
The next page is the Choose Your Plan page.
There are a couple of options, but for now you can select the Free plan.
This is enough for our purposes.
Follow the steps to verify your email address.
You now have an account on Docker Hub.
Now we've logged in to Docker Hub, it's time to find the image for the SQL Server database.
We're going to get the URL for the image, and then use this URL as part of a command
we'll run on the command line.
To find the image, enter the term "SQL Server" into the search bar at the top of the page.
We enter this because there are a range of Microsoft and SQL images and we only want
the database.
Click on Microsoft SQL Server, and you'll be taken to this page.
At the moment, there are both 2017 and 2019 versions available.
In this demo, we'll use SQL Server 2017, but you can just as easily use SQL Server 2019.
If you scroll down a little, you can see a few sample commands under the "How to use
this image" section.
Copy the command from the first example here, which is the CU8 version of SQL Server 2017.
There are a few things to note in this command.
The "-e" stands for environment flags, and there are a couple of those on this command.
The first one allows Docker to accept the EULA or End User License Agreement.
The second one will set the password for the built-in "SA" account.
You can change this password before you run it, which we'll see shortly.
The "-p" indicates the port numbers.
This means that port 1433 is used both inside the container and outside the container for
us to connect to the database.
The "-d" means detached mode, which means the container runs in the background and we
can use our terminal for other commands.
The final part is the path to the image on docker hub, which will be downloaded.
Now, copy the command here that you want to use.
Next, we need to open a command line or terminal.
I'll open the Terminal app as I'm on a Mac, but I believe the Command Line on Windows
will work in the same way.
Once the Terminal app is open, we can start the process of getting our Docker image.
We're going to do three things: log in, then download the image, then run it.
We need to log in to the Docker Hub account first.
This is done using the docker login command.
Enter "docker login" at the command line and press enter.
You'll be asked to enter your username and password, which is what you provided when
you created the Docker Hub account.
Now we have logged in, we can download the image.
This can be done using the "docker pull" command, which is the command in the black box we saw
on the Docker Hub page.
However, we can also use the "docker run" command, which will run the container if it
exists on your computer already, and download it if it does not exist.
So, paste the command here in the terminal window that you copied from the previous page.
Before you press Enter, you may want to replace the "your strong password" with another password.
It's up to you, but the password mentioned here should work.
You should also add a name for the container.
This makes it easier to refer to in the future.
To do this, add this to the end of the command:—name sqlserver.
That's two dashes, then name, then space, then the name you want to give it such as
sqlserver.
Then, press Enter to run the command.
The image will start downloading.
It's a few hundred megabytes so may take a few minutes.
Once you run the command, the image will start up.
This can take a minute or two.
If you get an error about the name already being used, then it's likely because you have
run this command in the past and the container name of "sqlserver" is already created.
If so, you can start the existing container.
To do this, type "docker start sqlserver".
After a moment, the image will start up.
To check the status of your containers, you can run the command "docker ps".
This shows a list of all images that are running.
In the status column, you'll see something like "starting" to indicate the container
is starting up.
Run the docker ps command again in a minute or two and you should see "healthy".
This means the container is running.
Awesome!
We now have a Docker container running with our SQL Server database.
Let's connect to it and run some SQL.
We'll use Azure Data Studio to connect, which is Microsoft's freely available IDE that works
on Windows and MacOS.
You can use whatever IDE you want.
I'm not using SQL Server Management Studio in this demo, as it doesn't run on a Mac,
but the process should work with Management Studio.
For now, I'll assume you have an IDE ready, such as Azure Data Studio, so open it now.
Click Create a new connection and you'll see this tab here.
The connection details are available on the image page on Docker Hub and are also in the
command we ran, and we'll enter them here.
For the host, enter localhost, then comma, then 1433 which is the port number.
For the username, enter "sa".
For the password, enter the password you provided when you ran the terminal command earlier.
Leave everything as the default, and click Connect.
If everything has been set up correctly, the connection should be successful.
If there are any errors, take a look at the description and comments where I'll list a
few ways to resolve them.
You should see a screen that looks like this.
In other IDEs, you may get taken directly to an SQL editor screen.
In Azure Data Studio, click on the New Query button.
A new SQL tab will be displayed.
Enter a simple query, such as SELECT \@@VERSION, and run it.
This should show the version of SQL Server you're running.
You'll see the output on the screen.
This is the result of the query on the SQL Server Database inside the container.
It shows SQL Server 2017 which is the version we chose on Docker Hub.
Congratulations!
You've successfully set up an SQL Server database inside a Docker container and connected to
it!
If you have any errors, let me know in the comments below.
I'll put some resources in the Description for some common issues as well.
To stop the database, you can run the Docker Stop command.
Go back to the terminal, and enter "docker stop sqlserver", where sqlserver is the name
of your container.
This will stop the container and the SQL Server database.
To start it again, use the docker run command we saw earlier.
That brings us to the end of this tutorial.
I'll create some other videos to cover things like persisting data and automatically running
scripts.
If you learned something new from this video, make sure to subscribe to my channel.
If you want to learn more about database design and development, visit databasestar.com.
That’s where I share my best database-related content.
Which part of this Docker tutorial was the most helpful for you?
Was it the command to actually run the container?
Or the steps to connect to the database using your IDE?
Or something else?
Thanks for watching.