V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
qwertyegg
V2EX  ›  Docker

Python 容器的 Dockerfile 为什么需要这一句 "COPY requirements.txt ."

  •  
  •   qwertyegg · 2021-04-02 19:57:17 +08:00 · 1454 次点击
    这是一个创建于 1091 天前的主题,其中的信息可能已经有所发展或是发生改变。
    FROM python:3.9.2-alpine3.12
    
    WORKDIR /usr/src/app
    
    RUN pip install -r requirements.txt
    
    CMD [ "python", "api.py" ]
    

    我已经把文件夹映射到 /usr/src/app 里面了,文件夹包含了 requirements.txt(所以 /usr/src/app 里面也有这个文件?)

    错误提示

    ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
    

    修改为官方推荐的

    FROM python:3.9.2-alpine3.12
    
    WORKDIR /usr/src/app
    
    COPY requirements.txt .
    
    RUN pip install -r requirements.txt
    
    CMD [ "python", "api.py" ]
    

    就能成功 build 。请问为什么在第一种情况下找不到 requirements.txt

    5 条回复    2021-04-06 00:08:14 +08:00
    Phant0m
        1
    Phant0m  
       2021-04-03 03:36:20 +08:00 via iPhone
    默认拉取回来的镜像内是没有 requirements.txt 这个文件的,镜像只包含必要的运行环境。
    julyclyde
        2
    julyclyde  
       2021-04-04 11:39:20 +08:00
    “把文件夹映射”这一步是 build 步骤吗?
    qwertyegg
        3
    qwertyegg  
    OP
       2021-04-05 02:24:07 +08:00
    @julyclyde 我是用的 docker-compose 映射的文件夹

    @Phant0m 不好意思,我没说清楚。我在 docker-compose 里面已经把包含 requirement.txt 和.py 的文件夹映射到 /usr/src/app 下了
    julyclyde
        4
    julyclyde  
       2021-04-05 10:20:46 +08:00
    @qwertyegg 那你映射过之后 RUN ls 或者 RUN tree 一下看看里面是什么样子吧?
    qwertyegg
        5
    qwertyegg  
    OP
       2021-04-06 00:08:14 +08:00
    @julyclyde 谢谢你的提示,我去试一试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5316 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:13 · PVG 16:13 · LAX 01:13 · JFK 04:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.