【发布时间】:2020-08-26 22:20:30
【问题描述】:
问题
您好,我是 Docker 新手。我想在我的 Django 项目中使用 .dockerignore 忽略一些文件和目录。一开始没有忽略任何文件,后来在stackoverflow中搜索,发现是因为docker-compose.yml中的volumes,所以我把它注释掉了。但是现在有些文件和目录被忽略了,但有些不是( pycache , db.sqlite3 )。我遇到了很多问题,但找不到任何解决方案。
项目结构
-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
命令
# 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
预期输出
# Byte-compiled / optimized / DLL files
__pycache__/ # ignored
**/migrations # ignored
src/media # ignored
src/db.sqlite3 # ignored
Procfile # ignored
.git # ignored
原始输出
# Byte-compiled / optimized / DLL files
__pycache__/ # NOT ignored
**/migrations # ignored
src/media # ignored
src/db.sqlite3 # NOT ignored
Procfile # ignored
.git # ignored
尝试
__pycache__/
**/__pycache__
**/*__pycache__
**/*__pycache__*
**/*__pycache__/
**/__pycache__/
*/db.sqlite3
db.sqlite3
【问题讨论】:
-
你能把这个减少到minimal reproducible example吗?您希望忽略哪个文件,不是吗? (或者不被忽略,是吗?)如果你
docker-compose run web ls,你会得到不同的输出吗? -
@DavidMaze 我期待 pycache 和 db.sqlite3 被忽略。在我运行命令
sudo docker-compose run web ls src/后,我没有看到 db.sqlite3,所以我猜它被忽略了,但 pycache 仍然存在。
标签: django docker docker-compose