This tutorial, Docker for dummies, is a short guide to help you get started with Docker. I will show you how you can install Docker Desktop, download and install Microsoft SQL Server, and use the SQL server (with database) which runs in Docker.
Things you should know
For this tutorial you need to know the following terms/techniques:
- Virtualization
At the end of this tutorial is a TLTR (Too Long To Read). With this, you skip all the long rows of text and get a step-by-step list of what you need to do to get Docker running and installing SQL Server.
Table Of Contents
Virtualization is a technique people in IT use a lot; create a fake computer to do all sorts of work. Most of us know VMWare, VirtualBox, or Hyper-V. A few applications allow you to create and maintain virtual computers. The big advantage of virtualization is that you can create a completely new environment that has no link to your own devices. With this you can test your software if it would work in a different environment like OS, time zone, language, and much more.
When you are done playing with your virtual machine you can simply delete it. If you broke your virtual machine there is no harm done; you simply delete it and start a new one. Buying a new pc or laptop is way more expensive.
Virtual machines usually cost machine power. The computer you use to host the virtual machines gives up some (or a lot if you choose to do so) memory and CPU. If you have an older machine or do not have a lot of internal memory, virtualization could be a problem for you. But Docker is here to save the day!
Starting in 2013, Docker introduced containers. These are packages of software bundled together. Docker is what we call OS-level virtualization, using the host machine’s hardware to run. In ‘normal’ virtualization you create a new machine, add internal memory, create a disk, assign CPUs, and much more. With Docker, you simply install what you want and it works. Containers are the key subjects in Docker.
Containers
Containers are used to isolate your application. If you are a developer you might know the pain of multilanguage applications or websites. Or maybe a coworker says “it works on my machine!” when you try to run the application on your computer. Well, containers can solve this.
Each container can have a different operating system, different settings, other languages, or even another timezone. This way we can test the software if it still works. If your application supports different types of databases, create a container with database 1 and a container with database 2. Now you can check if your application still works.
Containers cannot talk to each other unless explicitly set up. Contains can contain more than one item. A web application usually has its front-end (the part the user sees) and a database. These two run separately, but in a container, they are together. Still separately, but in one container.
See it as families; each family lives in a house, but not with other families. Other families have their own houses. They cannot communicate with each other unless you connect the houses to the internet or with a phone line.
Another reason to use Docker is to run web applications without installing extra software, paying for an expensive host, or diving into the depths of cloud computing. And this is where this tutorial is written for.
Images
We need something to get the software in the containers, right? This is where images come into play. Images are like archives with the software you want. Examples are databases, operating systems, programming languages, and much more. Images can be downloaded and installed into Docker where you can run the images in containers. A container can contain one or more images.
Images contain an OS and all the software you need to run your application(s). For example: If you want to run an MVC REST API, you will need C# and .NET. There are images online that can install a container containing C# and .NET. But how do these run? Do they run on your OS? No, they run on an isolated OS, mostly Linux. The reason why Linux is used has a few reasons: It’s cheap, most software works in Linux too, and last – but most important – you can’t use Windows containers unless you have Windows professional or enterprise. Most home users have a home and not the more expensive Windows versions.
Most of the time you will download, install, and run images you find online. But you can also create your images. How this is done is a story for another time, because it’s a bit too complex for a beginner’s tutorial. So I will focus on the downloading and usage of images.
But how do you find these online images? Well, it’s very easy: There is a website called Docker Hub, which is the number one website for Docker images.
Docker Hub
There is a big repository called the Docker Hub. You can find it on hub.docker.com. It contains all the images you can download and use.
Although you are immediately confronted with a registration form, it’s not required. You can simply click on “Explore” at the right top of the screen. This will bring you to all the images available for you.
A few things you should know:
Docker has three types of publishers.
- Docker Official Image
Official images by Docker. - Verified Publisher
Verified by Docker. Manages by a commercial entity (profit-making company like Microsoft) - Open Source Program
Images by members of the Docker Open Source program.
Each with its pros and cons.
Watch for the updated time.
Images are maintained and get updates. Especially because of security issues. When something is not safe anymore, the creators of images should fix this and update the image. The longer they wait, the more insecure the image gets. My advice: Don’t use images older than 6 months if possible.
In the next chapters, I will explain how you download, install, and use Docker. I will use the MSSQL image for demonstration purposes. But this is a good moment to check the image of MSSQL. Microsoft MSSQL runs in Microsoft SQL Server. If you search for MSSQL in Docker Hub you won’t find it. Even with a query of “SQL Server” it’s not easy to locate. Let me help you with a link.
The detail page of an image is usually the same. Some information about the image (which we usually don’t read), some text on how to use the image (which we usually don’t read either since you are reading tutorials), and some configuration (which you should read although it sometimes looks as if NASA wrote it).
The most important part of the details is how to use the image. It shows you commands you can use to download and install the image into your local Docker environment.
A small example:
Environment Settings
You don’t have to run this command yet. Let me explain some of the parts. Just keep in mind that other images have other configuration settings, usually noted in the “Environment Variables” chapter. SQL Server has its over variables:
- ACCEPT_EULA=Y
This says you agree to the End-User Licensing Agreement. You can set it to N(o), instead of Y(es), but then the installation won’t work. - SA_PASSWORD=yourstrong(!)Password
SA stands for Server Administrator. This SA always has the username sa (lower case) and a password. Put the password here.
These are the most important environment settings. There is a third: MSSQL_PID, which lets you select/choose the edition of SQL Server. Options are:
- Developer
The default is MSSQL_PID is not set. - Express
- Standard
- Enterprise
- EnterpriseCore
If you want to know the difference, please use Google.
Ports
Another setting in the command example is -p. which is the flag for ports. SQL Server, and much other internet-related software, use ports. SQL Server uses port 1433 as a default. But… If you have multiple SQL Server instances running you will have a conflict with the ports. To use a different port, change 1433:1433 to 1444:1433. This means you want to map port 1444 to 1433.
Detached Mode
The -d flag stands for detached mode. This will run the container in the background. Also, the console you are using to run or install the image will be available for other commands. If you don’t use the -d the console will be used for the output of the container. This way you can see errors and logs, which are also available in the Docker Desktop.
The actual image
The last part of the example command is the image. Not all images have the same, logical markup and I think the URL-like image is long on the SQL Server image. Anyway, this seems to work so let’s not worry about it too much.
Do notice the: latest at the end of the image. This indicates you want to use the latest image version of the SQL Server image. I do recommend using the latest tag for most images so you know you have the most recent, up-to-date version. If you do want a different version, check out the Full Tag Listing. This gives you all the image versions.
Other images use keywords such as “Supported tags” where you can find the version.
Download and install Docker
Reading about containers, images, and settings is fun and all, but how about we are going to download and install an image for real!
For this, you will need Docker. It is available for Windows and Linux. If you use a browser to navigate to https://www.docker.com/get-started/ you can download Docker Desktop. It has all you need, including a fancy desktop application if you don’t like command lines, like me.
When you have downloaded the installation file you can open it and follow the instructions. In some cases, you need to restart your computer, but Docker installation will tell you when the time comes. After restarting (or not) you will have to accept the Service Agreements. Then Docker starts… Or stops…Or starts… This is a message you will see and according to the documents, it’s rare that this happens. After a while, you will get an error message.
Again; this happens in rare cases. Fun fact: I have been installing Docker many times on different systems and I always get this error.
So, what is this WSL 2 Linux kernel? It allows you, or Windows actually, to run a Windows sub-system to run Linux environments. This way Docker can use Linux as the base operating system for your containers. It doesn’t need a virtual machine or anything special after you installed the WSL 2 Linux kernel.
In the error is a link. You can click it and your default browser shows a page that shows you another link to download the installation file to install WSL 2 on your machine. For your convenience; click here to download the installation file. After installation, a restart could be needed.
When all is well, you will see the next screen when you start Docker:
You can follow this “2-minute” introduction, but I am going to skip it for now. If you skip it you will see the screen that should contain the containers, but you haven’t got any containers… yet. Instead, it shows you some examples to run a Sample container and some guides.
But we want to install SQL Server, so let’s continue with that. To download and install an image into a container we need to open a command prompt. It can be done within Docker Desktop, but for this, you need an account and I want to show you the command line commandos for Docker.
Install and use MSSQL in Docker
Next is the SQL Server image. Let’s install and run it in a container. I am going to use the command I have shown before:
Copy and paste this command into a command prompt and hit enter. Docker will now download and install the image into a container. This could take some time, depending on your Internet connection and local machine. Windows can ask if your firewall is allowed to use features. The name of this service is Docker Desktop Backend. You can accept this and allow access.
SQL Server in Docker Desktop
When Docker is finished downloading and installing you can go back to Docker Desktop. Under the tab Containers, you will see a new item. Docker gives random names to containers. Some can be funny, like this one: recursing_panini.
Apart from the name, which we will change later, a few other things are visible:
- Image
This is the image used for this container. - Status
This is now running, but it can also be stopped or paused. Sometimes it stopped because of an error. - Port(s)
The port(s) to which this container can be addressed to. - Started
The time this container is up and running. - Some buttons. From left to right:
Open with browser. Doesn’t do much with this container.
Open in a terminal. A good way to access the container in a Linux environment. It allows you to run certain commands.
Pause. Pause the container from running.
Restart. Think this is self-explanatory.
Stop. Stops the container from running.
Delete. Deletes the container, not the image(!).
Renaming container
Although recursing_panini has a nice ring to it, it doesn’t give me any clue of what it contains. A better name would be “sql_server001” or something. To change the name you need to open a command prompt and execute the following command:
It would be so much easier to do this through the Docker Desktop, but we can’t.
A command to rename an existing container is fun and all, but it’s better to prevent extra work, right? Well, there is an option in the command called “–name”. So next time when you download and install an image and create a container, add the –name option. Example:
Accessing the SQL Server
Having an image in a container is cool and all, but it’s even better if we actually use it. Since this is a SQL Server we need some software to access the database. I use Azure Data Studio. Although this is not ideal, it’s a great, little tool to access SQL databases. Let’s start this tool and create a new connection:
In some rare cases, you could get an error about the fact the connection has been established, but the handshake isn’t correct. Just wait a few seconds and then try to connect again. The service in the container wasn’t fully started.
When all is well and you are connected, you can not see the database – which is none since it’s a new container – and manage them or create new databases.
Conclusion
There you have it; Docker basics and an image installed in a container. Now, the world of Docker is way bigger than this and there are so many images available you can use.
I will create a more advanced tutorial about Docker soon. For now, you can use the knowledge of this tutorial for WordPress in Docker. Which allows you to run a local WordPress website.
TLTR;
Don’t like reading essays about the whole subject? Don’t worry! I have a step-by-step list for you.
Install Docker
- Go to https://www.docker.com/get-started/.
- Download the correct version of Docker for your machine.
- Open and run the installation file.
After installation, you can get an error about WSL 2.
- Download the fix here: https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
- Install the fix and restart Docker (better would be to restart your machine)
Install SQL Server in Docker
- Make sure Docker is running.
- Open a command prompt.
- Enter the following command:
- Change the name and password.
- Execute the command.
- After success, use a tool like Azure Data Studio to connect to the server.
