Home Linux-based SQL Server in Docker Containers
Post
Cancel

Linux-based SQL Server in Docker Containers

I have been really liking what Docker can offer and have been experimenting with it whenever possible. In the July issue of MSDN magazine, Julie Lerman wrote about using SQL Server in a Docker container.

Article: https://msdn.microsoft.com/en-us/magazine/mt784660.aspx

I had been going through the article carefully but using a Windows machine. When it came time to interact with the SQL Server by using the SqlCmd command line tool, I was getting stuck. I was not able to initiate SqlCmd correctly (again, on a Windows machine) based on the instruction from the article.

For background, I used the following command to start up my Sql Server container.

1
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourPassword>" -e "MSSQL_PID=Developer" -p 1401:1433 --name ericsqlcontainer -d microsoft/mssql-server-linux

I had switched my locally installed Docker to use Linux Containers instead of windows ones and I was able to get an instance of SQL Server up and running as a Linux Docker container.

The trouble for me started after starting up the Sql Server container and trying to use SqlCmd. If I tried to connect like it is written in article, I could not connect. I then tried to connect using the credentials of the fully installed Sql Server Developer Edition on my laptop. That worked! I was able to connect SqlCmd to a database server, however, it was not the server I wanted. I clearly was no longer running within the docker container of my new linux-based Sql Server instance.

I discovered that I needed a couple of more steps to use SqlCmd in the container that I just started up.

1
docker exec -it ericsqlcontainer “bash”

I needed to use the docker exec -it command to start an interactive bash shell inside my running container. The name “ericsqlcontainer” is the name I gave my container when I started it up. You can also use the whole or part of the alpha-numeric code that docker gives you when your container starts up.

After starting the bash shell, you can then go after SqlCmd.

1
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourPassword>'

When you get your prompt after this, you will be able to run sql commands through SqlCmd and surprise, surprise……you are in your docker container!

This post is licensed under CC BY 4.0 by the author.