.DockerIgnore WSL2 --Not Ignoring

Hello.

My .dockerignore file does not seem to ‘ignore’ directories.

In the build logs, the .dockerignore file is being loaded. => [internal] load .dockerignore
That said, the only .dockerignore command that works is to ignore everything with ‘*’

In my .dockerignore in the project root, I’ve tried
ignore_this_dir
**/ignore_this_dir
*/ignore_this_dir
ignore_this_dir?

What am I missing?

Docker Desktop v4.15.0
Windows 10 WSL 2
:white_check_mark: Use the WSL 2 based Engine
:white_check_mark: Use Docker Compose V2

running in WSL2 Ubuntu Terminal:
docker system prune -a
docker-compose up --build

Much appreciation in advance

  1. Can you show a directory tree to show what you want to ignore?
  2. What is your build context in the compose file?

In your build context a simple

/ignore_this_dir

should ignore the whole directory. If you want to ignore

Please use code blocks for commands and codes otherwise your post could be altered by forum and it is hard to notice your shared commands even without that
.

I’d like to ignore the ‘archive’
/my_project

  • archive
  • modules_dir
  • main.py
  • DockerFile
  • .dockerignore
  • .gitignore

First 4 lines of DockerFile:

# syntax = docker/dockerfile:1
FROM python:3.9.9-slim-buster

ENV PIP_CACHE_DIR=/var/cache/buildkit/pip
RUN mkdir -p $PIP_CACHE_DIR
WORKDIR /app

in the .dockerignore file running in pycharm pro windows…running wsl2
image

Thanks :slight_smile:

Okay, but please, don’t ignore the end of my post :slight_smile:

You can see how your code was changed right? I guess the first line should have been a comment not a title.

Regarding the question, your dockerignore seems right. I tested it on macOS (so not on Windows) with docker run and also with docker compose.

Well, this first 4 lines doesn’t seem relevant, but I haven’t asked for a Dockerfile so no problem.

If you want to simplify, please don’t change the name of the directories unless the original name is secret. :slight_smile:

How did you realize it didn’t work? Do you have a copy command that copies everything from the project root?

How did you realize it didn’t work?

In the running docker container, the ‘archive’ directory is still there.

There are so many strange happenings with WSL2 and Windows 10.
Perhaps, someone on that system has an idea

Can anyone give confirmation that .dockerignore works on WSL2 Windows?
Or, is there a more verbose log file that docker produces–how it is parsing .dockerignore?

Thanks

It is possible that I still don’t understand your environment correctly. I tried it on my Windows and it worked.

Please, try this:

Folder structure

Dockerfile
archive/test.txt
a/archive/test.txt
b/archive/test.txt
.dockerignore

Content of Dockerfile:

FROM alpine
COPY . /app

Content of .dockerignore

**/archive
archive

The content of the txt files doesn’t matter, just create empty txt files.

Run this command to build an image:

docker build . -t localhost/test

Run this command to test the result:

docker run --rm localhost/test find /app

You should get this:

/app
/app/Dockerfile
/app/a
/app/b
/app/.dockerignore

If you get this, we know the issue does not always happen. If you have different result (the order of the lines doesn’t matter) then something is different on our hosts, or there is something wrong with the encoding of the files.

That works :slight_smile: The only difference -I’m using a docker-compose.yml file for my project
Mine looks looks like this

version: "3.9"
services:
  web:
    build: .
    image: 123456789.thing.domain.com/repo:1.2.3.4
    ports:
      - "8889:8889"
    volumes:
      - .:/app

is there a default yml file you would use for the debug?

I think I got it. The bind mount was overwriting dockerignore
I changed the ‘volume’ settings from:

volumes:
  - .:/app

to

volumes:
  - ./vol:/app/vol

Wait, the bind mount should not affect the build.
Did that really solved everything?

Update:

Okay, I edited my edited comment… I thought I was wrong first, but I understand now the build was right but you mounted the files to the same folder where you copied the files during the build. Thanks for sharing the solution. I really should have asked for the compose file in the beginning :slight_smile:

1 Like

this worked for me, thanks.