文件目录结构

/webapps/jinzhengba-webapi/
├── Dockerfile
└── Jinzhengba.WebApi.dll

Dockerfile

# 添加.net6基础镜像
FROM mcr.microsoft.com/dotnet/aspnet:6.0.14 AS base

# 暴露端口
EXPOSE 80

# 容器工作目录
WORKDIR /app

# 拷贝发布目录下的所有文件到容器的工作根目录
COPY . . 

# 如果用System.SqlClient.dll访问数据库,需要加这一句
# RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf

# 设置时区
RUN ["rm","-rf","/etc/localtime"]
RUN ["ln","-sf","/usr/share/zoneinfo/Asia/Shanghai","/etc/localtime"]

# 安装redis , 对应 start.sh 文件
# RUN echo 'deb http://mirrors.aliyun.com/debian bullseye main'>/etc/apt/sources.list
# RUN echo 'deb http://mirrors.aliyun.com/debian-security bullseye-security main'>>/etc/apt/sources.list
# RUN echo 'deb http://mirrors.aliyun.com/debian bullseye-updates main'>>/etc/apt/sources.list
# RUN apt-get update -y
# RUN apt-get install -y redis-server

# 安装nginx , 对应 start.sh,default.conf,dist 文件
# RUN apt-get install -y nginx
# RUN rm -rf /etc/nginx/sites-enabled/*
# COPY default.conf /etc/nginx/conf.d/default.conf
# COPY dist /etc/nginx/dist

# 容器入口点(无redis) 二选其一
ENTRYPOINT ["dotnet", "Jinzhengba.WebApi.dll"]

# 容器入口点(带redis和后台管理) 二选其一
# ENTRYPOINT ["sh", "start.sh"]

start.sh(可选, 启动redis)

#!/bin/bash
nginx
redis-server &
dotnet Jinzhengba.WebApi.dll

只能在容器内部访问redis连接串: 127.0.0.1:6379 , 适用于云托管场景

default.conf(可选, 启动后台管理)

server {
    listen 80;
    server_name localhost; 
    location / {
        alias /etc/nginx/dist/; # 这里存放前端文件
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    # 配置反向代理
    location /api {
        proxy_ssl_server_name on;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        expires off;
        sendfile off;
        proxy_pass http://127.0.0.1:5000; #后端接口地址
    }
}

需要把管理端放 dist 文件夹

生成镜像

cd /webapps/jinzhengba-webapi
docker build -t jinzhengba-webapi .

启动容器

docker run -itd \
--name jinzhengba-webapi \
--restart always \
-p 5000:80 \
-v /webapps/jinzhengba-webapi:/app \
jinzhengba-webapi

文章作者: 正焕
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 海边的曼切斯特
教程
喜欢就支持一下吧