Skip to main content

Using Postgres on Docker with EF7 and ASP.NET 5

In an effort to get a better understanding of the ASP.NET framework, I've started a simple toy project from scratch.  I've really been enjoying how easy it is to plug everything together with Microsoft's reboot of the framework.  Just spin up a project.json file and get going, it's pretty impressive how much you can get done with so few files.
I think the community has been doing a great job with blog posts, so I've not had many opportunities to contribute any unique content that isn't already covered.

While working through things today however, I thought it might be nice to try and prove out how Entity Framework 7 can work with non-Microsoft databases and containers.  The most appealing at the time of writing being postgres thanks to the awesome work by the people over at npgsql.
Because I've been developing in Windows using Visual Studio, the first thing that would be nice is to not have to spoil my system with a database installation.  No surprise, docker seems like it would make a great ally for that!

So, for this blog post, I'd like to take you through setting up a docker container running postgres and then adding support to your ASP.NET 5 project to run migrations against it.

Docker & Database Setup

It's easy to get started with Docker Toolbox, just download and run the installer.  One snag I encountered was that I had Hyper-V enabled on my machine.  This resulted in the docker quickstart terminal giving me some errors about virtualization not being enabled on my machine.
Under the hood, docker toolbox uses VirtualBox which cannot run with Hyper-V.  It's not much at the moment, but I have taken the time to ask that they make docker machine's Hyper-V support a little more user friendly.  I encourage you to head over there and give it a +1 if you agree.

With toolbox downloaded, create a file called local-development.yml at the root of your project.  Take a copy of this code which defines a container configuration based on the official postgres docker image.

If you're running on OSX or Windows and are like me, you'll probably also want the server to behave as though it was on localhost.  You can run this to forward all traffic destined for 5432 on your machine to the docker vm:

docker-machine stop default
VBoxManage modifyvm "default" --natpf1 "app,tcp,,5432,,5432"
docker-machine start default

(Note: VBoxManage may not be on your path in Windows, but it is in the program's installation folder in Program Files.)

Finally, you can try starting the database server by simply typing the following in a docker-enabled terminal at the root of your project:

docker-compose -f local-development.yml up

After which point, you should be able to use any postgres client to connect to the database.  I suggest DataGrip - formerly 0xdbe - made by the folks over at JetBrains.

Project Setup

So far, you've got a database, but now what steps need to be taken to get ASP.NET talking to the containerized database?

Really, things couldn't be any simpler.  First off, you'll want to open up project.json and ensure you have the lines from this gist present.

Make sure you get a package restore done automatically via your tooling or manually by running dnu restore.

Next, you're going to want to make sure you've enabled EF7 in your project's Startup.cs file.  Here's a copy of the method and what you have to call to get that done.  Mix in what I have with what you've got as needed.

The last step, which I've left to this point deliberately is to add the configuration for postgres to your DbContext.
This file is usually going to be heavily tailored to your project.  So I'm going to try and avoid anything to do with your schema.  The file I've shared with you is just barebones enough to have identity and a crude configuration string.  Once you've mixed in my example code with your working DbContext, you should have everything you need to try generating a migration and running it.


When I started learning about frameworks, running my first migrations was an oddly rewarding experience.  For me, it completed the circle of understanding and represented the moment that I felt I was able to apply what I had learned to create real solutions.

My hope with this blog post is that you'll feel the same about ASP.NET today as I do.  These next steps are easy.  All you need is a terminal with dnx working at the root of your project:

dnx ef migrations add Initial
dnx ef database update

Which, in my case, got me a new file in my project, as well as this:

And that's it!  You now have access to all the functionality of EF7 and ASP.NET 5.

Keep in mind that while ASP.NET 5 is now RC, there's always some remote chance that something could change.  The one thing I can easily predict now is that any use of dnx will be changing to dotnet.
Beyond that though, this should be a fairly safe blog post.  Most of it concentrates on getting you going with a postgres container.

Final Thoughts

There are a few more things when it comes to containerization that I'd like to get working, mainly adding an entry in local-development.yml for my ASP.NET app.  Although simulating a live environment locally is something that can easily swallow days of effort.  It's usually enough to say that ASP.NET takes care of a lot of environment abstraction for you.
Over time, I feel like if I need to containerize the app itself, I'll just make that a deployment detail and not concern my development workflow with it.
We'll see though, as using containers for actual development can sometimes be quite useful when onboarding new team members.

Hopefully I haven't accidentally left anything out, but if you have any issues getting to a point where you've got your migration generated, let me know in a comment!

Popular posts from this blog

Amazon in Winnipeg?

Late last week, Amazon put word out that they're starting to look for a city to locate a second headquarters.  The fuel this announcement provides has the hype train in Winnipeg going so fast, it's missing stops.

I'm curious though.  As you've been digesting this exciting news, who have you spoken or listened to?
Was it this guy and his party's historic theft of public infrastructure (pdf) and incompetence with digital strategy? Or how about this guy, who maintains that Winnipeg doesn't need a proper rapid transit option?  You know, the kind that might actually catch the eye of handsome Amazon who now has Winnipeg trying to be the belle of the ball.

Runner up articles are Winnipeg's inability to join the 21st centry on active transport and the compounding of the aforementioned digital strategy mistakes.
Stop Listening to These Guys They are not technology experts. At worst, they're unabashed self promoters. At best they're conduits for very bad pol…

The Cons, Winnipeg's New Splash Pads

I never had the chance to be ungrateful, the new splash pad at Kildare and Wabasha isn't a splash pad at all.  It was taken away from us and replaced with an "aquatic park", a little bit of wordsmithing designed to gloss over the fact that an open piece of our community has been replaced with yet another closed gate.

As I write this post now, I can hear it already: "Taken away, what?! It's a new water park, you're so..."
Sure, some might reach for that tired recrimination, which is why I started this blog post by dismissing such a premise.  To be fair however, I offer the response: Don't spoil this discussion with nonsense.
You see, I was grateful before the renovations happened.  The communal service on offer was adequate and I never complained about it or saw it as flawed.  Don't believe me? Here, this is a cute google-generated animation of my son enjoying the splash pad in 2014.

Today we took the kids to see if we could spend some time at th…

Winnipeg Devs! Let's get WPL to carry O'Reilly Safari!

Short and sweet. I discovered the other day that the Toronto Public Library actually gives members access to O'Reilly Safari -- I was instantly jealous! You probably already know that this is a pretty good service chock-a-block full of great resources.

The good news is that we have a way to request new content from the Winnipeg Public Library.  This includes digital content, so I've done exactly that!
If you're interested in adding your voice to mine, I've put together a letter that you need to copy/paste and enter into their simple form. Here's a copy that you can read as part of this post:

This is a request for ongoing and full access to the complete O'Reilly Safari catalogue via a Winnipeg Public Library card. I am filing this under "Magazine Purchase" as it's not a single book, but an ongoing service. Following is a list of the most prominent benefits of adding this service to the collection:
O'Reilly produces extremely high quality conten…