Home » Create a SQL Server container with Podman

Create a SQL Server container with Podman

by Vlad Drumea
0 comments

In a previous post I’ve covered how to use the new sqlcmd to spin up a SQL Server Developer Edition container, in this post I cover how to achieve that just by using Podman.

Prerequisites

Make sure you have WSL and Podman installed. You can use the same steps from my Create a SQL Server Developer edition container using sqlcmd post.

Note, if you already have Docker set up, you can follow the steps from this Microsoft Learn article.

Create the SQL Server container in Podman

I’ll be using the SQL Server 2022 Docker container image as an example, but the commands apply to 2019 as well.

The following command pulls, if not already in your local cache, the latest SQL Server 2022 container image and spins up a container with the following configuration:

  • container name – sql2022
  • hostname – sql2022
  • sa password – S0MeP4sS!
  • the instance in the container will listen to port 1433 which will be mapped to your machines port 1433
PowerShell window PS C:\Users\Vlad> podman run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=S0MeP4sS!" <code> >> -p 1433:1433 –name sql2022 –hostname sql2022 </code> >> -d mcr.microsoft.com/mssql/server:2022-latest 9a8d8e37c3e830f772f6ce395fde69d5b8b574bee8191366ac96cd0a3e3d4e42 SQL Server container podman” class=”wp-image-4073″/></figure> <br> <p>And then you can check the status of the newly created container via the following command.</p> <div class=

Connect to and interact with the container

To connect to the container and run commands from inside it you can use the podman exec command together with the container name or ID.

Connect as root

I recommend doing this especially if you want to create symlinks in order to fix the issues caused by the new path used for the new version of the SQL Server command-line tools for Linux. You can read more about that in this blog post.


And to create the symlinks, while you’re connected as root.


Might as well upgrade and install curl, because it will come in handy later.
Note that you’ll be prompted twice to confirm (press Y and Enter).


Connect as a non-root user

For anything else you should interact with the container as the non-root user named mssql.


Connecting to the SQL Server instance hosted in the container

From inside the container

Note that the -C option is needed to avoid getting the error:0A000086:SSL error message.

PowerShell window mssql@sql2022:/$ sqlcmd -S localhost -U sa -P S0MeP4sS! -C 1> SELECT @@SERVERNAME, @@VERSION; 2> GO sql2022 Microsoft SQL Server 2022 (RTM-CU14) (KB5038325) - 16.0.4135.4 (X64) Jul 10 2024 14:09:09 Copyright (C) 2022 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 22.04.4 LTS) <X64> SQL Server container podman

From the container’s host using SSMS

To connect from SSMS you can use the local IP 127.0.0.1 or you can use the hosts file (C:\Windows\System32\drivers\etc\hosts) to map the container name to the local port.
You should normally be able to also use localhost as the server name, but for some reason that doesn’t work for me in SSMS.

I’ve added the following entry in my hosts file:

And connected from SSMS to both the local IP and to SQL2022Container and this is how the connections look in Object Explorer.


Restore a sample database

It’s all kinda pointless if the instance just sits there empty with no database to play with.
So, I also restore the AdventureWorks2022 database.

The instance root directory doesn’t contain a backup directory.

Terminal window showing the result of ls -alth /var/opt/mssql which shows that there's no backup directory in that path

So, first I have to create the backup directory.


And then proceed to download the backup file in the previously created directory.


Beyond this point I show how this can be done via the terminal, but this can also be done via SSMS without any issues or extra requirements.

Before I can restore the backup, I have to see the logical file names.

Note that I’m running the query like this so that I can pipe it to tr and cut in order to make the result easier to read.

The result of the above command showing the logical names of the files found in the backup file.

Looks like the data file’s logical name is AdventureWorks2022 and the log file’s is AdventureWorks2022_log.
Resulting in the following restore command.

The restore command, its outcome and the state of the AdventureWorks2022 database after the restore. SQL Server container podman

And now I can freely interact with the AdventureWorks2022 database from both sqlcmd and SSMS.

Stop and Start the SQL Server container

If you want to stop the container until the next time you need it you can simply stop it.

If the stop was successful, the status of the container, when checking with pdoman ps --all, should be Exited.
The status is also shown in the Podman Desktop GUI.


Next time you need the container you can start it from the GUI, using the Start/Play button, or via the start command.

Removing the SQL Server container

When the container has outlived its usefulness you can simply delete it from the GUI, using the delete button, or via the following command:

Conclusion

Once you get the hang of it, it’s a fairly simple task to spin up, use, and dispose of a SQL Server container using Podman.

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.