Giter Club home page Giter Club logo

mssql-aspnet-docker-demo-app's Introduction

SQL Server on Linux + ASP.NET Core app demo

This project is a demonstration of how to build ASP.NET Core applications with SQL Server on Linux in Docker containers using Docker Compose.

This is the standard ASP.NET defaul app, but with the twist of using SQL Server in a container that the app connects to. Docker Compose is used to bring up the app and the SQL Server container together.

Running this demo

Prerequisites:

  • Git
  • Docker for Mac, Docker for Windows, or Docker Engine

To run this demo, first create a folder, cd into it, and then git clone the project.

git clone https://github.com/twright-msft/mssql-aspnet-docker-demo-app.git

Then run these commands to start the containers:

cd webapp
docker-compose up

Then you can access the web app at http://localhost:8000.

Details

A few key points about this demo app:

docker-compose.yml file

Note that there are two images - one for the web app (web) and one for SQL Server (db). The DB image uses microsoft/mssql-server-linux as the base. It is the SQL Server vNext image for running on Linux. Whenever you start a mssql-server-linux container, you need to pass in some environment variables:

  • ACCEPT_EULA=Y
  • SA_PASSWORD= For this demo the password is set to 'Yukon900' but you can change it if you want. Make sure you also change it in web container environment configuration if you do though!

The web container also has some environment variables that are passed in when the container is created. These are then used to build the connection string to connect to the SQL Server. Note that the server name is 'db'. This is the name that the SQL Server container is known by the web container as on the container network. Note the name of the DB is 'mydb'. You can change this to whatever you want. Entity Framework will attempt to create the DB if it doesnt already exist.

entrypoint.sh

The entrypoint.sh script in this demo does something special. It calls this command to tell Entity Framework to create the database if it doesnt exist already and then to populate the schema for the app into it:

dotnet ef database update

Note that this command is wrapped in a do/until loop to allow SQL Server time to come up and for the migrations to be run before continuing on and starting the app.

Startup.cs

This is the startup routine of the app. The key thing to point out here is how the environment variables are passed in and automatically added to the Configuration string array. The startup routine reads out the DB server name ('db' - see above), DB name ('mydb' - see above), login, password and then constructs the connection string.

Other than those small changes, this is just the standard default ASP.NET Core web application. From here, you can customize the app to do whatever you want and have SQL Server as the data store.

Don't forget to make sure you use persistence for your SQL Server container!

If you're not familiar with ASP.NET Core, read on!

Welcome to ASP.NET Core

We've made some big updates in this release, so it’s important that you spend a few minutes to learn what’s new.

You've created a new ASP.NET Core project. Learn what's new

This application consists of:

  • Sample pages using ASP.NET Core MVC
  • Bower for managing client-side libraries
  • Theming using Bootstrap

How to

Overview

Run & Deploy

We would love to hear your feedback

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.