Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy a .Net6 project to docker

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to deploy the .Net6 project to docker". In the daily operation, I believe many people have doubts about how to deploy the .Net6 project to docker. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to deploy the .Net6 project to docker"! Next, please follow the editor to study!

Deploy a .net6 project to docker [1] create a new .net6 mvc or webapi project; [2] create a Dockerfile file in the root directory of the mvc or webapi project and set it to always copy

The content of the Dockerfile file is:

# add .net6 basic image FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base# container working directory WORKDIR / app# specify the internal startup port of webapi or mvc site. By default, 80#ENV ASPNETCORE_URLS http://+:5001# exposes docker container port # EXPOSE 8001#EXPOSE 44copies all files under the publishing directory to the container's working root directory # COPY. . (you can also write this way) COPY. /. / # if you use System.SqlClient.dll to access the database, you need to add the sentence RUN sed-I's hand TLSv1.2 and TLSv1.0 / etc/ssl/openssl.cnf# container entry point ENTRYPOINT ["dotnet", "dotnet6Demo.web.dll"] [3] publish the mvc or webapi project [4] copy the published mvc or webapi project to a folder on the centos server, for example, to the [/ wwwroot/myproject] folder; [5] install docker [6] install .net6 image docker pull mcr.microsoft.com/dotnet/aspnet:6.0 [7] into the / wwwroot/myproject directory of the centos server, and build an image with the image name format of [image name: version number], such as dotnet6-demo:0.0.1.

Build Image command:

Cd / wwwroot/myprojectdocker build-t dotnet6-demo:0.0.1.

Dotnet6-demo indicates the name of the built image, and: 0.0.1 indicates the image version number. Note that the image name is followed by a space + English period, indicating the file under the current path of construction.

The above command is an image built with the default configuration file name Dockerfile. If you build the image with a custom configuration file name, you need to add the-f parameter.

Docker build-f Dockerfile02-t dotnet6-demo:0.0.1. [8] create a custom network

Default network docker0, which cannot be accessed by domain name, so create a custom network.

Docker network create-- driver bridge-- subnet 192.168.0 mynet 16-- gateway 192.168.0.1

Parameter description:

-- driver bridge # indicates bridging mode

-- subnet 192.168.0.0 ip 16 # 16 indicates that a maximum of 65535 CPUs are supported, and can be assigned from 192.168.0.2 to 192.168.255.255. If not specified, the default network segment of the-- subnet parameter is 172.17.0.0 ip 16, and the default gateway is 172.17.0.1.

-- subnet 192.168.0.0Uniple 24 # 24 indicates that a maximum of 254 ip is supported and can be assigned from 192.168.. 0.2 to 192.168.0.255.

-- gateway # represents the gateway of the docker container

Mynet represents the network name

When creating containers directly in docker, run does not add a custom network. By default, the network named bridge is used, which is the docker0 network. Containers cannot ping each other using container names, but can only ping each other through ip.

In the network defined by ourselves, containers can communicate with each other directly through the container name.

View the details of the mynet network:

Docker network inspect mynet [9] create and run the container

Create a container format:

Docker run-d-name container name-p external port number: docker container port number-restart=always-v / host directory: / container directory image name

Create a container using the default network docker0 (not recommended in the production environment):

Docker run-d-- name my-dotnet6-demo01-p 8001 restart=always 80-- restart=always-v / www-appdata/dotnet6-demo/logs:/logs-v / www-appdata/dotnet6-demo/upload:/upload dotnet6-demo:0.0.1

Create a container using a custom network (with an extra-- net parameter):

Docker run-d-- name my-dotnet6-demo01-p 8001 restart=always-v / www-appdata/dotnet6-demo/logs:/logs-v / www-appdata/dotnet6-demo/upload:/upload-- net mynet dotnet6-demo:0.0.1

Parameter description:

-d # running in the background

-p 8001VR 80 # abbreviated host port number: docker port number

-v # data volume path, format: (- v / host directory: container directory) or (- v / container directory), you can take multiple-v parameters.

-- restart=always # dokcer also starts the container automatically when it is restarted

-- net mynet # specify the network name

View the container that is running:

Docker ps

View all containers:

Docker ps-a [10] access container

Access method: http:// host ip: Port number

Http://192.168.1.200:8002

Enter the container [11] and enter the container docker exec-it my-dotnet6-demo01 / bin/bash container settings

Set the existing container to boot automatically.

Docker update-- name of the restart=always container

Cancel container boot and start automatically

Docker update-this is the name of the restart=no container, so the study on "how to deploy the .net6 project to docker" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report