. NET core containerized multi-container application deployment-using Docker-compose

Source: Internet
Author: User

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容器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.1200 OKDate:24201702: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 a list of running containers$ docker PS CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS namesd046b7b878a0 Hellodocker.web"dotnet Run"             5Seconds ago Up3Seconds0.0. 0. 0: the- the/tcp Hellodocker.web.nginx//Use the ' | ' pipe operator plus the ' grep ' filter operator to directly extract the keywords 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 {  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: the>-V $HOME/demo/Nginx/my_nginx.conf:/etc/nginx/conf.d/default. conf > Nginx95381aa56a336f65b6d01ff9964ae3364f37d25e5080673347c1878b3a5bb514/usr/bin/docker-Current :Error response fromDaemon: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"    -Minutes ago Up -Minutes0.0. 0. 0:8080- the/tcp Hellonginx//Use Docker RM <container id> to remove the container and specify-F to force delete$ docker RM1Bd630b60019-f//Restart Nginx container$ docker run-d-P8080: the>-V $HOME/demo/Nginx/my_nginx.conf:/etc/nginx/conf.d/default. conf > Nginx793d4c62ec8ac4658d75ea0ab4273a0b1f0a9a68f9708d2f85929872888b121d

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`1.18.01719ceb
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 imag  E:  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 {    listen8080;    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---> Runninginch 4Bb7f34edbbe Restore Completedinch 597.13Ms for /app/HelloDocker.Web.csproj. Restoring Packages for /app/HelloDocker.Web.csproj ... Restore completedinch 1.76Sec for /app/HelloDocker.Web.csproj.--->6869609Ece23removing Intermediate Container4Bb7f34edbbestep5: EXPOSE the---> RunninginchA97FEBF01E5A--->9B2639862a94removing Intermediate Container A97febf01e5astep6: ENV Aspnetcore_urlshttp://*:5000---> Runninginch 4e2f4af28a8d--->0069661e891Aremoving Intermediate Container4e2F4af28a8dstep7: entrypoint dotnet Run---> RunninginchCBBF08D906F9--->0Bbeef249b30removing Intermediate Container cbbf08d906f9successfully built0Bbeef249b30WARNING:Image forService Hellodocker-web was built because it does not already exist. To rebuild ThisImage you must use ' docker-compose build ' or ' docker-compose up--build '. Creating hellodocker.web.compose ... donestarting reverse-proxy ... done//Perform docker-compose PS Authentication to start the service$ docker-compose PS Name Command State Ports----------------------------- ----------------------------------------------------------Hellodocker.web.compose dotnet run up the/tcpreverse-proxy nginx-g daemon off; Up the/tcp, 0.0.0.0:9090->8080/Tcp//Use the Curl command to verify Nginx's reverse proxy$ curl-ihttp://localhost:9090http/1.1  $OkServer:nginx/1.13. 7Date:Sun, -Dec . Geneva:Panax Notoginseng: *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 PSCONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                            NAMESA52830499CFF Hellodockerweb_hellodocker-web"dotnet Run"             7Minutes ago up 7Minutes the/tcp HELLODOCKER.WEB.COMPOSEE1FE109E10BC Nginx"Nginx-g ' daemon Off"    OneMinutes ago up 4Minutes the/TCP,0.0.0.0:9090-8080/tcpReverse-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.

. NET core containerized multi-container application deployment-using Docker-compose

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.