美文网首页
Docker部署Gradio项目

Docker部署Gradio项目

作者: Cui_Shu | 来源:发表于2022-04-28 14:22 被阅读0次
    使用Docker简单部署Gradio项目

    创建 main.py 文件,在此文件中编辑gradio内容

    import gradio as gr
      
    def greet(name):
        return "Hello " + name + "!!"
    
    demo = gr.Interface(fn=greet, inputs="text", outputs="text")
    
    if __name__ == "__main__":
        demo.launch(server_name="0.0.0.0")  
        # 注意:gradio启动项目后默认地址为127.0.0.1;使用docker部署需要将地址修改为0.0.0.0,否则会导致地址访问错误
        # 默认端口为7860,如需更改可在launch()中设置server_port=7000
    ~                                      
    

    编辑 requirements.txt 文件,将项目所有需要的包添加到此文件

    gradio==2.9.4
    

    编辑 Dockerfile 文件

    # Use an official Python runtime as a parent image
    FROM python:3.7.4-slim
    
    # Set the working directory to /app
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    COPY . /app
    
    # Install any needed packages specified in requirements.txt
    RUN pip3 install --trusted-host pypi.python.org -r requirements.txt
    
    # Define environment variable
    ENV NAME gradio
    
    # The default gradio port is 7860
    EXPOSE 7860
    
    # Run main.py when the container launches
    CMD ["python", "main.py"]
    ~                            
    

    其中涉及到的问题:
    gradio项目启动后默认使用的地址为127.0.0.1,使用docker部署后导致外部无法访问
    这是因为打包成镜像之后127.0.0.1只表示本机本容器中的一个虚拟网卡,只接受本容器中的应用相互通讯。虽然说服务已经成功启动了,但是外部却不能访问。所以将项目地址修改为0.0.0.0就可以正常运行了

    相关文章

      网友评论

          本文标题:Docker部署Gradio项目

          本文链接:https://www.haomeiwen.com/subject/rdhdyrtx.html