Series 13 dockerAsp.netCore deployment

1. Introduction

This full introductionAsp.netHow the core web api is deployed in a docker container and accesses web api services externally.After you have finished writing the dockerfile, you can create a mirror with the docker [image] build command.

The basic format is: docker build [options] path | url | -

The command reads the dickerfile at the specified path, including subdirectories, and sends all data under that path to the docker server as a context.After checking the dockerfile format, the docker server executes the instructions defined in the dockerfile. When it encounters ADD, COPY and RUN instructions, a new image is generated.Finally, if the mirror is created successfully, the ID of the final image is returned.

1. dockerfile file file creation

CreateAsp.netCore web API application, project name: k8swebapi.The dockerfile is as follows:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
EXPOSE 5000
COPY . .
ENTRYPOINT ["dotnet", "k8swebapi.dll", "--urls", "http://*:5000;http://*:5001"]



2. Upload

*Asp.netAfter the core web API is published, upload to the cloud server opt directory

[root@VM_0_12_centos opt]# ls
containerd  kubectl-1.17.3-0.x86_64.rpm  rh  webapipublish

Locate in the webapipublish directory to view the web file you just uploaded

[root@VM_0_12_centos opt]# cd webapipublish
[root@VM_0_12_centos webapipublish]# ls
appsettings.Development.json                         Microsoft.OpenApi.dll
appsettings.json                                     Swashbuckle.AspNetCore.Swagger.dll
dll                                                  Swashbuckle.AspNetCore.SwaggerGen.dll
Dockerfile                                           Swashbuckle.AspNetCore.SwaggerUI.dll
k8swebapi.deps.json                                  System.Runtime.CompilerServices.Unsafe.dll
k8swebapi.dll                                        System.Text.Encodings.Web.dll
k8swebapi.pdb                                        System.Text.Json.dll
k8swebapi.runtimeconfig.json                         web.config
k8swebapi.Views.dll                                  wwwroot
k8swebapi.Views.pdb                                  YLYUN.Common.Dapper.dll
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer.dll  YLYUN.Common.Dapper.pdb
Microsoft.AspNetCore.Mvc.Versioning.dll              YLYUN.Common.Dapper.xml
Microsoft.Bcl.AsyncInterfaces.dll

3. Build a mirror

Execution format: docker build-t <name of the image to be generated>.

* k8swebapi is the name of the image

Ending. refers to the dockerfile path (. stands for the context path because the dockerfile is in the current directory)

[root@VM_0_12_centos webapipublish]# docker build -t k8swebapi .
Sending build context to Docker daemon 7.513MB
Step 1/5 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
---> e7e3b238011c
Step 2/5 : WORKDIR /app
---> Running in f6496d30d085
Removing intermediate container f6496d30d085
---> 5657f984ae35
Step 3/5 : EXPOSE 5000
---> Running in 93c58d4b5fc1
Removing intermediate container 93c58d4b5fc1
---> 1a6657883eb9
Step 4/5 : COPY . .
---> 9badab908e55
Step 5/5 : ENTRYPOINT ["dotnet", "k8swebapi.dll", "--urls", "http://*:5000;http://*:5001"]
---> Running in 25a44038e606
Removing intermediate container 25a44038e606
---> 3068b399a9f4
Successfully built 3068b399a9f4
Successfully tagged k8swebapi:latest


















When the mirror is built successfully, display Successfully and return the mirror ID 3068b399a9f4

4. Create Container and Start Container

Using docker run

- d stands for Background Running Container

-- rm Specifies that containers are automatically deleted when they are stopped, and that containers are automatically deleted when docker stop contriner is stopped

-- P (upper case) Host host automatically allocates ports and associates with port 5000 exposed by the container, accessing web api services externally using ports automatically allocated by host host

-- name Container name

The last parameter is the mirror name, preceding by [OPTIONS] format: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

[root@VM_0_12_centos webapipublish]# docker run -d --rm -P  --name  k8swebapi  k8swebapi
dd8b01b33183f621d98043fdddbb7fda5817d312b7943a53718d6e6b0b0b94aa

View the created container and the host's automatically assigned port 32770 through docker ps.In-container access to api service port 5000 and out-of-container access to api service port 32770

[root@VM_0_12_centos webapipublish]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
dd8b01b33183        k8swebapi           "dotnet k8swebapi.dl..."   About an hour ago   Up About an hour    0.0.0.0:32770->5000/tcp   k8swebapi
3ddee94cef3a        ubuntu:latest       "/bin/bash"              6 days ago          Up 6 days                                     quizzical_nash

 

5. Enter the container

Enter the k8swebapi container, view the app directory, and request the api service as follows:

[root@VM_0_12_centos webapipublish]# docker exec -it dd8b01b33183 /bin/bash
root@dd8b01b33183:/app# ls
Dockerfile                         System.Text.Encodings.Web.dll  k8swebapi.Views.pdb
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer.dll  System.Text.Json.dll        k8swebapi.deps.json
Microsoft.AspNetCore.Mvc.Versioning.dll             YLYUN.Common.Dapper.dll        k8swebapi.dll
Microsoft.Bcl.AsyncInterfaces.dll             YLYUN.Common.Dapper.pdb        k8swebapi.pdb
Microsoft.OpenApi.dll                     YLYUN.Common.Dapper.xml        k8swebapi.runtimeconfig.json
Swashbuckle.AspNetCore.Swagger.dll             appsettings.Development.json   web.config
Swashbuckle.AspNetCore.SwaggerGen.dll             appsettings.json            wwwroot
Swashbuckle.AspNetCore.SwaggerUI.dll             dll
System.Runtime.CompilerServices.Unsafe.dll         k8swebapi.Views.dll
root@dd8b01b33183:/app# curl http://localhost:5000/api/v1/user/IndexList
[{"id":1,"name":"Zhang San"},{"id":2,"name":"Li Si"}]root@dd8b01b33183:/app#

6. External access

Access on the host machine as follows:

[root@VM_0_12_centos webapipublish]# curl http://localhost:32770/api/v1/user/IndexList
[{"id":1,"name":"Zhang San"},{"id":2,"name":"Li Si"}][root@VM_0_12_centos webapipublish]#

) Accessed externally through a browser on the windows system (where IP is server cloud ip), as follows:

Keywords: Docker JSON xml curl

Added by Velausanakha on Thu, 21 May 2020 05:13:18 +0300