项目添加 dockerfile
Docker file 内容
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["WebAPIForDocker/WebAPIForDocker.csproj", "WebAPIForDocker/"]
RUN dotnet restore "WebAPIForDocker/WebAPIForDocker.csproj"
COPY . .
WORKDIR "/src/WebAPIForDocker"
RUN dotnet build "WebAPIForDocker.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebAPIForDocker.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebAPIForDocker.dll"]
Visual stdio 执行 dockerfile
直接运行 docker build 报错
D:\Work\My\opensource\Dotnet2\DockerExample\WebAPIForDocker>docker build -t dotnet:WebAPIForDocker .
Sending build context to Docker daemon 303.9MB
Step 1/16 : FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
---> e56d8091e0bf
Step 2/16 : WORKDIR /app
---> Using cache
---> 1b458c38a238
Step 3/16 : EXPOSE 80
---> Using cache
---> 9b2ec0ced4cd
Step 4/16 : FROM microsoft/dotnet:2.1-sdk AS build
---> e87e5f0285fc
Step 5/16 : WORKDIR /src
---> Using cache
---> b4d77609de72
Step 6/16 : COPY ["WebAPIForDocker/WebAPIForDocker.csproj", "WebAPIForDocker/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder044742703/WebAPIForDocker/WebAPIForDocker.csproj: no such file or directory
选中dockerfile,右键执行生成docker 镜像
查看生成的docker 镜像
D:\Work\My\opensource\Dotnet2\DockerExample>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
webapifordocker latest f97cbfd07c4a 2 minutes ago 253MB
运行 docker 镜像
D:\Work\My\opensource\Dotnet2\DockerExample>docker run -it -p 5000:5000 webapifordocker
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]
Creating key {cbb57e3c-2b4d-4e00-b9cc-bbb7c3e4977a} with creation date 2018-12-26 09:28:53Z, activation date 2018-12-26 09:28:53Z, and expiration date 2019-03-26 09:28:53Z.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {cbb57e3c-2b4d-4e00-b9cc-bbb7c3e4977a} may be persisted to storage in unencrypted form.
info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39]
Writing data to file '/root/.aspnet/DataProtection-Keys/key-cbb57e3c-2b4d-4e00-b9cc-bbb7c3e4977a.xml'.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
终端中测试
返回数据一切正常
root@084a60515e21:/app# curl http://localhost:5000/api/orders/getorderList
[{"item1":"ja201808204778560863744992601","item2":"08/20/2018 18:29:16"},{"item1":"201808205628636382300589946","item2":"08/20/2018 18:29:18"},{"item1":"201808205753427170575780018","item2":"08/20/2018 18:29:19"},{"item1":"201808204749312987745111125","item2":"08/20/2018 18:29:21"},{"item1":"201808204725778270582113008","item2":"08/20/2018 18:29:22"},{"item1":"201808204776350327734840932","item2":"08/20/2018 18:29:27"},{"item1":"201808205627318305959441050","item2":"08/20/2018 18:29:30"},{"item1":"201808205593544898683339668","item2":"08/20/2018 18:29:34"},{"item1":"201808205707984438836626731","item2":"08/20/2018 18:29:35"},{"item1":"201808205573431454844603206","item2":"08/20/2018 18:29:39"}]root@084a60515e21:/app#