You might have seen the Magazine’s previous article on running SQL Server v.Next on Fedora.  That post talks about how to install it directly in to your Fedora installation. However, containers are an incredibly popular way to deploy apps especially if you just want to try it out.  Read on for how to run the SQL Server public preview in a Docker container on Fedora.

Docker

First you need to get docker installed. In short, run these commands:

sudo dnf install docker
sudo systemctl start docker

By default, you’ll need to run docker as root or with sudo, but you can change that if desired. Check out the Fedora Developer page on docker for more details and more elaborate setups. You may also be interested in other things you can do with docker and Fedora based on one of my past posts.

Setting up SQL Server

To pull the Docker image with SQL Server, run this command:

docker pull langdon/fedora-mssqlserver

You will see it download all the layers. The process goes faster if you’ve pulled a Fedora docker image in the past. To run it, use this command (copy and paste makes this easier):

docker run -d -t -p 1433:1433 -v $DATADIR:/var/opt/mssql/data:rw,z -v $LOGDIR:/var/opt/mssql/log:rw,z -e ACCEPT_EULA=$ACCEPT_EULA -e SA_PASSWORD=$SA_PASSWORD langdon/fedora-mssqlserver

To explain this long command a bit:

  • -p 1433:1433 — This is the port exposed from the container to connect to SQL Server. This command binds 1433 on your machine to the container port. You can also use an arbitrary port by just using -P if you’re using 1433 for something else.
  • $DATADIR — Either replace this with a local directory, or set an environment variable for where SQL Server should store its data.
  • $LOGDIR — Just like $DATADIR except for logs.
  • $ACCEPT_EULA — You need to accept the Microsoft license agreement to use this software.
  • $SA_PASSWORD — The default admin user in SQL Server is SA and this is where you set its password. Probably better to replace this than set an environment variable.

Technically speaking, you don’t need to pull the image first. I believe it makes the explanation simpler. I also have a handy script you can run to make this easier, but you still have to provide the password.

Check to ensure your container is running:

docker ps

There should be an entry similar to this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
381a922c2a3e langdon/fedora-mssqlserver "/start.sh" 3 seconds ago Up 2 seconds 0.0.0.0:1433->1433/tcp angry_jepsen

Connecting to SQL Server

Finally, you can connect to the container. To get the tools, run these commands:

curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo
dnf install -y --allowerasing mssql-tools

Note that –allowerasing may be needed depending on your package set. The mssql-tools package requires a different version of unixODBC than is shipped in Fedora.

Now you have a sqlcmd utility you can use to connect like this:

sqlcmd -U sa -P $SA_PASSWORD

For $SA_PASSWORD use the password you provided when you started the container running. Finally, to prove SQL Server is up and working, use these commands:

1> SELECT Name from sys.Databases;
 

 


2> GO

You’ll see the default tables. You can read more in this Getting Started from Microsoft.

More information

If you’re looking for more information on SQL Server on Linux, refer to the official website.  If you’re interested in other ways to use Docker, there are several interesting docker posts in Fedora Magazine. I also have this all in a Github repo, including a Vagrantfile to try this process in a controlled environment. Please check it out and submit a pull request if you find any problem.


Image courtesy of Boba Jovanovic — originally posted to Unsplash.