The .dockerignore file will modify the context that is sent to the docker daemon to build the image. So the created image will not have these files inside unless you recreate them.

The volume mount is completely separate and a host volume mount, aka bind mount, will map the contents of the host directory directly into the container as is. This is a Linux OS level activity that doesn't follow .dockerignore.

You will need to exclude those files from the directory that you mount into the container, or not mount the volume into the container and rely on the image to take advantage of .dockerignore.

Answer from BMitch on Stack Overflow
🌐
TestDriven.io
testdriven.io › tips › 6850ab62-9323-4dca-8ddf-8db1d479accc
Tips and Tricks - Use a .dockerignore File | TestDriven.io
A properly structured .dockerignore file can help: Decrease the size of the Docker image · Speed up the build process · Prevent unnecessary cache invalidation · Prevent leaking secrets · Example: **/.git **/.gitignore **/.vscode **/coverage **/.env **/.aws **/.ssh Dockerfile README.md docker-compose.yml **/.DS_Store **/venv **/env ·
Discussions

COPY with --exclude does not apply to sub-directories (in general)
While I know, I can use .dockerignore for that, I wanted to try out COPY --exclude and found that COPY --exclude .venv . . Actually does not omit a/b/.venv. And neither does COPY --exclude */.venv . . But actually COPY --exclude */*/.venv . . does. So my question is: Is there a syntax to exclude ... More on forums.docker.com
🌐 forums.docker.com
1
0
September 19, 2024
Is .dockerignore treated differently using docker compose?
Please clarify: However, when looking inside my container, everything inside my .dockerignore is still prevent such as my migration folders, venv, etc... Fwiw i dont think volume mounts are taken into account with a dockerignore, as it is used for the build context. The volume mount is a runtime thing. Also, as a performance boost, do this: WORKDIR /app COPY requirements.txt . RUN pip ... COPY . . This way your cache for PIP installs is only invalidated when your requirements change and not while doing some coding. Saves you time and some cycles. More on reddit.com
🌐 r/docker
7
1
March 24, 2024
Ignore in project virtual environment
Hi! I propose to add config settings.virtualenvs.ignore-in-project true flag to make it possible to use .cache/pypoetry/virtualenvs rather than .venv. Use case: build a Docker image: poery creates ... More on github.com
🌐 github.com
21
March 18, 2019
python 3.x - How to exclude the VENV in Docker PUSH - Stack Overflow
It's packaging the entire project with the venv, that's ~550 mb. I'd like to avoid that, if possible. I added the venv directory in the .dockerignore file, but that doesn't seem to help. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › microsoft › vscode-docker › issues › 3476
Add .venv to default .dockerignore · Issue #3476 · microsoft/vscode-docker
March 28, 2022 - I suggest that either the docker plugin add .venv as one of its defaults in .dockerignore, OR change the Flask tutorial to recommend building a virtual environment in the .env directory.
Author   matneh
🌐
Docker Community
forums.docker.com › general
COPY with --exclude does not apply to sub-directories (in general) - General - Docker Community Forums
September 19, 2024 - While I know, I can use .dockerignore for that, I wanted to try out COPY --exclude and found that COPY --exclude .venv . . Actually does not omit a/b/.venv. And neither does COPY --exclude */.venv . . But actually C…
🌐
Readthedocs
caliban.readthedocs.io › en › latest › recipes › dockerignore.html
dockerignore speeds up builds — Caliban documentation
If you don’t want to include any of these things in the Docker container that caliban builds for you, you can significantly speed up your builds by creating a file called .dockerignore in the directory of your project.
🌐
GitHub
github.com › sir-dunxalot › flask-boilerplate › blob › master › .dockerignore
flask-boilerplate/.dockerignore at master · sir-dunxalot/flask-boilerplate
March 18, 2019 - .venv · env/ venv/ ENV/ · # Spyder project settings · .spyderproject · .spyproject · · # Rope project settings · .ropeproject · · # mkdocs documentation · /site · · # mypy ·
Author   sir-dunxalot
Find elsewhere
🌐
GitHub
github.com › themattrix › python-pypi-template › blob › master › .dockerignore
python-pypi-template/.dockerignore at master · themattrix/python-pypi-template
.venv/ venv/ · # PyCharm · .idea · · # Python mode for VIM · .ropeproject · */.ropeproject · */*/.ropeproject · */*/*/.ropeproject · · # Vim swap files · *.swp · */*.swp ·
Author   themattrix
🌐
Astral
docs.astral.sh › uv › guides › integration › docker
Using uv in Docker | uv
5 days ago - services: example: build: . # ... develop: # Create a `watch` configuration to update the app # watch: # Sync the working directory with the `/app` directory in the container - action: sync path: . target: /app # Exclude the project virtual environment ignore: - .venv/ # Rebuild the image on changes to the `pyproject.toml` - action: rebuild path: ./pyproject.toml
🌐
Reddit
reddit.com › r/docker › is .dockerignore treated differently using docker compose?
r/docker on Reddit: Is .dockerignore treated differently using docker compose?
March 24, 2024 -

Previously, using a single Dockerfile with it's associated .dockerignore, it worked fine. However, when trying to follow this through with docker compose, it now fails to work. I will provide my structure.

I have uploaded my folder structure to https://imgur.com/65bjhat for easy reading.

My Dockerfile is,

# Use the official Python image as a base image
FROM python:3.9

Prevents Python from writing pyc files to disc (equivalent to python -B option)
ENV PYTHONDONTWRITEBYTECODE 1

Prevents Python from buffering stdout and stderr (equivalent to python -u option)
ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY . /app/

RUN pip install -r requirements.txt

My .dockerignore is,

venv/*
**/migrations
.dockerignore
Dockerfile
images/*

My docker-compose.yaml is

services:
  web:
    build: ./app
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./app:/usr/src/app/
    ports:
      - "8000:8000"
    env_file:
      - ./.dev.env
    depends_on:
      - db
  db:
    image: postgres:16
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=admin
      - POSTGRES_DB=radix_fitness_postgresql_db

volumes:
  postgres_data:

I run this using,

docker compose -f compose-dev.yaml up

However, when looking inside my container, everything inside my .dockerignore is still prevent such as my migration folders, venv, etc...

The only thing I did was switch to docker compose, but it seems to be treated identically as my Dockerfile inside of the app directory should be using the .dockerignore. I wonder if COPY is overriding the behaviour of .dockerignore?

🌐
GitHub
github.com › sdispater › poetry › issues › 967
Ignore in project virtual environment · Issue #967 · python-poetry/poetry
March 18, 2019 - Hi! I propose to add config settings.virtualenvs.ignore-in-project true flag to make it possible to use .cache/pypoetry/virtualenvs rather than .venv. Use case: build a Docker image: poery creates .cache/pypoetry/virtualenvs/* link proje...
Author   ADR-007-SoftServe
🌐
Medium
medium.com › @maheshgaikwad128 › exclude-files-and-folders-from-docker-build-using-dockerignore-965550f01aae
Exclude Files and Folders from Docker Build Using .dockerignore | by Maheshgaikwad | Medium
February 24, 2025 - __pycache__/ *.pyc *.pyo venv/ .env .DS_Store .idea/ For Go projects, exclude build binaries and cache directories: bin/ *.exe *.out *.log vendor/ .env .DS_Store · Keep it Up to Date: Regularly update your .dockerignore file as your project evolves. Test Builds: Ensure that essential files are not mistakenly excluded.
🌐
Reddit
reddit.com › r/docker › a collection of .dockerignore templates
A collection of .dockerignore templates : r/docker
November 4, 2020 - So if you are building on a remote machine with the remote Docker API, dockerignore can drastically reduce the amount of data you have to send it over the network. When building Python projects, I don't want my entire local virtualenv folder to get copied to the server. That can be a lot of extra data that I don't need. For this reason, I add my venv/ directory to my .dockerignore file the first thing.
🌐
GitHub
gist.github.com › KernelA › 04b4d7691f28e264f72e76cfd724d448
.dockerignore example for Python projects · GitHub
.dockerignore example for Python projects. GitHub Gist: instantly share code, notes, and snippets.
🌐
Reddit
reddit.com › r/python › why you don't need venv inside a docker container
r/Python on Reddit: Why you don't need venv inside a docker container
May 26, 2021 -

Isolation

The purpose of virtualenv is to isolate the system's packages from your app packages so that you don't have any version conflicts etc.

That makes sense because if you have installed python on your regular OS there might be multiple apps using python and every app might require different packages/versions.

But in a docker container your app is the only app requiring python, so there are actually only the packages installed, that your app needs.

So there is no point in using a virtualenv inside a docker container. You could also ask yourself why you don't create another virtualenv inside a virtualenv? Simply because there is no need to because it is already isolated.

But: The system installs a lot of packages by default that might conflict with app's packages

I've read this argument multiple times and they even posted some screenshots showing a lot of packages installed on the system by default.

So I checked what the official python package actually installs:

docker run python pip list
Package Version 
---------- ------- 
pip 21.1.2 
setuptools 57.0.0
wheel 0.36.2

As you can see there are no unnecessary packages installed like urllib, requests, etc.

The only time you get a lot of unwanted stuff is if you don't use the official image but instead use for example ubuntu and manually install python and pip etc.

But in that case, it was your decision to have unwanted stuff already installed...

Conclusion

As you can see there is no reason to use virtualenv inside a docker container, if you use the official images.

If you have multiple python apps that need different packages, create separate images for them and use something like docker-compose to manage them.

If you don't need real isolation (virtualenv isn't real isolation from the host os, since you can still access memory, disk space, etc.), why use containers at all?

Just install everything on the host OS (with venvs) or install another OS in a virtual machine and install everything there.

🌐
OneUptime
oneuptime.com › home › blog › how to use docker ignore files
How to Use Docker Ignore Files
January 25, 2026 - # .dockerignore - Essential exclusions for most projects # Version control .git .gitignore .gitattributes # Documentation *.md docs/ LICENSE # Docker files (prevent recursive builds) Dockerfile* docker-compose* .docker/ # IDE and editor files .idea/ .vscode/ *.swp *.swo *~ # OS files .DS_Store Thumbs.db # Test files **/test/ **/tests/ **/__tests__/ *.test.* *.spec.* coverage/ # Build artifacts (will be generated in container) dist/ build/ out/ target/ # Dependencies (will be installed in container) node_modules/ vendor/ venv/ .venv/ __pycache__/ *.pyc # Environment and secrets .env .env.* *.pe
🌐
Sourcery
sourcery.ai › blog › python-docker
A perfect way to Dockerize your Pipenv Python application
We can make a .dockerignore file to exclude files and directories from the image.
🌐
GitHub
github.com › astral-sh › uv-docker-example › blob › main › .dockerignore
uv-docker-example/.dockerignore at main · astral-sh/uv-docker-example
An example of using uv in Docker images. Contribute to astral-sh/uv-docker-example development by creating an account on GitHub.
Author   astral-sh