「マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
以下の手順で検証・評価した。
ASP.NET Core MVCアプリケーションの作成
> docker version > docker run hello-world
※ 後々、「from microsoft/aspnetcore」でググって、
コンパイル済みのASP.NET Coreアプリケーションを実行するための
公式のDocker Imageをダウンロードしていたことが解った。
https://hub.docker.com/r/microsoft/aspnetcore/
Docker側でライブラリのデバッグをする場合、ライブラリのPDBが、
「完全(full)」ではなくて、「ポータブル(portable)」である必要がある。
ホスト側のDBで動作させようとしたが、ハマりどころが多かったのでメモ。
Expressの場合は、SQL Server 構成マネージャーの設定も必要にある。
以下は、ASP.NET Core MVCのOpen棟梁テンプレートの変更点のスクショです。
(設定ファイル読み込み方法に「埋め込まれたリソース」を採用した場合)
手順1と同じ。
複数のコンテナを扱うので、そろそろ管理コマンド・ツールを理解する。
Update-Packageを実行する。
Program.csに以下の行を追加する。
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseUrls("http://0.0.0.0:5000/") // ← ココを追加 localhost や 127.0.0.1 だとダメ。 .Build();
Dockerfileファイル(Dockerfile)の編集
既定のDockerfileファイルを修正
FROM microsoft/aspnetcore:2.0 AS base WORKDIR /app EXPOSE 80 FROM microsoft/aspnetcore-build:2.0 AS build WORKDIR /src COPY WebApplication1.sln ./ COPY WebApplication1/WebApplication1.csproj WebApplication1/ RUN dotnet restore -nowarn:msb3202,nu1503 COPY . . WORKDIR /src/WebApplication1 RUN dotnet build -c Release -o /app FROM build AS publish RUN dotnet publish -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "WebApplication1.dll"]
FROM microsoft/aspnetcore:latest AS base WORKDIR /app EXPOSE 5000 FROM microsoft/aspnetcore-build:latest AS build WORKDIR /src COPY WebApplication1.sln ./ COPY WebApplication1/WebApplication1.csproj WebApplication1/ RUN dotnet restore -nowarn:msb3202,nu1503 COPY . . WORKDIR /src/WebApplication1 RUN dotnet build -c Release -o /app FROM build AS publish RUN dotnet publish -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "WebApplication1.dll"]
nginxフォルダにDockerfileファイルを作成する。
※ nginxフォルダは、Docker Composeファイル(docker-compose.yml)と同じ階層に作成。
FROM nginx:latest COPY default.conf /etc/nginx/conf.d/default.conf
nginxフォルダにdefault.confを作成する。
※ nginxフォルダは、Docker Composeファイル(docker-compose.yml)と同じ階層に作成。
server { listen 80; server_name example.com *.example.com; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $http_host; proxy_cache_bypass $http_upgrade; } }
server { listen 80; server_name localhost; location / { proxy_pass http://xxx.xxx.xxx.xxx:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $http_host; proxy_cache_bypass $http_upgrade; } }
※ xxx.xxx.xxx.xxxには、
links: - webapplication1
Docker Composeファイル(docker-compose.yml)の編集
version: '3' services: webapplication1: image: webapplication1 build: context: . dockerfile: /WebApplication1/Dockerfile
version: '3' services: nginx-proxy: image: nginx-proxy build: context: ./nginx dockerfile: Dockerfile ports: - "8888:80" links: - webapplication1 webapplication1: image: webapplication1 build: context: ./WebApplication1 dockerfile: Dockerfile ports: - "5000:5000"
Dockerfileには様々な設定が可能。
手順3に
コンテナを追加してみる。
がある。
を採用する。
手順3と同じ。
bind 0.0.0.0
redis: image: redis volumes: - ./redis/data:/data - ./redis/redis.conf:/usr/local/etc/redis/redis.conf command: redis-server --appendonly yes ports: - "6379:6379" restart: always
links: - redis
WSLから、redis-cliをインストールして確認する。
sudo apt-get install redis-tools
$ redis-cli -h 127.0.0.1 127.0.0.1:6379> set mystr1 "abc" OK 127.0.0.1:6379> get mystr1 "abc" 127.0.0.1:6379>
using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Http; namespace WebApplication1 { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Redisを設定 services.AddDistributedRedisCache(option => { option.Configuration = "redis"; option.InstanceName = "redis"; }); // Sessionを使用する。 services.AddSession(options => { // Set a short timeout for easy testing. options.IdleTimeout = TimeSpan.FromSeconds(10); options.Cookie.HttpOnly = true; }); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); // Sessionを使用する。 app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } }
public IActionResult Index() { HttpContext.Session.SetString("TestId", DateTime.Now.ToString()); return View(); } public IActionResult About() { ViewData["Message"] = "Your application description page. " + HttpContext.Session.GetString("TestId"); return View(); }
127.0.0.1:6379> keys * 1) "redis69ed5ead-0115-372b-8ef7-665bc769e747" 2) "mystr1"
psql -U postgres << "EOSQL" CREATE DATABASE postgres; \c postgres; -------------------- -- TABLE: Shippers -------------------- CREATE TABLE Shippers( ShipperID integer NOT NULL, CompanyName VARCHAR(40) NOT NULL, Phone VARCHAR(24), CONSTRAINT PK_Shippers PRIMARY KEY (ShipperID) ); -------------------- -- Sequence: ShipperID -------------------- CREATE SEQUENCE TS_ShipperID; -------------------- -- INSERT -------------------- INSERT INTO Shippers (ShipperID, CompanyName, Phone) VALUES(nextval('TS_ShipperID'), 'Speedy Express', '(503) 555-9831'); INSERT INTO Shippers (ShipperID, CompanyName, Phone) VALUES(nextval('TS_ShipperID'), 'United Package', '(503) 555-3199'); INSERT INTO Shippers (ShipperID, CompanyName, Phone) VALUES(nextval('TS_ShipperID'), 'Federal Shipping', '(503) 555-9930'); EOSQL
image: postgres volumes: #- ./postgres/data:/var/lib/postgresql/data - ./postgres/init:/docker-entrypoint-initdb.d environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=seigi@123 ports: - "5432:5432" restart: always
links: - redis - postgres
WSLから、postgresql-clientをインストールして確認する。
sudo apt-get install postgresql-client
$ psql -h 127.0.0.1 -d postgres -U postgres -c "select * from shippers" Password for user postgres: shipperid | companyname | phone -----------+------------------+---------------- 1 | Speedy Express | (503) 555-9831 2 | United Package | (503) 555-3199 3 | Federal Shipping | (503) 555-9930 (3 rows)
public IActionResult About() { string count = ""; using (var con = new NpgsqlConnection("HOST=postgres;DATABASE=postgres;USER ID=postgres;PASSWORD=seigi@123;")) { con.Open(); var cmd = new NpgsqlCommand(@"select count(*) from shippers", con); count = cmd.ExecuteScalar().ToString(); } ViewData["Message"] = "Your application description page. " + count + "件"; return View(); }
https://github.com/daisukenishino2/EvaluateAspNetCoreOnDocker/tree/master/MVC_Sample
https://github.com/daisukenishino2/EvaluateAspNetCoreOnDocker/tree/master/WebApplication1
外部から接続する場合、redis.confを修正する必要がある模様。
Tags: :.NET開発, :.NET Core, :Hyper-V, :仮想化