How to build and run a SQL container using a DACPAC file

C:\Dave\Storey
ITNEXT

Introduction

Ok so recently I was working on a code project which had a hard dependency on a SQL database. Now normally I’d try and mock things and add abstractions etc. but for what was a relatively simple service, I wondered if there might be a better way to approach this.

The project I was working on already contained a .sqlproj project which generated a dacpac file to enable developers to easily keep their local or remote SQL instances in sync (if you are unfamiliar with either of these concepts please checkout the dacpac docs here and the sqlproj docs here). This meant that if I built the sqlproj I got a dacpac… but how to get that to deploy into a container?

Where to start

So I know about dockerfiles, and I know about dacpac files, but how can I munge these two things together?

Basically what I wanted was a way of doing a docker build, using the Microsoft provided base image for SQL2019, throw into that database my dacpac spec so my tables etc were created, and then have that container definition on my machine ready to spin up and spin down whenever I wanted.

Luckily I stumbled across a post by Ken Muse on Wintellect and while it didn’t work for my use case (because it was SQL2017 and had a few bugs) it did give me a good place to start. So huge kudos to Ken 👏👏👏👏👏

The Container Definition

Breaking it down

Ok so there are a number of steps here, let me break it down:

  1. Firstly I use the base SQL2019 image provided by MSFT
  2. Next I elevate my permissions to root to enable me to do some apt-get package installations
  3. Ok so on line 11, I go to the aka.ms link for the linux distro of sqlpackage . If you are not familiar with this tool, please check the docs here but essentially it is a CLI tool that allows you to do dacpac deployments into SQL.
  4. Line 19 I copy in my dacpac file to the container layers
  5. Lines 22–27 setup some of the default options required for the SQL container to startup. More info can be found in the docs here
  6. Line 31+ is where the real magic happens.
  7. Firstly we start the sqlserver process, and wait for it to inform us that the broker has started.
  8. Next we invoke our downloaded sqlpackage cli binary and tell it to do a Publish to localhost and to use our dacpac file.
  9. Line 33> tidy up the image.

Running the container

We can easily build and then run this container by invoking the following commands:

$ podman build ../. --build-arg PASSWORD="<YourStrong@Passw0rd>" -t mydatabase:1.0 --no-cache$ podman run -p 1433:1433 --name sqldb -d mydatabase:1.0

I’m using podman here as my container engine, but the same commands should work perfectly in docker too 😃

(Notice I used the --no-cache flag here because i ran into problems where podman would cache the dacpac file, and not notice if it had been changed/rebuilt 🙈)

Differences between mine and Ken’s solutions

For those of you interested in the differences between the blog Ken wrote and my version:

  • I had to elevate permissions to root because the apt-get didn’t work without it. I could have probably dropped them back down, but didn’t bother.
  • The sqlpackage uri used by Ken didn’t seem to want to run properly in the container with a dacpac built in netstandard2.0. It kept complaining about openssl not being the correct version, bumping it to this never link fixed.
  • Did some tidying up of the sqlpackage cli to keep the image a bit smaller.

Conclusion

I really love the isolation and protection that containers give me. Especially when running service tests because i can pollute my local database container, tear it down and start again in a few minutes. I can also see a future where I get these containers running on my build agents so I don’t have to have infra hanging around for CI/CD builds.

I hope this maybe helps someone else in the future 👋

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by C:\Dave\Storey

Software engineer & lover of all things code. Too much to learn and so little time. Currently working at Trainline London.

Responses (1)

Write a response