通过Docker部署OpenAuth.Core(二)

上一篇文章仅使用了一个Dockerfile用于编译所有项目文件,然后使用微软默认的aspnet镜像来运行三个独立的容器,但有些同学提意见希望能有三个独立的镜像方便实际使用时版本更新,好吧,满足你们.....

1. 编写三个Dockerfile

  • Dockerfile-mvc
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build

COPY OpenAuth.Core /OpenAuth.Core/
WORKDIR "/OpenAuth.Core/OpenAuth.Mvc"
RUN dotnet build "OpenAuth.Mvc.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "OpenAuth.Mvc.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS final
WORKDIR /app
COPY --from=publish /app .
EXPOSE 1802 1803 56831
ENTRYPOINT ["dotnet", "OpenAuth.Mvc.dll"] 
  • Dockerfile-identity
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build

COPY OpenAuth.Core /OpenAuth.Core/
WORKDIR "/OpenAuth.Core/OpenAuth.Identity"
RUN dotnet build "OpenAuth.IdentityServer.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "OpenAuth.IdentityServer.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS final
WORKDIR /app
COPY --from=publish /app .
EXPOSE 12796
ENTRYPOINT ["dotnet", "OpenAuth.IdentityServer.dll"] 
  • Dockerfile-api
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build

COPY OpenAuth.Core /OpenAuth.Core/
WORKDIR "/OpenAuth.Core/OpenAuth.WebApi"
RUN dotnet build "OpenAuth.WebApi.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "OpenAuth.WebApi.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS final
WORKDIR /app
COPY --from=publish /app .
EXPOSE 52789
ENTRYPOINT ["dotnet", "OpenAuth.WebApi.dll"] 

2. 初始化数据库

  • 使用数据库脚本sql server 初始化脚本或mysql初始化脚本 初始化数据库

  • 修改OpenAuth.Mvc/appsettings.json连接字符串,并复制到/config/mvc.json:

 "OpenAuthDBContext": "Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000"
  • 修改OpenAuth.WebApi/appsettings.json连接字符串,并复制到/config/api.json:
 "OpenAuthDBContext": "Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000"

3.切换登录认证方式(可选)

OpenAuth.core支持两种登录认证方式:自定义认证和基于IdentityServer的OAuth认证。

这两种方式通过配置webapi或mvc的appsettings.json可以自由切换:

"IdentityServerUrl": "http://宿主机IP:12796", //IdentityServer服务器地址。如果为空,则不启用OAuth认证

同时,你还需要修改OpenAuth.Identity的Config.cs约第46行,将host改为你的服务器地址,同时你还能修改一些其它参数

当IdentityServerUrl为空时,采用普通的token认证,这时不需要OpenAuth.Identity启动支持。

当IdentityServerUrl配置了地址时,则采用Identity认证方式,这时必须启动OpenAuth.Identity。

具体在OpenAuth.Mvc/WebAPI中如何控制登录访问并进行权限控制,请点击这里


4. 生成镜像

.net core 3.1版的OpenAuth.Core还未发布,所以我们选用dev分支

#下载基础镜像
docker pull mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine

#下载源码
git clone -b dev --progress --verbose https://gitee.com/yubaolee/OpenAuth.Core.git

#生成镜像
docker build -f Dockerfile-mvc -t openauth-mvc .
docker build -f Dockerfile-api -t openauth-api .
docker build -f Dockerfile-identity -t Dockerfile-identity .

5. 运行OpenAuth.Core

  • 运行OpenAuth.Identity(IdentityServer)
docker run -d \
--name openauth-identity \
-p 12796:12796 \
openauth-identity
  • 运行OpenAuth.WebApi(API服务)
docker run -d \
--name openauth-api \
-p 52789:52789 \
-v /config/api.json:/app/appsettings.Production.json
openauth-api
  • 运行前端网站
docker run -d \
--name openauth-mvc \
-p 56831:56831 \
-p 1802:1802 \
-p 1803:1803 \
-v /config/mvc.json:/app/appsettings.Production.json
openauth-mvc


6.浏览网站

通过浏览器,打开地址http://ip:1802 即可看到你刚部署的OpenAuth.Core的网站了

你可能感兴趣的:(通过Docker部署OpenAuth.Core(二))