.net core 3.1 Docker部署 Nginx


windows 10 安裝 Docker Desktop

https://www.docker.com/products/docker-desktop

 

沒有帳號可以新建一下,免費提供一個私有倉庫

 

 

新建VS 項目

 

 

 

Dockerfile 配置

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80



COPY . /app
ENTRYPOINT ["dotnet", "WebDocker.dll"]

 

 

配置好后發布程序

 

 

 

發布成功后,系統會自動推送到倉庫中,包括Build命令,VS會替你解決

查看你的鏡像

 

 

 

查看本地鏡像

Docker images

 

 

 

CentOS 安裝 Docker

  1. 使用 root 權限登錄 Centos。確保 yum 包更新到最新。
    sudo yum update
  2. 卸載舊版本(如果安裝過舊版本的話)
    sudo yum remove docker  docker-common docker-selinux docker-engine
  3. 安裝需要的軟件包, yum-util 提供yum-config-manager功能,另外兩個是devicemapper驅動依賴的
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  4. 設置yum源
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  5. 可以查看所有倉庫中所有docker版本,並選擇特定版本安裝
    yum list docker-ce --showduplicates | sort -r
  6. 安裝docker
    sudo yum install docker-ce  #由於repo中默認只開啟stable倉庫,故這里安裝的是最新穩定版17.12.0
  7. 啟動並加入開機啟動
    sudo systemctl start docker
    sudo systemctl enable docker
  8. 驗證安裝是否成功(有client和service兩部分表示docker安裝啟動都成功了)
    docker version

     

Docker 安裝好 輸入docker login

用你剛才注冊的用戶和密碼登錄

dokcer images

 

 

 拉取鏡像

docker pull xxx:tag

成功之后運行鏡像

docker run -d --restart=always --name myapp -p 5000:80 bch1983410/mydocker :v1

查看運行鏡像

docker ps

這時可以訪問地址

 

 

 拉取nginx

docker pull nginx:latest

創建掛載目錄

mkdir -p /data/nginx/{conf,conf.d,html,logs}

編寫nginx,conf配置文件,並放在文件夾中

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://pic; 
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    upstream pic{
                server localhost:8082 weight=5;
    }

}

啟動容器

 

docker run --name mynginx -d -p 8081:80  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx

訪問地址8081

 

訪問地址8082

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM