Code Monkey home page Code Monkey logo

docker_dot_net_core_web_application's Introduction

Docker .NET Core Web application with MS SQL connection

A Visual Studio sollution prepared to be used (plug and play) with Docker. It will create 2 contaners, one for the web server and one for the SQL server (both running on Linux).

To whole tutorial about creating this solution is available in my blog post .NET Core Web App Docker and MSSQL Docker


Connection to the SQL server (separate container)

Code: A code example of a SQL connection (MSSQL is located in another container) is located in the following file: Controllers/HomeController.cs. The important lines are:

//CONNECTION STRING TEST - BEGIN
/*
SqlConnection cnn;
var connection = @"Server=db;Database=dean;User=sa;Password=TestPassZXY017+;";
cnn = new SqlConnection(connection);
try
{
    cnn.Open();
    cnn.Close();
}
catch (Exception ex)
{

}
//*/
//CONNECTION STRING TEST - END

Server=db: "db" is the name of the image instance (container), which is defined in the docker-compose.yml file below.

All the mentioned lines above are commented out, bacause at the first project run you need to create a DB first (used DB name in this example is 'dean'). When you will create a DB, uncomment those lines and at the next application start, the C# code above should establish a connection to the MS SQL server without any errors.

Insight into docker-compose.yml

version: '3'

services:
  webapplication1:
    image: webapplication1
    build:
      context: ./WebApplication1
      dockerfile: Dockerfile
  db:
    image: "microsoft/mssql-server-linux"
    environment:
      SA_PASSWORD: "ZacetnoGeslo2017+"
      ACCEPT_EULA: "Y"
    ports:
    - "1433:1433"

webapplication1 is a container for the Web Server

db is a container for hodling the MS SQL Server

docker_dot_net_core_web_application's People

Contributors

deanpodgornik avatar deanpodgornikbs avatar

Watchers

 avatar

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.