英文:
Docker cannot find file listed in .gitignore
问题
我遇到了一个问题,Docker 找不到在 .gitignore 中列出的文件。
例如,我的目录结构如下:
- 项目文件夹
- WebServer.py
- DockerFile
- config
- privateKey.json
我将 config/
添加到 .gitignore
文件以保护私钥,但当我使用 gcloud run deploy
部署项目时,出现了 FileNotFoundError: [Errno 2] No such file or directory: config/privateKey.json
错误。如果我从 .gitignore
中删除 config/
,错误就消失了。
是否有办法解决这个问题并保持 config/
在 .gitignore
中?
谢谢!
以下是我的 DockerFile:
# 使用官方的轻量级 Python 镜像。
# https://hub.docker.com/_/python
FROM python:3.10-slim
# 允许语句和日志消息立即出现在 Knative 日志中
ENV PYTHONUNBUFFERED True
# 将本地代码复制到容器镜像中。
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# 安装生产依赖。
RUN pip install --no-cache-dir -r requirements.txt
# 在容器启动时运行 Web 服务。这里我们使用 gunicorn
# web 服务器,有一个工作进程和 8 个线程。
# 对于具有多个 CPU 内核的环境,增加工作进程的数量,使其等于可用的内核数。
# 超时设置为 0 以禁用工作进程的超时,以允许 Cloud Run 处理实例扩展。
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 "WebServer:create_app()"
我的 Dockerignore 文件:
Dockerfile
README.md
*.pyc
*.pyo
*.pyd
__pycache__
.pytest_cache
weight_upload_workflow.py
.git
.gitignore
<details>
<summary>英文:</summary>
I got an issue where Docker could not find files listed in .gitignore.
For example, my directory structure looks like this:
- project_folder
- WebServer.py
- DockerFile
- config
- privateKey.json
I added ```config/``` to `.gitignore` file to protect the private key, but I got ```FileNotFoundError: [Errno 2] No such file or directory: config/privateKey.json``` error when deploying my project with ```gcloud run deploy```. The error went away if I deleted ```config/``` from .gitignore.
Is there a way to solve this issue and keep ```config/``` in `.gitignore`?
Thanks!
Here is my DockerFile:
Use the official lightweight Python image.
https://hub.docker.com/_/python
FROM python:3.10-slim
Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt
Run the web service on container startup. Here we use the gunicorn
webserver, with one worker process and 8 threads.
For environments with multiple CPU cores, increase the number of workers
to be equal to the cores available.
Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 "WebServer:create_app()"
My dockerignore
Dockerfile
README.md
*.pyc
*.pyo
*.pyd
pycache
.pytest_cache
weight_upload_workflow.py
.git
.gitignore
</details>
# 答案1
**得分**: 1
> 当使用 `gcloud run deploy` 部署我的项目时。
[gcloud 主题 `gcloudignore`][1] 文档包括:
> 生成的 `.gcloudignore` 文件的默认内容,可以使用 `--ignore-file` 进行覆盖,如下所示:
>
> ```
> .gcloudignore
> .git
> .gitignore
> ```
因此,可能会将 `config/` 添加到 `.gitignore` 以防止由于生成的 `.gcloudignore` 而导致上传失败。
[BMitch][2] 在[评论中][3]指出,在运行 `gcloud run deploy --source` 时:
> 如果未指定 `--ignore-file`,将使用 `.gcloudignore` 文件。
如果本地源目录中不存在 `.gcloudignore` 文件而存在 `.gitignore` 文件,则 `gcloud` 将使用生成的与 `.gitignore` 文件兼容的 `.gcloudignore` 文件,以尊重您的 `.gitignore` 文件。
[1]: https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore
[2]: https://stackoverflow.com/users/596285/bmitch
[3]: https://stackoverflow.com/questions/75980443/docker-cannot-find-file-listed-in-gitignore/75980533?noredirect=1#comment134009049_75980533
[4]: https://cloud.google.com/sdk/gcloud/reference/run/deploy#--source
<details>
<summary>英文:</summary>
> when deploying my project with `gcloud run deploy`.
The [gcloud topic `gcloudignore`][1] documentation includes:
> The default content of the generated `.gcloudignore` file, which can be overriden with `--ignore-file`, is as follows:
>
> ```
>.gcloudignore
>.git
>.gitignore
> ```
So it is possible that adding `config/` to `.gitignore` would end up preventing its upload due to the generated `.gcloudignore`
[BMitch][2] points out in [the comments][3] to [`gloud run deploy --source`][4]:
> If `--ignore-file` is not specified, use `.gcloudignore` file.
If a `.gcloudignore` file is absent and a `.gitignore` file is present in the local source directory, `gcloud` will use a generated Git-compatible `.gcloudignore` file that respects your `.gitignored` files.
[1]: https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore
[2]: https://stackoverflow.com/users/596285/bmitch
[3]: https://stackoverflow.com/questions/75980443/docker-cannot-find-file-listed-in-gitignore/75980533?noredirect=1#comment134009049_75980533
[4]: https://cloud.google.com/sdk/gcloud/reference/run/deploy#--source
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论