Docker-Compose one-click deployment of Ningx+Asp.net core site+Redis

Keywords: Web Server Nginx Docker Redis JSON

Docker-Compose manages multiple Docker containers through a configuration file, defines services in the configuration file, and then uses scripts to start, stop and restart applications, as well as services in applications and all service-dependent containers. Docker-Compose is very suitable for combining application scenarios using multiple containers to achieve rapid environment construction.

1. Create an asp.net core sample site

Using vs2017 to create an Asp.net core MVC site, Redis is introduced, mainly to achieve distributed caching and some demonstration content. Then you write dockerfile and create site images. There are many references to these processes, and I won't repeat them here.

2. Create docker-compose.yml file
The file is as follows. Two web site containers are created to connect redis containers. Nginx exposed port 80, can access web1 and web2, modify nginx configuration, reverse proxy, distribute requests to two web applications, nginx configuration reference 3


version: '3'

services:
   xxx_mvc:
        container_name: web
        image: registry.cn-shenzhen.aliyuncs.com/xxx/web
        environment:
            - ASPNETCORE_ENVIRONMENT=Production 
        expose:
            - 80
        entrypoint: ["dotnet", "XXX.Web.Mvc.dll"]  
        restart: always 
        links:
          - redis
        volumes:
            - "./appsettings.Production.json:/app/appsettings.Production.json"   
   
            
    xxx_mvc_2:
        container_name: web-2
        image: registry.cn-shenzhen.aliyuncs.com/xxx/web 
        environment:
            - ASPNETCORE_ENVIRONMENT=Production 
        expose:
            - 80 
        entrypoint: ["dotnet", "XXX.Web.Mvc.dll"]  
        restart: always 
        links:
          - redis
        volumes:
            - "./appsettings.Production.json:/app/appsettings.Production.json"   
   
 
    nginx:
      container_name:xxx-nginx
      image: nginx
      ports:
        - "8080:80" 
      links:
        - xxx_mvc:web1
        - xxx_mvc_2:web2 
      restart: always
      volumes:
        - "./nginx-about/nginx.conf:/etc/nginx/nginx.conf:ro"
  
   
    redis:
      image: redis
      restart: always
      expose:
        - 6379
    

3.Nginx configuration file

upstream web{
    ip_hash; 
    server web1:80; #Number 1  
    server web2:80; #Number 2 
}

server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://web;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

4. Execute the docker-compose up command
Connect to the server, after uploading the relevant files in the new directory, execute docker-compose up in the directory, and the command will automatically complete the creation and start of the service, if successful, access http://xxx:8080 You can see the site page.

Good luck:

Posted by like_duh44 on Thu, 18 Apr 2019 02:03:34 -0700