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

Docker直接运行Python脚本报错Syntax error: "(" unexpected求助

问题:Docker run直接执行Python脚本报错Syntax error: "(" unexpected

我知道自己操作有问题,但找不到症结。用VSCode在远程机器上创建了Dockerfile和devcontainer.json,能在VSCode里成功运行容器,拿到终端权限并在容器内运行脚本。

修改设置后,用以下命令能查到运行中的实例:

docker ps

为了保存这些设置,我提交了运行中的镜像:

docker commit 1f7cf26e8f26 container

接下来的操作出现了问题:
我能成功用下面的命令启动容器:

docker run --mount type=bind,source=/home/username/whale/source,target=/workspaces/source \
--mount type=bind,source=/home/username/whale_mount,target=/workspaces/whale_mount \
--mount type=bind,source=/etc/openvpn,target=/etc/openvpn,readonly \
--cap-add=NET_ADMIN --device /dev/net/tun \
--name test container

然后通过exec进入容器运行脚本完全正常:

docker exec -it test /bin/bash 
root -> /usr/local/python/current/bin/python /workspaces/source/somescript.py
Script worked!
root -> exit

但我想用单条命令完成启动容器、运行脚本、结束后自动关闭容器,于是尝试了这个命令:

docker run --mount type=bind,source=/home/username/whale/source,target=/workspaces/source \
--mount type=bind,source=/home/username/whale_mount,target=/workspaces/whale_mount \
--mount type=bind,source=/etc/openvpn,target=/etc/openvpn,readonly \
--cap-add=NET_ADMIN --device /dev/net/tun \
/usr/local/python/current/bin/python /workspaces/source/somescript.py

结果报错:

/usr/local/python/current/bin/python: 1: Syntax error: "(" unexpected

我搜了很多地方没找到类似问题,应该是个低级错误,求指点。


附:Dockerfile和devcontainer.json

Dockerfile

FROM  mcr.microsoft.com/devcontainers/base:focal
RUN /usr/bin/sudo /usr/bin/apt-get update && \
    /usr/bin/sudo /usr/bin/apt-get install -y  && \
    /usr/bin/sudo /usr/bin/apt-get install -y openvpn 

devcontainer.json

{
    "name": "container",
    "build": {
        "dockerfile": "Dockerfile" //"mcr.microsoft.com/devcontainers/base:focal"
    },
     "features": {
        "ghcr.io/devcontainers/features/python:1": {
            "version":"3.10.10"
        },
        "ghcr.io/warrenbuckley/codespace-features/sqlite:latest": {
            "autorun":"False"
        }
    },
    "runArgs":["--cap-add","NET_ADMIN",
               "--device","/dev/net/tun",
               //"--mount","type=bind,source=/home/username/whale/source,target=/workspaces/source",
               "--mount","type=bind,source=/home/username/whale_mount,target=/workspaces/whale_mount",
               "--mount","type=bind,source=/etc/openvpn,target=/etc/openvpn,readonly"],//target=/workspaces/openvpn,readonly"],
    "postStartCommand": "/usr/local/python/current/bin/python3.10 -m pip install pandas numpy pexpect"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 07:07:19