1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker上にASP.net Core WebAPI環境を構築する(VSCode)その1

Last updated at Posted at 2020-12-26

手順

dotnet new webapi -o ./WebApi1
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build app
COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .

ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000

ENTRYPOINT ["dotnet", "WebApi1.dll"]

  • コマンドパレットを開き「Docker:Add Docker Compose Files to Workspace...」を選択。
    何回か選択肢が出るが最初は「.NET:ASP.net Core」を選択すること。後2つは任意。

VSCode_20201226-1.png

  • 下図のメッセージが出た場合はコマンドパレットより「.NET:Generate Assets for Build and Debug」を選択・実行し、再度「Docker:Add Docker~」を選択・実行。

VSCode_20201226-2.pngVSCode_20201226-3.png

  • docker-compose.ymlが作成されることを確認。「ports:」パラメータを追加。
# Please refer https://aka.ms/HTTPSinContainer on how to setup an https developer certificate for your ASP .NET Core service.

version: '3.4'

services:
  webapi1:
    image: webapi1
    container_name: webapi-container
    build:
      context: .
      dockerfile: ./Dockerfile
    ports: 
      - 5001:5000 # 追加

  • docker-compose upコマンドを実行
docker-compose up
  • Createting~というメッセージが出ればとりあえずは作成終了。
Creating webapi1_webapi1_1 ... done
Attaching to webapi1_webapi1_1
webapi1_1  | info: Microsoft.Hosting.Lifetime[0]
webapi1_1  |       Now listening on: http://[::]:5000
webapi1_1  | info: Microsoft.Hosting.Lifetime[0]
webapi1_1  |       Application started. Press Ctrl+C to shut down.        
webapi1_1  | info: Microsoft.Hosting.Lifetime[0]
webapi1_1  |       Hosting environment: Production
webapi1_1  | info: Microsoft.Hosting.Lifetime[0]
webapi1_1  |       Content root path: /app

docker_20201226-1.png

VSCode_20201226-4.png

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?