Skip to main content
 首页 » 编程设计

python之在 Docker 中执行 python 脚本

2025年05月04日22langtianya

我从 Docker 开始。我从 Python 3 中的 Hello World 脚本开始。这是我的 Dockerfile:

FROM ubuntu:latest 
 
RUN apt-get update 
RUN apt-get install python3 
 
 
COPY . hello.py 
 
 
CMD python3 hello.py 

在同一目录中,我有这个 python 脚本:

if __name__ == "__main__": 
    print("Hello World!"); 

我已经用这个命令构建了图像:

docker build -t home/ubuntu-python-hello . 

到目前为止,还不错。但是当我尝试使用此命令运行脚本时:

docker run home/ubuntu-python-hello 

我收到这个错误:

/usr/bin/python3: can't find '__main__' module in 'hello.py' 

我做错了什么?接受任何意见或建议,我只是一个新手。

谢谢。

请您参考如下方法:

感谢 Gerrat,我是这样解决的:

COPY hello.py hello.py 

代替

COPY . hello.py