qwertyegg
V2EX  ›  Docker

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

  •  
  •   qwertyegg · Apr 2, 2021 · 2615 views
    This topic created in 1936 days ago, the information mentioned may be changed or developed.
    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 replies    2021-04-06 00:08:14 +08:00
    Phant0m
        1
    Phant0m  
       Apr 3, 2021 via iPhone
    默认拉取回来的镜像内是没有 requirements.txt 这个文件的,镜像只包含必要的运行环境。
    julyclyde
        2
    julyclyde  
       Apr 4, 2021
    “把文件夹映射”这一步是 build 步骤吗?
    qwertyegg
        3
    qwertyegg  
    OP
       Apr 5, 2021
    @julyclyde 我是用的 docker-compose 映射的文件夹

    @Phant0m 不好意思,我没说清楚。我在 docker-compose 里面已经把包含 requirement.txt 和.py 的文件夹映射到 /usr/src/app 下了
    julyclyde
        4
    julyclyde  
       Apr 5, 2021
    @qwertyegg 那你映射过之后 RUN ls 或者 RUN tree 一下看看里面是什么样子吧?
    qwertyegg
        5
    qwertyegg  
    OP
       Apr 6, 2021
    @julyclyde 谢谢你的提示,我去试一试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3009 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 15:01 · PVG 23:01 · LAX 08:01 · JFK 11:01
    ♥ Do have faith in what you're doing.