3

The Problem

Hi I'm new to Docker. I want to ignore some files and directories using .dockerignore in my Django project. In the beginning, no files were ignored , then I searched in stackoverflow and found that its because of the volumes in docker-compose.yml, so I commented it out. But now some of the files and directories are getting ignored but some are not( pycache , db.sqlite3 ). I went through a lot of questions but couldn't find any solution.

Project structure

-src
--coreapp
---migrations
---__init__.py
---__pycache__
---admin.py
---apps.py
---models.py
---tests.py
---views.py
---tests.py
--admin.json
--db.sqlite3
--manage.py
-.dockerignore
-.gitignore
-docker-compose.yml
-Dockerfile
-Procfile
-README.md
-requirements.txt
-runtime.txt

Dockerfile

FROM python:3.7

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /code/requirements.txt
RUN pip install -r /code/requirements.txt

COPY . /code/
WORKDIR /code/

EXPOSE 8000

docker-compose.yml

version: '3'

services:
  db:
    image: postgres

  web:
    build: .
    command: bash -c "python src/manage.py runserver 0.0.0.0:8000"
#    volumes:
#      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

.dockerignore

# Byte-compiled / optimized / DLL files
__pycache__/

**/migrations
src/media
src/db.sqlite3
Procfile
.git

Commands

# build image
sudo docker-compose up --build

# to enter container
sudo docker exec -it [container id] bash

# to check ignored files inside the container
ls

Expected output

# Byte-compiled / optimized / DLL files
__pycache__/ # ignored

**/migrations # ignored
src/media # ignored
src/db.sqlite3 # ignored
Procfile # ignored
.git # ignored

Original Output

# Byte-compiled / optimized / DLL files
__pycache__/ # NOT ignored

**/migrations # ignored
src/media # ignored
src/db.sqlite3 # NOT ignored
Procfile # ignored
.git # ignored

Attempts

__pycache__/
**/__pycache__
**/*__pycache__
**/*__pycache__*
**/*__pycache__/
**/__pycache__/

*/db.sqlite3
db.sqlite3
tasif99
  • 65
  • 1
  • 8
  • Can you reduce this to a [mcve]? What file are you expecting to be ignored, that isn't? (Or not ignored, that is?) If you `docker-compose run web ls`, do you get different output? – David Maze Aug 25 '20 at 10:17
  • @DavidMaze i'm expecting __pycache__ and db.sqlite3 to be ignored. After i ran the command `sudo docker-compose run web ls src/` i don't see db.sqlite3 so I guess it being ignored but __pycache__ is still there. – tasif99 Aug 26 '20 at 11:03

2 Answers2

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
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.

imharindersingh
  • 166
  • 5
  • 14