[英]IdentityServer4 - Different behavior from docker-compose than manual run
我在 .net 核心 3.1 中有三個不同的項目:
我有一個 dockerfile 項目,它正在很好地構建我的圖像。
如果我使用這些命令手動運行容器,一切正常:
docker run -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=http://+:80" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -p 5001:80 stsidentity:latest
docker run -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=http://+:80" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -p 5003:80 adminapi:latest
docker run -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=http://+:80" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -p 9001:80 admin:latest
我的下一步是創建一個 docker-compose 以使其自動化,但我有一些問題。
在 Visual Studio 中,當我執行下面的 docker-compose 時,容器 sts 和 api 運行良好。 當我嘗試訪問我的管理 UI 時,出現錯誤“InvalidOperationException:IDX20803:無法從以下位置獲取配置:'http://localhost:5001/.well-known/openid-configuration'。”
如果我手動復制/粘貼 Url 在我的瀏覽器中,我可以正常訪問它。
我不明白為什么 docker-compose 和我從相同圖像手動運行容器時會有不同的行為。
version: "3.4"
services:
admin:
image: ${DOCKER_REGISTRY-}admin:latest
ports:
- "9001:80"
build:
context: .
dockerfile: src/IdentityServer/Admin/Dockerfile
container_name: IS4-admin
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
- DOTNET_USE_POLLING_FILE_WATCHER=1
depends_on:
- sts.identity
- admin.api
admin.api:
image: ${DOCKER_REGISTRY-}admin-api:latest
ports:
- "5003:80"
build:
context: .
dockerfile: src/IdentityServer/Admin.Api/Dockerfile
container_name: IS4-admin-api
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
- DOTNET_USE_POLLING_FILE_WATCHER=1
depends_on:
- sts.identity
sts.identity:
image: ${DOCKER_REGISTRY-}sts-identity:latest
ports:
- "5001:80"
build:
context: .
dockerfile: src/IdentityServer/STS.Identity/Dockerfile
container_name: IS4-sts-identity
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
- DOTNET_USE_POLLING_FILE_WATCHER=1
這可能是顯而易見的,但我沒有看到。
--
- 編輯
--
我讀到這是一個可能與本地主機有關的問題。 所以我嘗試使用 Traefik 容器作為反向代理,最后我遇到了同樣的問題。
我開始認為我的問題可能與容器無關,而只是在與 IISExpress 上的本地主機不同的地方執行它。
根據我在 IdentityServer4 上閱讀的內容,它可能來自證書問題。 我想我把所有東西都放在了 HTTP 中,但我不明白為什么我會在 HTTP 中遇到問題。
我成功獲得了更詳細的錯誤信息:
IOException: IDX20807: Unable to retrieve document from: 'http://login.traefik.me/.well-known/openid-configuration'. HttpResponseMessage: 'StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Wed, 08 Jul 2020 13:21:07 GMT
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SameOrigin
Referrer-Policy: no-referrer
Content-Security-Policy: script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline' https://fonts.googleapis.com/ https://fonts.gstatic.com/;font-src 'self' https://fonts.googleapis.com/ https://fonts.gstatic.com/
Content-Length: 0
}', HttpResponseMessage.Content: ''.
Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel)
InvalidOperationException: IDX20803: Unable to obtain configuration from: 'http://login.traefik.me/.well-known/openid-configuration'.
Microsoft.IdentityModel.Protocols.ConfigurationManager<T>.GetConfigurationAsync(CancellationToken cancel)
提前致謝,
祝你今天過得愉快
這與我使用 Docker Compose 解決方案獲得的完全相同。
以下是解決方案的簡化概述:
我將 Traefik 配置為將“api.localhost”上的請求轉發到 Api 容器,將“admin.localhost”上的請求轉發到 Admin 容器,並將“identity.localhost”上的請求轉發到 IdentityServer4 容器。
像你一樣,對“api.localhost”、“admin.localhost”和“identity.localhost”的請求在瀏覽器中都可以正常工作。
當我嘗試登錄 Api 或 Admin 容器時出現問題,因為 Traefik 不處理容器間通信。 此外,DNS(例如 '*.localhost')在容器本身中處理,因為它是一個環回 DNS,因此沒有按應有的方式解析。
我使其按預期工作的解決方案如下:
第 1 步:在 docker-compose 文件中創建一個新網絡。 創建該網絡是為了添加您自己的子網,因此為您的 Traefik 容器指定 static IP。
networks:
traefik:
ipam:
driver: default
config:
- subnet: 172.30.0.0/16 #replace by your desired subnet
STEP 2:配置Traefik容器的static IP地址:
networks:
default: #required to talk to containers
traefik:
ipv4_address: 127.30.1.1 #can be any address compatible with the subnet defined at step 1
第 3 步:將新創建的網絡添加到依賴 IdentityServer4 的所有容器中,不要忘記也添加“默認”網絡。 此外,我們將根據 Traefik 中的配置創建與 IdentityServer4 容器的 DNS 對應的額外主機。 這些額外的主機將指向 Traefik 容器的 static IP 地址。
admin:
image: ...
environment: ...
labels: ...
networks:
traefik:
default:
extra_hosts:
- "identity.localhost: 127.30.1.1 #replace by the configured traefik static address
此時,如果 Admin 容器嘗試解析 'identity.localhost' DNS,它將被轉發到 Traefik,而 Traefik 又會重定向到 IdentityServer4 容器:yaaai!
但是,如果您對其進行測試,您仍然會遇到一些與 HTTPS 相關的問題(我強烈建議您啟用和配置)
這些問題是:
為此,您必須掛載一個卷,如下例所示:
admin:
image: ...
environment: ...
labels: ...
networks:
traefik:
default:
extra_hosts:
- "identity.localhost: 127.30.1.1
volumes:
- "./localPathToMyCACertificateFiles/:/etc/ssl/certs/"
這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.