vkk*_*k07 1 django django-rest-framework docker docker-compose
我对使用 Docker 时如何使用 Django 应用程序(创建/使用)感到困惑。一些教程建议在启动 Web docker 容器后使用命令 startapp(我正在使用 docker-compose 来启动容器)。但由于文件是在该容器内创建的,我如何从本地开发机器向该容器添加代码?而且,创建这样的应用程序来编辑代码似乎并不正确......
\n\n到目前为止,我一直在使用以下结构,它启动容器并运行良好。但只有一个“应用程序”,即待办事项\n(取自https://github.com/realpython/dockerizing-django)
\n\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nginx\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sites-enabled\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 django_project\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 production.yml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tmp.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 web\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Pipfile\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Pipfile.lock\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker_django\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 apps\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 todo\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 admin.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 static\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.css\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _base.html\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dashboard.html\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 home.html\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 urls.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 views.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 settings.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 urls.py\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wsgi.py\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 manage.py\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 requirements.txt\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 shell-script.sh\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tests\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_env_settings.py\n
Run Code Online (Sandbox Code Playgroud)\n\n当我使用上述结构时,我无法在本地创建应用程序,因为我们必须使用 manage.py 来创建应用程序,但我需要导航到 apps 文件夹才能执行此操作,但无法访问 manager.py。因此,我尝试为manage.py提供完整的abs路径,但它抱怨SeTTINGS_MODULE SECRET_KEY错误。
\n\n使用 Docker-Compose 时使用 django 应用程序的正确方法是什么?
\n\n我需要改变上面的结构吗?或者我应该改变我的工作流程?
\n\n编辑:
\n\n我的码头工人撰写:
\n\nversion: \'3.7\'\n\nservices:\n web:\n restart: always\n build: ./web\n expose:\n - "8000"\n links:\n - postgres:postgres\n - redis:redis\n volumes:\n - web-django:/usr/src/app\n - web-static:/usr/src/app/static\n env_file: .env\n environment:\n DEBUG: \'true\'\n command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000\n\n nginx:\n restart: always\n build: ./nginx/\n ports:\n - "80:80"\n volumes:\n - web-static:/usr/src/app/static\n links:\n - web:web\n\n postgres:\n restart: always\n image: postgres:latest\n ports:\n - "5432:5432"\n volumes:\n - pgdata:/var/lib/postgresql/data/\n\n pgadmin:\n restart: always\n image: fenglc/pgadmin4\n ports:\n - "5050:5050"\n volumes:\n - pgadmindata:/var/lib/pgadmin/data/\n environment:\n DEFAULT_USER: \'pgadmin4@pgadmin.org\'\n DEFAULT_PASSWORD: \'admin\'\n\n redis:\n restart: always\n image: redis:latest\n ports:\n - "6379:6379"\n volumes:\n - redisdata:/data\n\nvolumes:\n web-django:\n web-static:\n pgdata:\n redisdata:\n pgadmindata:\n
Run Code Online (Sandbox Code Playgroud)\n\n我的 Dockerfile 在 web 文件夹中:
\n\nFROM python:3.7-slim\n\n# Set environment variables\n\nENV PYTHONDONTWRITEBYTECODE 1\nENV PYTHONUNBUFFERED 1\n\nRUN mkdir -p /usr/src/app\nWORKDIR /usr/src/app\n\nADD Pipfile /usr/src/app\nADD Pipfile.lock /usr/src/app\n\nRUN python -m pip install --upgrade pip\n\nRUN python -m pip install pipenv\n\nCOPY requirements.txt requirements.txt\nRUN pipenv install --system\n\nCOPY . /usr/src/app\n
Run Code Online (Sandbox Code Playgroud)\n
你的结构是正确的。您正在寻找的是一个卷,为了使主机上的django项目在容器内可用,您可以在项目中创建您喜欢的任何内容,并且更改将在容器上生效。
\n\n例如:
\n\n结构是:
\n\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 django\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 entireDjangoAppFiles\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 docker-compose.yml\n
Run Code Online (Sandbox Code Playgroud)\n\n说这是我的 django dockerfile
\n\nFROM python:3\nENV PYTHONUNBUFFERED 1\nRUN mkdir /code\nWORKDIR /code\nRUN pip install Django psycopg2\nEXPOSE 8000\nCMD python manage.py runserver 0.0.0.0:8000\n
Run Code Online (Sandbox Code Playgroud)\n\n我的码头工人组成:
\n\nversion: \'3.7\'\nservices:\n django:\n build:\n context: django\n dockerfile: Dockerfile\n ports:\n - "8000:8000"\n volumes:\n - "./django:/code" \n
Run Code Online (Sandbox Code Playgroud)\n\n现在我在 django 目录中所做的任何更改也将应用于容器的/code
目录
编辑
\n\n我们的 docker-compose 文件不相似...您正在使用命名卷而不是通常的安装。这些卷正在 docker 自己的卷目录中创建,容器可以使用它们,但没有任何信息告诉 docker 您希望这些卷包含您的应用程序 - 所以它们是空的。要解决此问题,您可以从 docker-compose 中的选项中删除它们volumes
,并更喜欢挂载卷:
version: \'3.7\'\n\nservices:\n web:\n restart: always\n build: ./web\n expose:\n - "8000"\n links:\n - postgres:postgres\n - redis:redis\n volumes:\n - .web:/usr/src/app #mount the project dir\n - .path/to/static/files/dir:/usr/src/app/static #mount the static files dir\n env_file: .env\n environment:\n DEBUG: \'true\'\n command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000\n\n nginx:\n restart: always\n build: ./nginx/\n ports:\n - "80:80"\n volumes:\n - web-static:/usr/src/app/static\n links:\n - web:web\n\n postgres:\n restart: always\n image: postgres:latest\n ports:\n - "5432:5432"\n volumes:\n - pgdata:/var/lib/postgresql/data/\n\n pgadmin:\n restart: always\n image: fenglc/pgadmin4\n ports:\n - "5050:5050"\n volumes:\n - pgadmindata:/var/lib/pgadmin/data/\n environment:\n DEFAULT_USER: \'pgadmin4@pgadmin.org\'\n DEFAULT_PASSWORD: \'admin\'\n\n redis:\n restart: always\n image: redis:latest\n ports:\n - "6379:6379"\n volumes:\n - redisdata:/data\n\nvolumes:\n #web-django:\n #web-static:\n pgdata:\n redisdata:\n pgadmindata:\n
Run Code Online (Sandbox Code Playgroud)\n\n关于其他命名卷的注释 - 如果您想知道为什么必须使用它们 - 它们是数据库卷,应该由容器填充。
\n