SQL Server in a Fedora Docker Container

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:


<span class="hljs-keyword">1&gt; SELECT</span> <span class="hljs-keyword">Name</span> <span class="hljs-keyword">from</span> sys.Databases;
 

 


2&gt; 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.

Using Software

2 Comments

  1. Lucas Vasconcelos

    I’m trying Fedora 25 (Workstation) from a VirtualBox VM .

    I tried the commands on this article and there are some issues:

    1) I’m new to Docker. so , after installing and start the service. I wasn’t able to run ‘ docker pull ‘ command . You need to create a ‘docker’ group and add your user to it in order to use docker as a regular user

    2) Using your handy script , the Docker image just hang out. Took me sometime until I realise that the container was exiting.

    using : $ docker ps -a

    I get this:

    728f0a47437a langdon/fedora-mssqlserver “/start.sh” 2 minutes ago Exited (1) 2 minutes ago mad_fermi

    Then I googled for a while and found this:

    using the command ” docker logs ”

    I’ve learned that:

    It required at least 3.5 GB of ram
    password should be at least 8 characters long

  2. Denis Savenko

    Thank you for article. But you don’t know how work docker-machine on Fedora? I have some problem with mounting volume. Docker itself work fine, but under docker-machine i can’t get access to my file in volume… 🙁

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions