<Tech>Brunch


SQL Server in Docker Container + VS Code

It’s amazing to see the shift in containerizing our existing applications with Docker. As the value add they say, Docker enables developers to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container, which can run virtually anywhere. Now at present, it feels awesome to see that softwares/applications which were tied to a particular platform only, now they are being opened to use for every platforms via Docker.




I am working on a project which for some reasons gave us choice to go with Microsoft SQL Server, as the backend database service provider. Afterall, we are working on Microsoft platform, and it’s supported apps. For me, configuring my codebase on my mac machine became a nightmare.

Thanks to Docker. I am able to run MS SQL Server in container, and manage my databases too with a light weight yet one of the most powerful tool that ever existed, VS Code from Microsoft. There is not a single day when I dont thank Microsoft for keeping this powerful tool, VS Code, as Open Source.

Let me begin with how I spinned up my MS SQL server. I created a simple docker-compose file, as I am not a big fan of running containers via command line configurations. My docker-compose file goes below:

1
2
3
4
5
6
7
8
9
10
11
version: "3.2"
services:

sql-server-db:
container_name: sql-server-db
image: microsoft/mssql-server-linux:2017-latest
ports:
- "5033:1433"
environment:
SA_PASSWORD: "defaultU53r!"
ACCEPT_EULA: "Y"

My compose file says my container name will be ‘sql-server-db’, and I will be exposing to port 5033 on my host machine. I accepted the essential aggrement via flag ‘Y’, and provided password for the ‘sa’ user for SQL Server.

1
docker-compose up -d

The above starts my container in detached mode.

Now I need to connect to my SQL Server running in Docker via VS Code. For this we need to make use of a VS code plugin: SQL Server(mssql).
Once installed in VS Code, we would run a command palettte inside the editor and provide connection details for our DB server running in container.

Steps for VS-Code mssql plugin configuration:

  • Server name: The SQL Server instance name. Use localhost to connect to the local SQL Server instance on your machine. If connecting to a remote SQL Server, enter the name of the target SQL Server machine or its IP address. If you need to specify a port for your SQL Server instance, use a comma to separate it from the name. For example for a local server running on port 5033 you would enter localhost,1401.
  • [Optional] Database name: The database that you want to use.
  • User name: Enter the name of a user with access to a database on the server. You can use SA as the user.
  • Password (SQL Login): Enter the password for the specified user. For SA, you already have provided the password detail in docker-compose.yml file.
  • Save Password?: Type Yes to save the password. Otherwise, type No to be prompted for the password each time the Connection Profile is used.
  • [Optional] Enter a name for this profile: The Connection Profile name. For example, you could name the profile localhost profile.

Its done! Navigate to SQL Server plugin added, and there you will be able to see the Database added.