码迷,mamicode.com
首页 > 其他好文 > 详细

Docker入门系列02

时间:2018-04-24 00:20:30      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:pass   结果   本地   单元   sts   release   github   otn   介绍   

上篇用一个简单的示例,简单的介绍了 Dockerfile 的配置及其相关的命令。这一篇会在上篇的示例程序里,继续添加新代码及如何将单元测试也放入 Image 建立过程内。

首先,我们需要建一个新的类库,将业务相关的代码从原来的项目移到新类库里。创建类库的代码如下:

dotnet new classlib -o utils
dotnet restore utils/
dotnet sln add utils/utils.csproj

现在我们已经创建好了新的类库,那么现在coding……,我会在文章末尾放入项目地址。假设你十指如飞的敲好了代码。OK,虽然我们对自己的代码很自信,但为了体现我们对代码的负责的态度,还是需要写个单元测试的。OK,我们需要创建个单元测试项目,非常简单,代码如下:

dotnet new xunit -o test

现在coding单元测试用例,5分钟后代码写好了,现在本地运行下,命令如下:

dotnet test ./tests/
#以下是test运行结果
Build started, please wait...
Build completed.

Test run for /Users/gebilaowang/Development/dotnet-docker/dotnetapp/tests/bin/Debug/netcoreapp2.0/tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.7581900]   Discovering: tests
[xUnit.net 00:00:00.8340440]   Discovered:  tests
[xUnit.net 00:00:00.8412310]   Starting:    tests
test text 1
test text 2
test text 3
test text.
[xUnit.net 00:00:01.0264220]   Finished:    tests

Total tests: 3. Passed: 3. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.8490 Seconds

到现阶段,单元测试相关的代码已经写好了,那么该进入主题了,修改 Dockerfile 内容如下:

FROM microsoft/dotnet:2.0-sdk AS build

# copy csproj and restore as distinct layer
WORKDIR /src
COPY *.sln .
COPY dotnetapp/*.csproj ./dotnetapp/
COPY tests/*.csproj ./tests/
COPY utils/*.csproj ./utils/
RUN dotnet restore

# copy and build everything else
COPY . .
RUN  dotnet build

# create test
FROM build AS test
WORKDIR /src/tests
RUN dotnet test

# publish execution app
FROM build AS publish
WORKDIR /src/dotnetapp
RUN dotnet publish -c Release -o out

#
FROM microsoft/dotnet:2.0-runtime as runtime
WORKDIR /app
COPY --from=publish /src/dotnetapp/out ./
ENTRYPOINT [ "dotnet","dotnetapp.dll" ]

OK,现在可以创建新的 Image 了,键入 docker build -t dotnetapp:1.0-test .,这个过程相对第一次比较快,因为相关的基础镜像(BaseImage)之前已经下载到本地了。注意观察 Image 的过程,你会发现单元测试的输出结果。

OK,有关单元测试的内容已经介绍完了,是不是很简单。聪明的你,也许想到了是不是也可以将单元测试作为容器启动的入口程序,你可以自己动手试试。

示例代码

Docker入门系列02

标签:pass   结果   本地   单元   sts   release   github   otn   介绍   

原文地址:https://www.cnblogs.com/ShawnHao/p/8922620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!