Build an ASP.NET Core Web API environment on Docker (VSCode) Part 2

procedure

1. Creating a Web API

Created by referring to Creating Web API with ASP.NET Core. Details of WebAPI are omitted in this article.

[Created Web API]

AuthorModel.cs



namespace webapisqlsv1.Models{
  public class AuthorModel {
      public int Id { get; set; }
      public string Author { get; set; }
      public string BookTitle { get; set; }
  }
}

AuthorContext.cs



using Microsoft.EntityFrameworkCore;

namespace webapisqlsv1.Models{
  public class AuthorContext:DbContext{
    public AuthorContext(DbContextOptions<AuthorContext> options)
      :base(options){

    }

    public DbSet<AuthorModel> TblAuthor {get; set;}
  }
}

AuthorController.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using webapisqlsv1.Models;

namespace webapisqlsv1.Controllers
{
  [ApiController]
  [Route("[controller]")]
  public class AuthorController : ControllerBase
  {

    private readonly ILogger<AuthorController> _logger;
    private readonly AuthorContext _context;

    public AuthorController(ILogger<AuthorController> logger, AuthorContext context)
    {
      _logger = logger;
      _context = context;
    }

    [HttpGet]
    public IEnumerable<AuthorModel> Get()
    {
      return _context.TblAuthor.ToArray();
    }
  }
}

Startup.cs



//Only additional parts
public void ConfigureServices(IServiceCollection services)
{
   services.AddDbContext<Models.AuthorContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("AUTHOR01")));
}

appsettings.json


  "ConnectionStrings":{
    "AUTHOR01" : "Data Source=db1;Database=Author;User ID=sa;Password=Password0!;"
  }

2. Creating a Docker environment

Dockerfile



FROM mcr.microsoft.com/mssql/server:2017-latest

ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=Password0!
ENV MSSQL_PID=Express
ENV MSSQL_COLLATION=Japanese_CI_AS
ENV MSSQL_LOG_DIR=/MSSQL/log
ENV MSSQL_DATA_DIR=/MSSQL/data
ENV MSSQL_BACKUP_DIR=/MSSQL/backup
ENV MSSQL_MASTER_DATA_FILE=/MSSQL/master.mdf
ENV MSSQL_MASTER_LOG_FILE=/MSSQL/mastlog.ldf

EXPOSE 1433

Add depends_on and add DB settings

docker-compose.yml



# Please refer https://aka.ms/HTTPSinContainer on how to setup an https developer certificate for your ASP .NET Core service.

version: '3.4'

services:
  webapisqlsv1:
    image: webapisqlsv1
    container_name: apisqlsv1
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 5001:5000
    depends_on: 
      - db
  db:
    image: mssqldb
    container_name: sqlsv1
    hostname: db1
    build: 
      context: ./db/
      dockerfile: Dockerfile
    volumes: 
      - C:\Docker\SQLServer\DB:/MSSQL  
    ports: 
      - 1433:1433

3. Command execution & result

docker-compose up

VSCode_20201227-3.png

VSCode_20201227-2.png VSCode_20201227-1.png

Click here for Source

Recommended Posts

Build an ASP.net Core Web API environment on Docker (VSCode) Part 1
Build an ASP.NET Core Web API environment on Docker (VSCode) Part 2
Build an environment with Docker on AWS
Build an Ultra96v2 development environment on Docker 1
Build PlantUML environment with VSCode + Docker
Build Clang x VSCode on Docker (1)
Build Unity development environment on docker
Build Redmine code reading environment on Docker
Build an environment of "API development + API verification using Swagger UI" with Docker
Build a Laravel / Docker environment with VSCode devcontainer
Build Java x Spring x VSCode x Gradle on Docker (1)
Building OpenPose (Pytorch_Realtime_Multi-Person_Pose_Estimation) environment on Docker: training part
Build a Laravel environment on an AWS instance
[Ruby on Rails] Let's build an environment on mac
Build a development environment for Docker, java, vscode
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Build a Doker-based development environment on Windows 10 Home 2020 ver. Part 1 Until WSL2-based Docker build
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
[Docker] Build an Apache container on EC2 using dockerfile
Build Rails (API) x MySQL x Nuxt.js environment with Docker
I tried to build an environment using Docker (beginner)
I tried to build the environment of WSL2 + Docker + VSCode
WSL2 + VSCode + Docker development environment
Build docker environment with WSL
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Build a Docker-based development environment on Windows 10 Home 2020 ver. Part 2 VS Code should make the Docker development environment comfortable
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build environment with vue.js + rails + docker
Build a XAMPP environment on Ubuntu
Rails on Docker environment construction procedure
Build docker + laravel environment with laradock
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Create a java web application development environment with docker for mac part2