LoginSignup
6
6

More than 5 years have passed since last update.

asp.net coreをdockerで動かしてみたメモ

Last updated at Posted at 2017-03-15

ASP.NET COREを試す

dockerでインストールして試してみる。
公式のサンプルはデプロイ用のようだったので開発環境のサンプルを見れるようにしてみる。

環境

  • docker1.12.3
  • ubuntu-16.04(仮想環境)
  • vagrant1.8.4
  • virtualbox5.1
  • windows10

フォルダ構造

dotnet
  - bin # dockerコマンド用シェルファイル
    - start.sh            # docker-compose run dotnet /bin/bash
    - container_build.sh  # docker-compose build
    - bash.sh             # docker-compose run dotnet /bin/bash
  - docker-compose.yml
  - dotnet
    - Dockerfile
    - project # アプリケーションプロジェクトディレクトリ
      - src
        - SampleWebApplication # yo aspnet generatorで作成
        + bin
        + Controllers
        + Data
        + Models
        + obj
        + Properties
        + Services
        + Views
        + wwwroot
        - .bowerrc
        - .gitignore
        - appsettings.Development.json
        - appsettings.json
        - bower.json
        - global.json
        - Program.cs # URLをdocker用に修正
        - README.md
        - SampleWebApplication.csproj
        - SampleWebApplication.db
        - Startup.cs
        - web.config
  - nginx # docker外からasp.netにアクセスできるようにプロキシとして利用
    - Dockerfile
    - default.conf 

各ファイルの設定

仮想環境設定。

# ansibleインストール用shell
$ansible_install = <<SHELL
    if ! type ansible > /dev/null 2>&1; then
      sudo apt-get update
      sudo apt-get -y install libffi-dev libssl-dev python-pip # package管理ツール
      sudo pip install --upgrade pip
      sudo pip install ansible           # provisioning ツール
    fi
SHELL

# virtual machine設定
Vagrant.configure(2) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.box_version = "2.2.9"

  # 繋がらないときは/etc/network/interfaces を確認。enp0s8に設定してやる。
  config.vm.network "private_network", ip: "192.168.50.10", auto_config:false

  config.vm.provider "virtualbox" do |vm|
    vm.memory = 2048
    vm.linked_clone = true
    vm.customize [ "modifyvm", :id, "--ioapic", "on"]
    # vm.gui = true # 起動が止まるときの確認用
  end

  config.vm.provision "shell", inline: $ansible_install
  config.vm.provision "shell", inline: <<-SHELL
    sudo ansible-playbook -i /vagrant/provision/playbooks/inventory/hosts /vagrant/provision/playbooks/site.yml -c local
  SHELL
end

docker-composeの設定ファイル。

docker-compose.yml
version: '2'
services:
  proxy:
    build: ./nginx
    links:
      - dotnet
    ports:
      - "80:80"
  dotnet:
    build: ./dotnet
    environment: 
      - ASPNETCORE_ENVIRONMENT=Development
    volumes:
      - ./dotnet/project/src/SampleWebApplication/Program.cs:/aspnetapp/project/src/SampleWebApplication/Program.cs

asp.netの設定

dotnet/Dockerfile
FROM microsoft/aspnetcore-build
WORKDIR /aspnetapp
COPY . .
WORKDIR /aspnetapp/project/src/SampleWebApplication
RUN dotnet restore
RUN bower install --allow-root
ENTRYPOINT ["dotnet", "run"]

※SampleWebApplicationはyo-generator aspnetで作成。
yo aspnet mvc bootstrap SampleWebApplication

アドレスとポートの指定。

dotnet/project/src/SampleWebApplication/Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace SampleWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseUrls("http://0.0.0.0:5001") // <-- 追記.ポート番号は任意。
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

プロキシ

nginx/Dockerfile
FROM nginx
COPY default.conf /etc/nginx/conf.d/default.conf

dotnetの50001ポートに80番ポートの通信を受け渡すプロキシ設定。

nginx/default.conf
server{
    listen 80;
    server_name 192.168.50.10;

    location / {

        proxy_pass http://dotnet:5001;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Host 192.168.50.10;
        proxy_set_header X-RealIP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

この時点でのソース

github

実行

コンテナを作成した後、asp.netをたちあげる。

./bin/container_build.sh # コンテナ作成
./bin/start.sh           # 実行

ブラウザでhttp://192.168.50.10 にアクセスすると、以下のような画面が表示される。

test.png

参照

dotnet docker
aspnet docker
docker compose
yo

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6