Docker cannot find file listed in .gitignore

304 views Asked by At

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
1

There are 1 answers

5
VonC On BEST ANSWER

when deploying my project with gcloud run deploy.

The gcloud topic gcloudignore 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 points out in the comments to gloud run deploy --source:

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.