[Django]-.dockerignore: some files are ignored, some are not

5👍

The .dockerignore file only affects what files are copied into the image in the Dockerfile COPY line (technically, what files are included in the build context). It doesn’t mean those files will never exist in the image or in a container, just that they’re not included in the initial copy.

You should be able to verify this by looking at the docker build output. After each step there will be a line like ---> 0123456789ab; those hex numbers are valid Docker image IDs. Find the image created immediately after the COPY step and run

docker run --rm 0123456789ab ls

If you explore this way a little bit, you should see that the __pycache__ directory in the container is either absent entirely or different from the host.

Of the specific files you mention, the db.sqlite3 file is your actual application’s database, and it will be created when you start the application; that’s why you see it if you docker exec into a running container, but not when you docker run a clean container from the image. What is __pycache__? clarifies that the Python interpreter creates that directory on its own whenever it executes an import statement, so it’s not surprising that that directory will also reappear on its own.

1👍

What exactly do you have in requirements.txt?

Is there some package in this file that created this directory? Because docker CLI can only ignore this before sending context for the build, once the build starts(docker base image, pip install, etc as written in dockerfile) then dockerignore might not be able to ignore it from docker image.

If not then you can try

*/db* -> eliminate files starting with db one level below the root,
*sqlite3

As per https://docs.docker.com/engine/reference/builder/ Matching is done using Go’s filepath.Match rules. A preprocessing step removes leading and trailing whitespace and eliminates . and .. elements using Go’s filepath.Clean. Lines that are blank after preprocessing are ignored.

In your attempts */db.sqlite3 db.sqlite3, maybe the . is getting eliminated as mentioned above and hence unable to remove the requested file from build.

Leave a comment