我的 .dockerignore 文件为什么不起作用?

huangapple go评论126阅读模式
英文:

Why is my .dockerignore file not working?

问题

I have a .dockerignore file with the following contents:

  1. **/__pycache__
  2. **/.venv
  3. **/.git
  4. **/.gitignore
  5. **/.dockerignore
  6. **/dockerfile*
  7. **/docker-compose*
  8. postgres_data/

and this is my docker-compose.yml

  1. version: '3.8'
  2. services:
  3. django_app:
  4. image: django_image
  5. container_name: Django
  6. build:
  7. context: .
  8. dockerfile: django_app/dockerfile
  9. command:
  10. - /bin/sh
  11. - -c
  12. - |
  13. python manage.py makemigrations
  14. python manage.py migrate --no-input
  15. python manage.py collectstatic --no-input
  16. gunicorn configurations.wsgi:application --bind 0.0.0.0:8000
  17. volumes:
  18. - ./django_app:/django_app
  19. ports:
  20. - 8000:8000
  21. env_file:
  22. - django_app/.envs/.env.django
  23. - django_app/.envs/.env.postgres
  24. depends_on:
  25. - postgres_host
  26. postgres_host:
  27. container_name: Postgres
  28. image: postgres:15-bookworm
  29. restart: unless-stopped
  30. ports:
  31. - 5432:5432
  32. volumes:
  33. - ./postgres_data/:/var/lib/postgresql/data/
  34. env_file:
  35. - django_app/.envs/.env.postgres

However, when I build my Docker image using docker-compose up, all of the files in the .dockerignore file are still being copied into the container. What could be causing this issue?

this is ls -la output

  1. total 36
  2. drwxrwxr-x 5 neptune neptune 4096 Aug 4 15:46 .
  3. drwxr-xr-x 3 neptune neptune 4096 Aug 2 21:27 ..
  4. drwxrwxr-x 7 neptune neptune 4096 Aug 4 17:24 django_app
  5. -rw-rw-r-- 1 neptune neptune 868 Aug 4 15:45 docker-compose.yml
  6. -rw-rw-r-- 1 neptune neptune 156 Aug 4 18:14 .dockerignore
  7. drwxrwxr-x 8 neptune neptune 4096 Aug 2 21:30 .git
  8. -rw-rw-r-- 1 neptune neptune 225 Aug 4 15:47 .gitignore
  9. drwx------ 19 999 root 4096 Aug 4 18:27 postgres_data
  10. -rw-rw-r-- 1 neptune neptune 848 Aug 4 12:08 README.md

this is ls -la django_app/

  1. total 180
  2. drwxrwxr-x 7 neptune neptune 4096 Aug 4 17:24 .
  3. drwxrwxr-x 5 neptune neptune 4096 Aug 4 15:46 ..
  4. drwxrwxr-x 4 neptune neptune 4096 Aug 3 18:37 account
  5. drwxrwxr-x 3 neptune neptune 4096 Aug 3 18:52 configurations
  6. -rw-r--r-- 1 root root 139264 Aug 4 17:07 db.sqlite3
  7. -rw-rw-r-- 1 neptune neptune 361 Aug 4 16:59 dockerfile
  8. drwxrwxr-x 2 neptune neptune 4096 Aug 4 12:18 .envs
  9. -rwxrwxr-x 1 neptune neptune 670 Aug 2 21:38 manage.py
  10. -rw-rw-r-- 1 neptune neptune 220 Aug 4 17:13 requirements.txt
  11. drwxr-xr-x 4 root root 4096 Aug 4 18:22 staticfiles
  12. drwxrwxr-x 5 neptune neptune 4096 Aug 4 10:50 venv

and this is root@a34210cf0029:/django_app# ls -la output: (inside container after run docker)

  1. total 180
  2. drwxrwxr-x 7 1000 1000 4096 Aug 4 13:54 .
  3. drwxr-xr-x 1 root root 4096 Aug 4 14:47 ..
  4. drwxrwxr-x 2 1000 1000 4096 Aug 4 08:48 .envs
  5. drwxrwxr-x 4 1000 1000 4096 Aug 3 15:07 account
  6. drwxrwxr-x 3 1000 1000 4096 Aug 3 15:22 configurations
  7. -rw-r--r-- 1 root root 139264 Aug 4 13:37 db.sqlite3
  8. -rw-rw-r-- 1 1000 1000 361 Aug 4 13:29 dockerfile
  9. -rwxrwxr-x 1 1000 1000 670 Aug 2 18:08 manage.py
  10. -rw-rw-r-- 1 1000 1000 220 Aug 4 13:43 requirements.txt
  11. drwxr-xr-x 4 root root 4096 Aug 4 15:01 staticfiles
  12. drwxrwxr-x 5 1000 1000 4096 Aug 4 07:20 venv

how can i fix this?

英文:

I have a .dockerignore file with the following contents:

  1. **/__pycache__
  2. **/.venv
  3. **/.git
  4. **/.gitignore
  5. **/.dockerignore
  6. **/dockerfile*
  7. **/docker-compose*
  8. postgres_data/

and this is my docker-compose.yml

  1. version: '3.8'
  2. services:
  3. django_app:
  4. image: django_image
  5. container_name: Django
  6. build:
  7. context: .
  8. dockerfile: django_app/dockerfile
  9. command:
  10. - /bin/sh
  11. - -c
  12. - |
  13. python manage.py makemigrations
  14. python manage.py migrate --no-input
  15. python manage.py collectstatic --no-input
  16. gunicorn configurations.wsgi:application --bind 0.0.0.0:8000
  17. volumes:
  18. - ./django_app:/django_app
  19. ports:
  20. - 8000:8000
  21. env_file:
  22. - django_app/.envs/.env.django
  23. - django_app/.envs/.env.postgres
  24. depends_on:
  25. - postgres_host
  26. postgres_host:
  27. container_name: Postgres
  28. image: postgres:15-bookworm
  29. restart: unless-stopped
  30. ports:
  31. - 5432:5432
  32. volumes:
  33. - ./postgres_data/:/var/lib/postgresql/data/
  34. env_file:
  35. - django_app/.envs/.env.postgres

However, when I build my Docker image using docker-compose up, all of the files in the .dockerignore file are still being copied into the container. What could be causing this issue?

this is ls -la output

  1. total 36
  2. drwxrwxr-x 5 neptune neptune 4096 Aug 4 15:46 .
  3. drwxr-xr-x 3 neptune neptune 4096 Aug 2 21:27 ..
  4. drwxrwxr-x 7 neptune neptune 4096 Aug 4 17:24 django_app
  5. -rw-rw-r-- 1 neptune neptune 868 Aug 4 15:45 docker-compose.yml
  6. -rw-rw-r-- 1 neptune neptune 156 Aug 4 18:14 .dockerignore
  7. drwxrwxr-x 8 neptune neptune 4096 Aug 2 21:30 .git
  8. -rw-rw-r-- 1 neptune neptune 225 Aug 4 15:47 .gitignore
  9. drwx------ 19 999 root 4096 Aug 4 18:27 postgres_data
  10. -rw-rw-r-- 1 neptune neptune 848 Aug 4 12:08 README.md

this is ls -la django_app/

  1. total 180
  2. drwxrwxr-x 7 neptune neptune 4096 Aug 4 17:24 .
  3. drwxrwxr-x 5 neptune neptune 4096 Aug 4 15:46 ..
  4. drwxrwxr-x 4 neptune neptune 4096 Aug 3 18:37 account
  5. drwxrwxr-x 3 neptune neptune 4096 Aug 3 18:52 configurations
  6. -rw-r--r-- 1 root root 139264 Aug 4 17:07 db.sqlite3
  7. -rw-rw-r-- 1 neptune neptune 361 Aug 4 16:59 dockerfile
  8. drwxrwxr-x 2 neptune neptune 4096 Aug 4 12:18 .envs
  9. -rwxrwxr-x 1 neptune neptune 670 Aug 2 21:38 manage.py
  10. -rw-rw-r-- 1 neptune neptune 220 Aug 4 17:13 requirements.txt
  11. drwxr-xr-x 4 root root 4096 Aug 4 18:22 staticfiles
  12. drwxrwxr-x 5 neptune neptune 4096 Aug 4 10:50 venv

and this is root@a34210cf0029:/django_app# ls -la output: (inside container after run docker)

  1. total 180
  2. drwxrwxr-x 7 1000 1000 4096 Aug 4 13:54 .
  3. drwxr-xr-x 1 root root 4096 Aug 4 14:47 ..
  4. drwxrwxr-x 2 1000 1000 4096 Aug 4 08:48 .envs
  5. drwxrwxr-x 4 1000 1000 4096 Aug 3 15:07 account
  6. drwxrwxr-x 3 1000 1000 4096 Aug 3 15:22 configurations
  7. -rw-r--r-- 1 root root 139264 Aug 4 13:37 db.sqlite3
  8. -rw-rw-r-- 1 1000 1000 361 Aug 4 13:29 dockerfile
  9. -rwxrwxr-x 1 1000 1000 670 Aug 2 18:08 manage.py
  10. -rw-rw-r-- 1 1000 1000 220 Aug 4 13:43 requirements.txt
  11. drwxr-xr-x 4 root root 4096 Aug 4 15:01 staticfiles
  12. drwxrwxr-x 5 1000 1000 4096 Aug 4 07:20 venv

how can i fix this ?

答案1

得分: 1

The volumes: block causes the /django_app directory in the container to be connected to the host system. You are not seeing the contents of the image at all; .dockerignore doesn't apply; and if your Dockerfile does any work like dynamically building files or rearranging the directory tree, this gets hidden.

You should delete the volumes: block, and then the container filesystem will look like what's in the image. As a bonus, you also won't need a local copy of the application source to run the image, and you'll get consistent behavior running the image anywhere (like a production system) independent of what's on the host.

英文:

The volumes: block causes the /django_app directory in the container to be connected to the host system. You are not seeing the contents of the image at all; .dockerignore doesn't apply; and if your Dockerfile does any work like dynamically building files or rearranging the directory tree, this gets hidden.

You should delete the volumes: block, and then the container filesystem will look like what's in the image. As a bonus, you also won't need a local copy of the application source to run the image, and you'll get consistent behavior running the image anywhere (like a production system) independent of what's on the host.

huangapple
  • 本文由 发表于 2023年8月4日 23:08:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定