docker-compose 构建失败,未找到文件但文件实际存在

docker-compose build failed, file not found but file actually exist

我在 Ubuntu-20.04 WSL

上键入 docker-compose build 时出现此错误
[+] Building 14.5s (9/9) FINISHED                                                                                                      
 => [internal] load build definition from Dockerfile                                                                              0.5s
 => => transferring dockerfile: 32B                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                 0.8s
 => => transferring context: 2B                                                                                                   0.0s
 => [internal] load metadata for docker.io/library/python:3.8                                                                     3.6s
 => [1/5] FROM docker.io/library/python:3.8@sha256:881e0df149c29af8b29a973a9e80814dae6cddf123fe38a0bcac71864c85fb8a               0.0s
 => [internal] load build context                                                                                                 0.5s
 => => transferring context: 1.90kB                                                                                               0.0s
 => CACHED [2/5] WORKDIR /home/asdf1234/                                                            0.0s
 => CACHED [3/5] COPY setup.py /home/asdf1234/setup.py                                              0.0s
 => CACHED [4/5] COPY init.sql /docker-entrypoint-initdb.d/                                                                       0.0s
 => ERROR [5/5] RUN pip install -e .[dev]                                                                                         8.7s
------                                                                                                                                 
 > [5/5] RUN pip install -e .[dev]:                                                                                                    
#9 5.617 Obtaining file:///home/asdf1234                                                                 
#9 5.978     ERROR: Command errored out with exit status 1:
#9 5.978      command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/asdf1234/setup.py'"'"'; __file__='"'"'/home/asdf1234/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-82n9p0l9
#9 5.978          cwd: /home/asdf1234/
#9 5.978     Complete output (5 lines):
#9 5.978     Traceback (most recent call last):
#9 5.978       File "<string>", line 1, in <module>
#9 5.978       File "/home/asdf1234/setup.py", line 17, in <module>
#9 5.978         with open(path.join(THIS_DIRECTORY, "README.md"), encoding="utf-8") as f:
#9 5.978     FileNotFoundError: [Errno 2] No such file or directory: '/home/asdf1234/README.md'
#9 5.978     ----------------------------------------
#9 5.979 WARNING: Discarding file:///home/asdf1234. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#9 5.980 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#9 7.090 WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
#9 7.090 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c pip install -e .[dev]]: exit code: 1

但问题是 README.md 文件存在于正确的位置且名称正确,这是我的 Dockerfile。如果我不使用 docker.

命令 pip install -e .[dev] 可以 运行 而不会出错
FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /home/asdf1234/
COPY setup.py /home/asdf1234/setup.py
COPY init.sql /docker-entrypoint-initdb.d/
RUN pip install -e .[dev]
CMD ["flask", "run", "--eager-loading", "-h", "0.0.0.0", "-p", "5000"]

这是我的 setup.py

"""Installation script for flask-hospital-api application."""
import sys

from os import path
from setuptools import setup, find_packages


if sys.version_info < (3, 8):
    sys.exit("Sorry, Python < 3.8 is not supported")


DESCRIPTION = (
    "Practice building flask app with REST API."
)

THIS_DIRECTORY = path.abspath(path.dirname(__file__))
with open(path.join(THIS_DIRECTORY, "README.md"), encoding="utf-8") as f:
    LONG_DESCRIPTION = f.read()

AUTHOR = "My Name"
AUTHOR_EMAIL = "my_name@gmail.com"
PROJECT_URLS = {
    "Documentation": "https://gitlab.com/asdfas/README.md",
    "Bug Tracker": "https://gitlab.com/asdfas/-/issues",
    "Source Code": "https://gitlab.com/asdfas/",
}
INSTALL_REQUIRES = [
    "Flask",
    "Flask-Bcrypt",
    "Flask-Cors",
    "Flask-Migrate",
    "flask-restx",
    "Flask-SQLAlchemy",

]
EXTRAS_REQUIRE = {
    "dev": [
        "black",
        "flake8",
        "pre-commit",
        "pytest-flask",
        "tox",
    ]
}

setup(
    name="flask-hospital-api",
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    long_description_content_type="text/markdown",
    version="0.1",
    author=AUTHOR,
    author_email=AUTHOR_EMAIL,
    maintainer=AUTHOR,
    maintainer_email=AUTHOR_EMAIL,
    license="MIT",
    url="https://gitlab.com/asdfas",
    project_urls=PROJECT_URLS,
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    python_requires=">=3.8",
    install_requires=INSTALL_REQUIRES,
    extras_require=EXTRAS_REQUIRE,
)

去掉setup.py前目录的前缀。将您的 Dockerfile 更改为:

FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /home/asdf1234/
COPY setup.py setup.py
COPY init.sql /docker-entrypoint-initdb.d/
RUN pip install -e .[dev]
CMD ["flask", "run", "--eager-loading", "-h", "0.0.0.0", "-p", "5000"]

更改 Dockerfile 以添加 README.md

FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /home/asdf1234/
COPY README.md /home/asdf1234/README.md
COPY setup.py /home/asdf1234/setup.py
COPY init.sql /docker-entrypoint-initdb.d/
RUN pip install -e .[dev]
CMD ["flask", "run", "--eager-loading", "-h", "0.0.0.0", "-p", "5000"]