Page 10 - MSDN Magazine, June 2019
P. 10
Data Points JULIE LERMAN
EF Core in a Docker Containerized App, Part3
In the past two articles, I’ve walked you through building a small ASP.NET Core API using Entity Framework encapsulated in a Docker container. In the first article, I included a SQLite database within the container. In the second, I targeted an Azure SQL Data base that can be reached from anywhere. The bigger challenge in this case was how to keep secrets (such as database passwords) secret and configurations (such as connection strings to develop ment or production databases) fluid. This involved learning how to leverage the ability of Docker Compose to read environment variables from the host.
There’s another very interesting path for persisting data, which is to have both a database server and the data in containers. I’ve written about using SQL Server for Linux in Docker containers in earlier columns, such as the one at msdn.com/magazine/mt784660. I really like the way Microsoft’s Steve Lasker expresses the perspec tive of using SQL Server in containers for dev and test:
Spinning up SQL in a container gives developers the ability to iso- late/simulate an environment. Including a different version of SQL Server. Being able to start a test from the exact same state, each time, is goodness. Especially when testing a DB upgrade, instead of figur- ing out how to roll back the DB update, just restart the container.
In the article on SQL Server for Linux, I interacted with the database using only sqlcmd at the command line. In a more recent article (msdn.com/magazine/mt832858), I used Azure Data Studio to interact with a containerized SQL Server and database. Now I want to integrate a dedicated, containerized SQL Server into my API development. I’ll show you how to accomplish this in Visual Studio, continuing with the sample I used in the previous column.
Quick Review of the API
The API is simple with a single twoproperty Magazine class (Id and Name), a Magazines controller and an EF Core DbContext that uses migrations to seed new databases with four magazines.
The next iteration of the app, in Part 2 of the series (msdn.com/ magazine/mt833438), introduced container orchestration via a docker compose file. While that was a singlecontainer solution that didn’t truly need orchestration, dockercompose provided the ability to pass environment variables into the container without storing the values directly in the image. But even with singlecontainer solutions, a compose file can be used to run the app on a Docker Swarm or Kubernetes cluster, giving you scale and selfhealing.
The MagazinesContext class used the environment variables to inject a password to the database connection string.
Moving to a Containerized Database
The goal of this article is to target a SQL Server for Linux server and a database in containers on my development machine. This will mean updating the existing dockercompose file and updating connection strings within the app. In all, not a lot of effort. But if you’ve never done it before, it’s certainly nice to have someone show you the way!
The biggest changes will be to the dockercompose.yml file. And what’s super cool is that Docker will take care of the hardest pieces for you, simply by reading the instructions relayed in dockercompose. Because all of this starts in Visual Studio 2017, the included Tools for Docker will also participate when you run or debug from Visual Studio. If you’re using Visual Studio 2019 (which, as I’m writing this, was officially released only yesterday, so I’ll continue this series in 2017), the tooling is built in and the experience should be the same.
Here’s the previous version of the dockercompose.yml file:
version: '3.4'
services: dataapidocker:
image: ${DOCKER_REGISTRY-}dataapidocker build:
context: .
dockerfile: DataAPIDocker/Dockerfile environment:
- DB_PW
This dockercompose file was only managing one image, defined in the dataapidocker service. The tooling will make sure
Figure 1 The Docker-Compose File with Changes to Include an mssql/server Container
version: '3.4'
services: dataapidocker:
image: ${DOCKER_REGISTRY-}dataapidocker build:
context: .
dockerfile: DataAPIDocker/Dockerfile environment:
- DB_PW depends_on:
- db db:
image: mcr.microsoft.com/mssql/server environment:
SA_PASSWORD: "${DB_PW}"
ACCEPT_EULA: "Y" ports:
- "1433:1433"
Code download available at msdn.com/magazine/0619magcode.
6 msdn magazine