如何在Azure Web App上运行Go二进制服务?部署故障排查
我有一个运行在8000端口的Go项目,尝试通过Azure DevOps构建后部署至Linux Azure Web App。但访问页面时显示“:( Application Error”,多数尝试中提示“Container didn't respond to HTTP pings on port: 8000”。以下是我的DevOps配置和日志信息:
DevOps配置
pool: name: Azure Pipelines timeoutInMinutes: 30 steps: - task: GoTool@0 displayName: 'Use Go 1.22.2' inputs: version: 1.22.2 - task: Go@0 displayName: 'go get' inputs: arguments: '-d' workingDirectory: '$(System.DefaultWorkingDirectory)' - task: Go@0 displayName: 'go build' inputs: command: build arguments: '-ldflags="-s -w" -o build' workingDirectory: '$(System.DefaultWorkingDirectory)' - task: ArchiveFiles@2 displayName: 'Archive $(System.DefaultWorkingDirectory)/build' inputs: rootFolderOrFile: '$(System.DefaultWorkingDirectory)/build' includeRootFolder: false - task: AzureRmWebAppDeployment@4 displayName: 'Azure App Service Deploy: MyServerProject' inputs: azureSubscription: 'Visual Studio Enterprise: ...' appType: webAppLinux WebAppName: MyServerProject packageForLinux: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip' RuntimeStack: 'DOTNETCORE|7.0' StartupCommand: 'my-server' AppSettings: '-PORT 8000 -WEBSITES_PORT 8000 -WEBSITES_CONTAINER_START_TIME_LIMIT 1800'
日志信息
2024-05-06T19:44:07.724140609Z _____ 2024-05-06T19:44:07.734185163Z / _ \ __________ _________ ____ 2024-05-06T19:44:07.734190363Z / /_\ \\___ / | \_ __ \_/ __ \ 2024-05-06T19:44:07.734193663Z / | \/ /| | /| | \/\ ___/ 2024-05-06T19:44:07.734196563Z \____|__ /_____ \____/ |__| \___ > 2024-05-06T19:44:07.734199763Z \/ \/ \/ 2024-05-06T19:44:07.734203064Z A P P S E R V I C E O N L I N U X 2024-05-06T19:44:07.734206264Z 2024-05-06T19:44:07.734208964Z Documentation: http://aka.ms/webapp-linux 2024-05-06T19:44:07.734211764Z Dotnet quickstart: https://aka.ms/dotnet-qs 2024-05-06T19:44:07.734214564Z ASP .NETCore Version: 7.0.13 2024-05-06T19:44:07.734217364Z Note: Any data outside '/home' is not persisted 2024-05-06T19:44:09.748366682Z Starting OpenBSD Secure Shell server: sshd. 2024-05-06T19:44:10.140172606Z Starting periodic command scheduler: cron. 2024-05-06T19:44:10.149816058Z Running oryx create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -defaultAppFilePath /defaulthome/hostingstart/hostingstart.dll -bindPort 8000 -bindPort2 '' -userStartupCommand 'my-server' 2024-05-06T19:44:10.341022895Z Could not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml' 2024-05-06T19:44:10.369622450Z Could not find operation ID in manifest. Generating an operation id... 2024-05-06T19:44:10.369672950Z Build Operation ID: 054d28e9-d3b7-43b9-8e77-ea26b35fa7fd 2024-05-06T19:44:11.658474634Z 2024-05-06T19:44:11.658510434Z Agent extension 2024-05-06T19:44:11.658515034Z Before if loop >> DotNet Runtime 2024-05-06T19:44:11.670287898Z DotNet Runtime 7.0Writing output script to '/opt/startup/startup.sh' 2024-05-06T19:44:11.949309510Z Running user provided startup command... 2024-05-06T19:44:11.949361811Z my-server: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by my-server) 2024-05-06T19:44:11.949386711Z my-server: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by my-server) 2024-05-06T19:44:05.685Z INFO - 7.0_20240321.5.tuxprod Pulling from appsvc/dotnetcore 2024-05-06T19:44:05.706Z INFO - Digest: sha256:*** 2024-05-06T19:44:05.706Z INFO - Status: Image is up to date for 10.1.0.5:13209/appsvc/dotnetcore:7.0_20240321.5.tuxprod 2024-05-06T19:44:05.724Z INFO - Pull Image successful, Time taken: 0 Seconds 2024-05-06T19:44:05.925Z INFO - Starting container for site 2024-05-06T19:44:05.926Z INFO - docker run -d --expose=8000 --name myserverproject_123312 -e WEBSITE_USE_DIAGNOSTIC_SERVER=true -e PORT=8000 -e WEBSITES_PORT=8000 -e WEBSITE_SITE_NAME=myserverProject -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=myserverproject.azurewebsites.net -e WEBSITE_INSTANCE_ID=*** -e HTTP_LOGGING_ENABLED=1 appsvc/dotnetcore:7.0_20240321.5.tuxprod my-server 2024-05-06T19:44:09.424Z INFO - Initiating warmup request to container myserverproject_123312 for site myserverproject 2024-05-06T19:44:13.018Z ERROR - Container myserverproject_123312 for site myserverproject has exited, failing site start 2024-05-06T19:44:13.044Z ERROR - Container myserverproject_123312 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging. 2024-05-06T19:44:13.057Z INFO - Stopping site myserverproject because it failed during startup.
疑问
是否可以实现该部署?若可行,我遗漏了哪些配置?
完全可以实现部署,问题出在三个核心配置错误上:
1. 错误选择了.NET Runtime栈
你当前指定的RuntimeStack: 'DOTNETCORE|7.0'是.NET的运行环境,但你的项目是Go语言,应该选择Go对应的Runtime栈,或者使用通用的Linux容器栈。
修改AzureRmWebAppDeployment任务的RuntimeStack参数:
RuntimeStack: 'GO|1.22'
如果找不到对应版本,也可以使用NONE,让Oryx自动检测Go环境。
2. Go编译时未做静态编译适配目标环境
日志里明确显示GLIBC_2.32和GLIBC_2.34版本缺失,这是因为你在Azure Pipelines代理环境中编译的Go程序依赖了较高版本的GLIBC,但Azure Web App的.NET容器里的GLIBC版本更低。
解决方法是修改go build命令,添加静态编译参数,让程序不依赖系统GLIBC:
arguments: '-ldflags="-s -w" -o build -tags netgo -ldflags "-linkmode external -extldflags -static"'
同时可以禁用CGO,彻底避免依赖系统库:在Go build任务前添加环境变量设置:
- task: Bash@3 displayName: 'Disable CGO' inputs: targetType: 'inline' script: 'export CGO_ENABLED=0'
3. 启动命令中的程序名称不匹配
你在StartupCommand: 'my-server'指定的程序名是my-server,但编译时输出的程序名是build(-o build),部署后解压到容器里的文件名还是build,所以启动命令应该改为:
StartupCommand: './build'
额外优化建议
- 可以在Azure Web App的配置页面直接设置
WEBSITES_PORT为8000,不需要在AppSettings里重复指定; - 延长容器启动超时时间的设置
WEBSITES_CONTAINER_START_TIME_LIMIT 1800可以保留,避免程序启动慢导致超时。
内容的提问来源于stack exchange,提问作者Pablo

