Docker Getting Started Series 02

The last article used a simple example to briefly introduce the configuration of the Dockerfile and its related commands. This article will continue to add new code and how to put unit tests into the Image creation process in the sample program of the previous article.

First, we need to build a new class library and move the business-related code from the original project to the new class library. The code to create the class library is as follows:

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

Now that we have created a new class library, now coding..., I will put the project address at the end of the article. Let's say you've typed the code with your fingers flying. OK, although we are very confident in our code, in order to reflect our responsible attitude towards the code, we still need to write a unit test. OK, we need to create a unit test project, very simple, the code is as follows:

dotnet new xunit -o test

Now the coding unit test case, the code is written after 5 minutes, and now run locally, the command is as follows:

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

At this stage, the code related to unit testing has been written, so it is time to enter the topic, modify the Dockerfile content as follows:

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, now you can create a new Image , type docker build -t dotnetapp:1.0-test ., this process is faster than the first time, because the relevant base image ( BaseImage ) has been downloaded to the local before. Pay attention to the process of observing the Image , and you will find the output of the unit test.

OK, the content about unit testing has been introduced, is it very simple? If you are smart, you may have thought of whether you can also use unit testing as the entry program for container startup. You can try it yourself.

sample code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771322&siteId=291194637