NET Core Container

Source: Internet
Author: User
Tags curl dotnet nginx reverse proxy docker ps docker compose docker run

1. Introduction

Next to the. NET core containerized @docker, this section introduces how to use Nginx to complete the reverse proxy for. NET core applications, and then introduce deployment issues for multi-container applications.

2. Why need Nginx

The default Web server in. NET core is Kestrel.

Kestrel is great for serving dynamic content from ASP. However the Web serving parts aren ' t as feature rich as Full-fe atured servers like IIS, Apache or Nginx. A reverse Proxy-server can allow the offload work like serving static content, caching requests, compressing requests, and SSL termination from the HTTP server.

Kestrel can be used to provide dynamic content for ASP, but there is no full-featured server for IIS, Apache, nginx in Web services. We can use a reverse proxy server to receive HTTP requests from the Internet and forward them to Kestrel after some initial processing, such as requested caching and compression, providing static content, and SSL termination.

The use of reverse proxy servers (Nginx in this article) not only gives our web site an optional additional layer configuration and defense, but it also simplifies load balancing and SSL settings. And more importantly, the reverse proxy server can be well integrated with the existing infrastructure.

3. Hello Nginx

We also try to play the Nginx based on Docker.

//拉取Nginx镜像$ docker pull nginx//启动Nginx容器$ docker run -d -p 8080:80 --name hellonginx nginx

Above we started in the background running a nginx container named Hellonginx, whose port is mapped to the host's 8080 port, we can now be directly accessed through the browser to http://<ip address>:8080 see the Nginx Welcome interface.

At this point, an Nginx container is started. So how do you reverse proxy? Don't worry, we'll take one step at a pace.

4. Reverse proxy. NET Core MVC4.1. Start the Web container

Remember the image we created for our last local packaged MVC project hellodocker.web ? Here we start the image again to create a container:

//启动一个helodocker.web的镜像并命名容器为hellodocker.web.nginx#  docker run -d -p 5000:5000 --name hellodocker.web.nginx hellodocker.web160166b3556358502a366d1002567972e242f0c8be3a751da0a525f86c124933//尝试访问刚刚运行的容器[[email protected] ~]# curl -I http://localhost:5000HTTP/1.1 200 OKDate: Sun, 24 Dec 2017 02:48:16 GMTContent-Type: text/html; charset=utf-8Server: KestrelTransfer-Encoding: chunked

OK, we've opened the host's 5000 port to map the MVC container we started.

4.2. Configuring the Reverse Proxy

Let's configure Nginx to reverse proxy the Web container we just started.
In order for Nginx to successfully proxy the Web site running within the specified container, we first need to know the IPAddress of the container. docker inspect <container id/name>can be found using the.

View the list of running containers $ docker PS CONTAINER ID IMAGE COMMAND CREATED S Tatus PORTS namesd046b7b878a0 Hellodocker.web"dotnet Run" 5 seconds ago up 3 seconds 0.0. 0. 0:5000->5000/tcp Hellodocker.web.nginx//Use the ' | ' pipe operator plus the ' grep ' filter operator to directly extract the keyword we're looking for $ docker Inspect Hellodocker.web.nginx | grep IPAddress "secondaryipaddresses": null, "IPAddress": "192.168.0.5", "IPAddress": " 192.168.0.5 ",              

From above you can see that my web container is running on the host 192.168.0.5:5000 . Below we configure the Nginx forwarding request to 192.168.0.5:5000 complete the reverse proxy.

The configuration file path for the Nginx configuration reverse proxy is:/etc/nginx/conf.d/default.conf.
We can configure the reverse proxy configuration by locally creating a configuration file attached to the Nginx container.

$ cd demo$ mkdir nginx//创建my_nginx.conf文件$ touch my_nginx.conf$ vi my_nginx.confserver {  listen 80; location / { proxy_pass http://192.168.0.5:5000; }}

Above listen , we configure Nginx to listen to 80 port, specify the proxy_pass IP and port of our web container to complete the configuration of reverse proxy file.

The next step is to launch a new Nginx container and share the configuration file inside the container by mounting it.

$ docker run-d-P8080:80 >-v $HOME /demo/nginx< Span class= "Hljs-regexp" >/my_nginx.conf:/etc/nginx/conf.d/ default.conf > Nginx 95381aa56a336f65b6d01ff9964ae3364f37d25e5080673347c1878b3a5bb514/usr/bin/docker-< Span class= "hljs-string" >current:error response from daemon:driver failed programming External connectivity on endpoint elated_mccarthy ( 5A576D5991DD164DB69B1C568C94C15E47EC7C67E43A3DD6982A2E9B83B60E08): Bind for 0.0.0.0:8080 failed:port is already allocated.          

We found that the container failed to start because the 8080 port was occupied by the Nginx container we had just started for the first time. What to do? Two methods: The first one is to kill the Nginx container you just created, and the second is to map to the new port. Choose the first one here.

$ docker PS1bd630b60019 Nginx "nginx-g ' daemon off" 59 minutes ago up 59 minutes 0.0.0.0:8080->80/tcp hellonginx//using Docker RM < Container id> Delete container, specify-F to force delete Docker rm 1bd630b60019-f// Restart Nginx Container $ docker run-d-P 8080:80 >-v $HOME /demo/nginx/my_nginx.conf:/etc/nginx/conf.d/< Span class= "Hljs-keyword" >default.conf > Nginx 793d4c62ec8ac4658d75ea0ab4273a0b1f0a9a68f9708d2f85929872888b121d        

After the successful launch, we again in the browser http://<ipaddress>:8080 , found that the return is no longer the default Nginx Welcome page, but we launched the Web container run in the first page of MVC, indicating the reverse proxy configuration success!

5. Docker compose makes everything easier

The above steps are simple, but in two steps: The first is that our web and Nginx are divided into two deployments, and the second is that we must know the IP and port number of the Web container to complete the configuration of the reverse proxy file.

For complex applications that require multiple containers (such as nginx, SQL Server, Redis, RABBITMQ, and so on) to run in a coordinated manner, it is obviously cumbersome to deploy using the above approach, and also to distress the network connection between the various containers.
Fortunately, Docker is thoughtful for us to think of this. With the Compose module, we can write a docker-compose.yml file that uses declarative syntax to start a series of interconnected containers that can be completed in one step.

Docker compose is a Docker tool for defining and running complex applications. With compose, you can define a multi-container application in a file, and then use a single command to launch your app and complete all the preparation.

5.1. Installing Docker Compose

Execute the following command in turn:

https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose$ sudo chmod +x /usr/local/bin/docker-compose$ docker-compose --versiondocker-compose version 1.18.0, build 1719ceb
5.2. Preparation of the first docker-compose.yml

DOCKERS-COMPOSE.YML file to be defined in our project folder, our project folder is located $HOME/demo/HelloDocker.Web .

$ cd $HOME/demo/HelloDocker.Web$ touch docker-compose.yml$ vi docker-compose.ymlversion: ‘2‘services:    hellodocker-web: container_name: hellodocker.web.compose build: . reverse-proxy: container_name: reverse-proxy image: nginx ports: - "9090:8080" volumes: - ./proxy.conf:/etc/nginx/conf.d/default.conf

A brief introduction to the above configuration file, which defines two services: one is to hellodocker-web build the image in our current project directory and start a hellodocker.web.compose container called. One is to reverse-proxy use the nginx image for the reverse proxy, which is configured by specifying volumes the way it is mounted.

$ touch proxy.conf$ vi proxy.confserver {    listen 8080;    location / {      proxy_pass http://hellodocker-web:5000;    }}$ ls[[email protected] HelloDocker.Web]# lsappsettings.Development.json Controllers Models Startup.csappsettings.json docker-compose.yml obj Viewsbin Dockerfile Program.cs wwwrootbundleconfig.json HelloDocker.Web.csproj proxy.conf[[email protected] HelloDocker.Web]#

It is important to note the configuration of the reverse proxy: proxy_pass http://hellodocker-web:5000; where the IP section directly specifies the name of the first service defined in Docker-compose.yml hellodocker-web .
Now let's start compose:

$ docker-compose up-dbuilding Hellodocker-webstep1:from microsoft/Dotnet:latest--->7d4dc5c258ebstep2:workdir/app---> Using cache--->98d48a4e278cstep3:copy. /app--->0cb9fc540aferemoving Intermediate Container9fecf088f03fstep4:run dotnet Restore---> RunningInch4bb7f34edbbe Restore CompletedInch597.13 msFor/app/hellodocker.web.csproj. Restoring PackagesFor/app/hellodocker.web.csproj ... Restore completedInch1.76 secFor/app/hellodocker.web.csproj. --->6869609ece23removing Intermediate Container4bb7f34edbbestep5:expose---> RunningIn a97febf01e5a--->9b2639862a94removing Intermediate Container A97febf01e5astep6:env Aspnetcore_urlshttp*:5000---> RunningInch4e2f4af28a8d--->0069661e891aremoving Intermediate Container4e2f4af28a8dstep7:entrypoint dotnet Run---> RunningIn CBBF08D906F9--->0bbeef249b30removing Intermediate Container cbbf08d906f9successfully built0bbeef249b30Warning:imageFor service Hellodocker-web is built because it did not already exist. To rebuildThis image is must use ' docker-compose build ' or ' docker-compose up--build '. Creating hellodocker.web.compose ... donestarting reverse-proxy ... donePerform docker-compose PS authentication start service $ docker-compose PS Name Command State Ports---------------------------------------------------------------------------------------Hellodocker.web.compose Dotnet run up5000/tcpreverse-proxy nginx-g daemon off; Up 80/tcp, 0.0.0.0:9090->8080/tcp //use the Curl directive to verify Nginx reverse proxy $ curl-i http: //localhost:9090http/1.1 200 OK< Span class= "hljs-string" >server:nginx/1.13.7date:sun, 24 Dec 2017  04:37:35 gmtcontent-type:text/ html Charset=utf-8connection:keep-alive     

As you can see, by performing a verification of the curl -I http://localhost:9090 Proxy server configuration successfully, we re-visit the browser to http://<ip address>:9090 find the default homepage of our MVC project returned correctly.

View the currently running container $ docker PSCONTAINERIdimage command created Span class= "Hljs-type" >status ports NAMESA52830499CFF Hellodockerweb_hellodocker-web  "dotnet Run" 7 minutes ago up 7 minutes 5000/tcp HELLODOCKER.WEB.COMPOSEE1FE109E10BC nginx  "nginx-g ' daemon Off" 11 minutes ago up 4 minutes 80/tcp, 0.0. 0.0:9090->8080/tcp  Reverse-proxy               

We also found that docker-compose two containers were created by the right one hellodocker.web.compose and reverse-proxy .

6. Finally

After the above practice, we have a certain understanding of nginx, and know how to configure. We also learned how to use docker-compose packaging to run complex applications that require multiple containers.

In this article, we'll talk about how to play MySQL on Linux and get through the containerized deployment of Nginx+web+mysql.

NET Core Container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.