In yesterday’s blog post we pulled SQL Server images in preparation for today’s blog post where we create containers from those images. If you haven’t already, get Docker installed and follow the instructions here to get at least one image on your machine.
If you’re interested in why I’m talking about containers all week, read this. I’m using all PowerShell commands to control Docker.
Docker Run
You can use ‘docker create’ to create an image and then start it up. However, we can just get started running a container from one of the images we downloaded yesterday. We can just simultaneously create and start the container using ‘docker run’:
docker run -e 'ACCEPT_EULA=Y' `
-e 'SA_PASSWORD=$cthulhu1988' `
-p 1433:1433 `
--name dockerdemo `
-d mcr.microsoft.com/mssql/server:2017-latest
Let’s break this down a bit so you know what you just did. The two ‘-e’ statements are setting environment variables. The first is accepting Microsoft’s end user license agreement, EULA. The second is setting the SA password. By default, we’re running a Developer Edition of SQL Server here. If you want to, you can change to a version that you have a specific license for using the MSSQL_PID environment variable. Documentation for that is located here.
Next, we’re setting a port, -p. I’m using the default port right now, 1433, but I could customize this. Then, I’m creating a name for my container, dockerdemo. -d tells Docker to run the container and output the ID value. Finally, we supply an image for the container to be created from.
You should see an output similar to this:
To check the status of your container, run this command:
docker ps
Assuming you used the default port, all you need to do to connect to this SQL Server instance that is now running on your system is use your IP address for the machine. You can immediately log in as SA using the password you supplied. I connected to mine through Azure Data Studio and it looks like this:
You can get rid of the container you’re running by using this command:
docker rm DockerDemo
That will leave the image in place, but remove the container. You can also directly control the container:
docker stop DockerDemo
Or, if you want to run it again:
docker start DockerDemo
If you’re not sure what containers you have, you can use this command to list all currently existing containers:
docker ps -a
Conclusion
At this point, you should have a container up and running. Inside the container, it’s just SQL Server, so you can do what you like. Assuming you used the default images as I showed, it will be SQL Server running on Linux, so take that into account when you start to create databases or run RESTORE commands. It will require Unix paths. Tomorrow, I’ll show how to create a container and create a volume at the same time so we can upgrade a database.
I have several precon events coming up. If you’re interested, please click on the following links to see the details on topics:
SQLSaturday Columbus Precon, June 7 2019, Columbus OH
SQLSaturday Indianapolis Precon, Friday August 16th, 2019. Click here now to register.
SQLSaturday Oslos Precon, Friday August 30th, 2019. Click here to register.
[…] Grant Fritchey continues a dive into containers. First up is running a Docker container: […]
[…] the previous two posts on containers I showed how use Docker commands to get an image and create a container. This time, we’re going to create a container again, but, we’re also going to create a […]