You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Windows ServerCore容器exe执行异常问题求助

问题排查与解决方案

1. 脚本路径不匹配

你的docker-compose命令里指定的脚本路径是./work/script_static_ip.ps1,但Dockerfile里把脚本复制到了容器内的C:\prog\目录下,路径完全不符,导致PowerShell找不到脚本,自然不会执行任何操作。

修改后的docker-compose.yml:

version: '3'
services:
  ana:
    build: ./sa
    command:  powershell.exe -ExecutionPolicy Bypass -File C:\prog\script_static_ip.ps1

2. 网络接口名称错误

Windows容器内的网络接口名称通常不是Ethernet,而是类似vEthernet (Container NIC)这类动态名称。硬写固定名称会导致netsh命令执行失败,IP修改操作无法完成。

优化后的PowerShell脚本(自动获取主网络接口):

$ip_address = "10.10.10.15"
$subnet_mask = "255.255.255.0"
$gateway = "10.10.10.1"

# 获取容器内非回环的主网络接口名称
$interfaceName = (Get-NetAdapter | Where-Object { $_.Name -notlike "*Loopback*" }).Name

# 执行IP设置并检查结果
netsh interface ip set address name="$interfaceName" static $ip_address $subnet_mask $gateway
if ($LASTEXITCODE -ne 0) {
    Write-Error "IP设置失败: $($Error[0])"
    exit 1
}

# 启动main.exe
Start-Process "C:\prog\main.exe" -Wait

3. 捕获脚本执行日志

容器启动时的脚本输出默认不会留存,建议添加日志重定向,方便排查执行错误:

带日志输出的docker-compose命令:

command:  powershell.exe -ExecutionPolicy Bypass -File C:\prog\script_static_ip.ps1 *> C:\prog\script_log.txt

之后可通过以下命令查看日志:

docker exec <容器ID> type C:\prog\script_log.txt

4. 统一执行环境

手动进入容器时的环境变量可能和容器启动时的默认环境有差异,建议在脚本中切换到main.exe所在目录后再启动:

# 替换原Start-Process行
Set-Location C:\prog\
.\main.exe

内容的提问来源于stack exchange,提问作者StasKE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:32:01