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

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

Why is my .dockerignore file not working?

问题

I have a .dockerignore file with the following contents:

**/__pycache__
**/.venv
**/.git
**/.gitignore
**/.dockerignore
**/dockerfile*
**/docker-compose*

postgres_data/

and this is my docker-compose.yml

version: '3.8'

services:
  django_app:
    image: django_image
    container_name: Django
    build:
      context: .
      dockerfile: django_app/dockerfile
    command:
      - /bin/sh
      - -c
      - |
        python manage.py makemigrations
        python manage.py migrate --no-input
        python manage.py collectstatic --no-input
        gunicorn configurations.wsgi:application --bind 0.0.0.0:8000        
    volumes:
      - ./django_app:/django_app
    ports:
      - 8000:8000
    env_file:
      - django_app/.envs/.env.django
      - django_app/.envs/.env.postgres
    depends_on:
      - postgres_host

  postgres_host:
    container_name: Postgres
    image: postgres:15-bookworm
    restart: unless-stopped
    ports:
      - 5432:5432
    volumes:
      - ./postgres_data/:/var/lib/postgresql/data/
    env_file:
      - 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

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

this is ls -la django_app/

total 180
drwxrwxr-x 7 neptune neptune   4096 Aug  4 17:24 .
drwxrwxr-x 5 neptune neptune   4096 Aug  4 15:46 ..
drwxrwxr-x 4 neptune neptune   4096 Aug  3 18:37 account
drwxrwxr-x 3 neptune neptune   4096 Aug  3 18:52 configurations
-rw-r--r-- 1 root    root    139264 Aug  4 17:07 db.sqlite3
-rw-rw-r-- 1 neptune neptune    361 Aug  4 16:59 dockerfile
drwxrwxr-x 2 neptune neptune   4096 Aug  4 12:18 .envs
-rwxrwxr-x 1 neptune neptune    670 Aug  2 21:38 manage.py
-rw-rw-r-- 1 neptune neptune    220 Aug  4 17:13 requirements.txt
drwxr-xr-x 4 root    root      4096 Aug  4 18:22 staticfiles
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)

total 180
drwxrwxr-x 7 1000 1000   4096 Aug  4 13:54 .
drwxr-xr-x 1 root root   4096 Aug  4 14:47 ..
drwxrwxr-x 2 1000 1000   4096 Aug  4 08:48 .envs
drwxrwxr-x 4 1000 1000   4096 Aug  3 15:07 account
drwxrwxr-x 3 1000 1000   4096 Aug  3 15:22 configurations
-rw-r--r-- 1 root root 139264 Aug  4 13:37 db.sqlite3
-rw-rw-r-- 1 1000 1000    361 Aug  4 13:29 dockerfile
-rwxrwxr-x 1 1000 1000    670 Aug  2 18:08 manage.py
-rw-rw-r-- 1 1000 1000    220 Aug  4 13:43 requirements.txt
drwxr-xr-x 4 root root   4096 Aug  4 15:01 staticfiles
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:

**/__pycache__
**/.venv
**/.git
**/.gitignore
**/.dockerignore
**/dockerfile*
**/docker-compose*

postgres_data/

and this is my docker-compose.yml

version: '3.8'

services:
  django_app:
    image: django_image
    container_name: Django
    build:
      context: .
      dockerfile: django_app/dockerfile
    command:
      - /bin/sh
      - -c
      - |
        python manage.py makemigrations
        python manage.py migrate --no-input
        python manage.py collectstatic --no-input
        gunicorn configurations.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./django_app:/django_app
    ports:
      - 8000:8000
    env_file:
      - django_app/.envs/.env.django
      - django_app/.envs/.env.postgres
    depends_on:
      - postgres_host

  postgres_host:
    container_name: Postgres
    image: postgres:15-bookworm
    restart: unless-stopped
    ports:
      - 5432:5432
    volumes:
      - ./postgres_data/:/var/lib/postgresql/data/
    env_file:
      - 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

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

this is ls -la django_app/

total 180
drwxrwxr-x 7 neptune neptune   4096 Aug  4 17:24 .
drwxrwxr-x 5 neptune neptune   4096 Aug  4 15:46 ..
drwxrwxr-x 4 neptune neptune   4096 Aug  3 18:37 account
drwxrwxr-x 3 neptune neptune   4096 Aug  3 18:52 configurations
-rw-r--r-- 1 root    root    139264 Aug  4 17:07 db.sqlite3
-rw-rw-r-- 1 neptune neptune    361 Aug  4 16:59 dockerfile
drwxrwxr-x 2 neptune neptune   4096 Aug  4 12:18 .envs
-rwxrwxr-x 1 neptune neptune    670 Aug  2 21:38 manage.py
-rw-rw-r-- 1 neptune neptune    220 Aug  4 17:13 requirements.txt
drwxr-xr-x 4 root    root      4096 Aug  4 18:22 staticfiles
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)

total 180
drwxrwxr-x 7 1000 1000   4096 Aug  4 13:54 .
drwxr-xr-x 1 root root   4096 Aug  4 14:47 ..
drwxrwxr-x 2 1000 1000   4096 Aug  4 08:48 .envs
drwxrwxr-x 4 1000 1000   4096 Aug  3 15:07 account
drwxrwxr-x 3 1000 1000   4096 Aug  3 15:22 configurations
-rw-r--r-- 1 root root 139264 Aug  4 13:37 db.sqlite3
-rw-rw-r-- 1 1000 1000    361 Aug  4 13:29 dockerfile
-rwxrwxr-x 1 1000 1000    670 Aug  2 18:08 manage.py
-rw-rw-r-- 1 1000 1000    220 Aug  4 13:43 requirements.txt
drwxr-xr-x 4 root root   4096 Aug  4 15:01 staticfiles
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:

确定