docker container deployment Asp.Net Core, Nginx, MySQL

2019/10/24, docker19.03.4,
.netcore 3.0 Abstract: asp.net core 3.0 website project container deployment, use docker-compose choreography container Nginx, MySQL container, web container

Dependency Structure Introduction

Entire website project named samplems, needs a total of three containers (by dependency order):
1.MySQL vessel, named samplems.mysql, from the mysql(official) Mirror
2.web vessel, named samplems.web, from samplemsweb(own build out) Mirror
3 .nginx vessel, named samplems.nginx, from samplemsnginx(build out of their own) image
using three containers docker-compose choreography, the establishment of a bridged network (the name samplems-net) to contact the three

Directory Structure

New deploy on CentOS in the deployment folder, used to store the required files to deploy, deploy in the file structure as described in the following will be gradually established after :()

│  docker-compose.yml       //compose编排文件
│ 
├─web                       //web 目录 
│  │  Dockerfile            //web docker配置文件 
│  │                      
│  └─publish                //web 发布文件(发布生成的内容) 
│      ...                     
│      appsettings.json     //发布文件中包含appsettings.json,其中有数据库连接配置 
│      ... 
│   
└─nginx                     //nginx目录
    │  Dockerfile           //nginx docker配置文件
    │                                                         
    └─confs                 //nginx 配置文件目录
        nginx.conf          //nginx全局配置
        samplems.conf       //web项目对应的nginx配置文件

The deployment of two steps:
1. Construction of the required image
2. Arrangement docker-compose the container

Construction of the required image

MySQL Mirror

The project uses a MySQL official mirror image directly without further modification package, direct pull (download) official image:

docker pull mysql

Mirroring web

web project preparation

In Visual Studio, MySQL database connection string web project (appsettings.json in) with the following:

server=samplems.mysql;database=samplems;user=root;password=mysql@samplems

important point:

  • server = samplems.mysql, this address is our MySQL vessel name;
  • Password here and later docker-compose.yml specified MySQL password to be consistent;
  • The connection string port not explicitly specified, it is the default port 3306

When the web publishing project, select the target runtime linux-x64or 可移植
publish content copied to the target system, put it in deploy / web / publish folder, as follows:

I am here for the web project name WebMvc, so where is the WebMvc.dll hereinafter Dockerfile objects in dotnet instructions.

Write Dockerfile

deploy / web folder under the new Dockerfile file, as follows:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /app
COPY ./publish/ /app 
EXPOSE 80
ENTRYPOINT ["dotnet","WebMvc.dll"]

Five sentences above mean are:

  • Based mcr.microsoft.com/dotnet/core/aspnet:3.0 mirror (because it is netcore3.0 web project), will automatically download the image
  • / App directory to work within the container
  • The / web / publish folder to copy all the contents of / app
  • 80 exposed container port
  • Use dotnet run asp.net core web project, here it is the "WebMvc.dll", because WebMvc.dll in the working directory, it does not require any path

Building web Mirror

Folder build mirrored in deploy / web file :( note the following instructions and spaces between samplemsweb., Samplemsweb is what we named the image name)

docker build -t samplemsweb .

As shown below:

nginx Mirror

nginx configuration file

deploy / nginx / confs / nginx.conf the new file, This file is the configuration file Nginx, as follows:

user  nginx;
worker_processes  1;
#error_log
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    client_max_body_size 256m;
    client_body_buffer_size 50m;
    access_log  /var/log/nginx/access.log  main;
    #以下是 websocket 支持配置
    #map $http_upgrade $connection_upgrade {
    #   default upgrade;
    #    '' close;
    #}

    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

deploy / nginx / confs / samplems.conf under the new document, as follows:

# 设定负载均衡后台服务器列表 
upstream composeserver {
#指定支持的调度算法
    ip_hash;
    server samplems.web:80;
}

#虚拟主机的配置
server {
    listen 80;
    location / {
        proxy_pass http://composeserver;
        
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #以下是 websocket 支持配置
        #proxy_http_version 1.1;
        #proxy_set_header Upgrade $http_upgrade;
        #proxy_set_header Connection $connection_upgrade;
    }
}

Samplems.web which is the name of our web container, ie container port on the back of 80

Write Dockerfile

New Dockerfile file deploy / nginx folder, as follows:

FROM nginx:latest

COPY ./confs/samplems.conf /etc/nginx/conf.d/default.conf
COPY ./confs/nginx.conf /etc/nginx/nginx.conf

Means the above three sentences are:

  • Nginx based on the latest image building, will automatically download the image
  • The confs / samplems.conf copy files to /etc/nginx/conf.d/default.conf
  • The confs / nginx.conf copy files to /etc/nginx/nginx.conf

Construction of nginx Mirror

Construction of the mirror using the following command :( note and spaces between samplemsnginx., Samplemsnginx is what we named the image name)

docker build -t samplemsnginx .

When finished, docker view mirror list:

so far all mirror has been established

Write docker-compose.yml

In the deploy folder New docker-compose.yml file, as follows:

version: '3.4'

services:
  samplems.mysql:
    image: mysql
    container_name: samplems.mysql
    ports:
      - "3306:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=mysql@samplems
      - TZ=Asia/Shanghai
    volumes:
      - /app/data/mysql:/var/lib/mysql
    networks:
      - samplems-net

  samplems.web:
    image: samplemsweb
    container_name: samplems.web
    restart: always
    depends_on:
      - samplems.mysql
    environment:
      - TZ=Asia/Shanghai
    networks:
      - samplems-net

  samplems.nginx:
    image: samplemsnginx
    container_name: samplems.nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /app/logs/nginxlogs:/var/log/nginx
    depends_on:
      - samplems.web
    networks:
      - samplems-net

networks:
  samplems-net:
    driver: bridge

Codes indicated above, the entire file structure yml roughly divided version, services, networks three, where the services have samplems.mysql, samplems.web, samplems.nginx three

samplems.mysql:

  • Mirror image represents the reference, here is the official mysql
  • container_name: container name (similarly defined two other containers of this field)
  • Port number of host ports map 3306 is mapped to the container port 3306 (similarly defined two other containers of this field)
  • restart: always specify the restart policy after the container exit (the same definition as two other vessels of this field) is always restarted
  • environment is defined in the MySQL root account password for mysql @ samplems, here and password web project needs consistency
  • defined environment (the same definition as two other containers of this field) region Asia Shanghai container, prevent the container zone and the time zone does not cause the host to obtain a time deviation
  • volumes mounted data volumes, all the containers to be written to the file / var / lib / mysql is written to the host / app / data / mysql (so mysql container to be deleted, even if the data still exists)
  • samplems-net networks using custom bridge network (the same definition as two other containers of this field)

samplems.web:

  • image referenced herein are the mirror of our own build of the web
  • depends_on show container first start samplems.mysql, then start samplems.web

samplems.nginx:

  • image referenced herein are the mirror of our own build of nginx
  • ports 80 port is the default http port 443 is the default https port
  • volumes mounted data volumes, all the containers to be written to the file / var / log / nginx is written to the host / app / logs / nginxlogs in this log file is mainly nginx
  • depends_on show container first start samplems.web, then start samplems.nginx, this way, the order for the start of mysql-> web-> nginx

Start container

At this point all the preparations have been completed, execute the following command at the start of the container deploy folder:

docker-compose up -d

The following diagram, three green done three containers represent a successful start:

Use docker ps view the running state of the container, it is running:

Open the host firewall port 80:

sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo systemctl restart firewalld

CentOS browser to access the IP address, you can view a Web page:

Guess you like

Origin www.cnblogs.com/kasnti/p/11731899.html