In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the method of installing Nginx+ASP.NET Core in a single container in Linux system, which has a certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
It is recommended to use a reverse proxy server to forward requests to the Kestrel Http server in the production environment, and practice the process of containerizing the Nginx-- > ASP.NET Core deployment architecture.
Nginx- > ASP.NET Coe deployment architecture containerization
There are two options for deploying Nginx--- > ASP.NETCore in Docker. The first is to deploy Nginx+ASP.NET Core in a single container, which is described in this article. The other is to deploy Nginx and ASP.NETCore in separate containers, and the containers communicate with each other through the network bridge built into Docker (please follow the blog post).
This practice will use .NET Core CLI to create the default web application mkdir appcd appdotnet new webdotnet restoredotnet build
The project is then published to the specified directory (dotnet publish), and the files generated by the release will be used for image packaging.
Related tutorials: Linux Video tutorial
Build an image
This time, we will use ASP.NETCore Runtime Image [mcr.microsoft.com/dotnet/core/aspnet:2.2] as the base image, which contains .NET Core Runtime, ASP.NETCore framework components, and dependencies, and makes some optimizations for production deployment.
Pit 1: web app is deployed this time. Do not use [mcr.microsoft.com/dotnet/core/runtime:2.2] as the basic image. An error will be reported when launching the container:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.AspNetCore.App', version' 2.2.0 'was not found.
-Check application dependencies and target a framework version installed at:
/ usr/share/dotnet/
-Installing .NET Core prerequisites might help resolve this problem:
Https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
-The .NET Core framework and SDK can be installed from:
Https://aka.ms/dotnet-download
Because the underlying image does not contain ASP.NET Core framework components.
The definition of Dokefile will include nginx. Enable Nginx standard configuration proxy to request to Kestrel in the container:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2RUN apt-get updateRUN apt-get install-y nginxWORKDIR / appCOPY bin/Debug/netcoreapp2.2/publish .COPY. / startup.sh. Run chmod 755 / app/startup.shRUN rm / etc/nginx/nginx.confCOPY nginx.conf / etc/nginx ENV ASPNETCORE_URLS http://+:5000EXPOSE 5000 80 CMD ["sh", "/ app/startup.sh"]
Line 1 specifies the base image
Line 3-4 installs Nginx from Debian package management store
Line 6-7 sets the working directory and places the ASP.NET Core WebApp deployment package
Line 9-10 setup startup script
Line 12-13 Settings nginx profile
Line 15-16 sets ASP.NETCore Kestrel to listen on port 5000, exposing port 5000 to the outside of the container
Line 18 will give you the startup script later
Tip: you need to understand that the container is a separate linux environment, and the EXPOSE in Dockfile is used to indicate the port that the container intends to expose.
Here, only port 80 can be exposed to the outside, but a non-port 80 must be defined for the ASPNETCORE_URLS as the kestrel listening port in the container.
The app directory structure of the final (tree-L 1) output is as follows
. ├── app.csproj ├── appsettings.Development.json ├── appsettings.json ├── bin ├── Dockerfile ├── nginx.conf ├── obj ├── Program.cs ├── Properties ├── Startup.cs └── startup.sh
Nginx configuration
Create the nginx configuration file needed in the above Dockerfile, and create the file in the same directory as vim nginx.conf:
Worker_processes 4 proxy_pass events {worker_connections 1024;} http {sendfile on; upstream app_servers {server 127.0.0.1 http 5000;} server {listen 80; location / {proxy_pass and proxy_redirect off; proxy_set_header Host $host Proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name;}
Line 8-10 defines a set of servers (here there is only webapp), and the resource name (app_servers) can be used anywhere in this file.
Line 13 tells Nginx to listen on port 80
Line 15-22 indicates that all requests need to be proxied to app_servers
In summary, this file defines that Nginx listens for external requests on port 80 and forwards them to port 5000 of the same container.
Startup script
For Docker containers, only one CMD (or ENTRYPOINT definition) can be used, but this reverse proxy configuration requires Nginx and Kestrel to be started, so we define a script to accomplish these two tasks
#! / bin/bashservice nginx startdotnet / app/app.dll
Related tutorials: docker Video tutorial
Build an image
Docker build-t example/hello-nginx.
The mirror name is example/hello-nginx observation output, and you will see each step of the output defined in Dockerfile.
The image construction Dockerfile is compared with the dockerfile generated by vs docker tool, and the image generated by this file is smaller, making full use of the concept of image layering.
Run Mirror
Docker run-- name test-it-d-p 8080 it 80 example/test
The container name is test and webapp is now accessible from the http://localhost:8080 port, verified by curl-s-D-localhost:8080-o / dev/null
Enter the container through the shell terminal to further explore Nginx and Kestrel services:
Docker exec-it test bash
# curl-s-D-localhost:80-o / dev/nullHTTP/1.1 200 OKServer: nginx/1.6.2Date: Fri, 24 Feb 2017 14:45:03 GMTContent-Type: text/html; charset=utf-8Transfer-Encoding: chunked# curl-s-D-localhost:5000-o / dev/nullHTTP/1.1 200 OKDate: Fri, 24 Feb 2017 14:45:53 GMTTransfer-Encoding: chunkedContent-Type: text/html; charset=utf-8Server: Kestrel
Tip: for running containers, you can use docker exec-it [container_id] [command] to enter the container inside and explore the container.
For containers that failed to start, use docker logs [container_id] to view the container output log
Learn about the network basics of docker:
When the Docker daemon starts on the host with its default configuration parameters, it creates a Linux bridge device called docker0, which automatically assigns random IP to and subnets that meet the standard private IP segment, which determines the network segment to which all newly created containers will be assigned IP addresses.
You can use docker inspect [container_id] to view part of the network configuration:
-output intercepted from docker inspect [container_id]-"Networks": {"bridge": {"IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "a74331df40dc8c94483115256538304f1cbefe9f65034f20780a27271e6db606", "EndpointID": "4f35ea62c1715bd9f6855bc82ada06e1bf5e58291dabb42e92ebc9552c6f017b" "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "," GlobalIPv6PrefixLen ": 0 "MacAddress": "02:42:ac:11:00:03", "DriverOpts": null}}
The NetworkID listed here is the docker network ls bridge named bridge, which is the default docker0 bridge (docker inspect networkid can continue to explore).
As mentioned above, ASP.NET Core has two containerized reverse proxy deployment architectures, and it will be practiced to deploy Nginx and ASP.NET Core as separate containers.
Thank you for reading this article carefully. I hope it is helpful for everyone to share the method of installing Nginx+ASP.NET Core in a single container in the Linux system. At the same time, I also hope that you can support it, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.