새로운 Docker 컨테이너에서 기존 Django 프로젝트 사용


0

같은 디렉토리에서 Django 프로젝트를 사용하여 Docker 컨테이너를 시작하려고합니다.

이 자습서 ( https://docs.docker.com/compose/django/#create-a-django-project )에 따라 사용할 수 있습니다

docker-compose run web django-admin startproject newproject .

docker-compose.yml에 정의 된 docker 컨테이너 내부에서 프로젝트를 시작하려면

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    #command: python3 manage.py runserver 0.0.0.0:8000
    command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn things.wsgi -b 0.0.0.0:8000"
    volumes:
      - .:/code
    expose:
      - "8000"
    depends_on:
      - db
  nginx:
    image: nginx:latest
    container_name: NGINXDOCKERNAME
    ports:
      - "8000:8000"
    volumes:
      - .:/code
      - ./config/nginx:/etc/nginx/conf.d
    ports:
      - "8000:8000"
    depends_on:
      - web

및 dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
VOLUME /code

django-admin startproject newproject를 어떻게 사용합니까? 컨테이너 외부에서 (즉, docker-machine 명령 줄에서) 프로젝트를 컨테이너에 복사합니까?

이제 컨테이너 내부에서 프로젝트를 시작하면 서버가 시작되고 올바른 포트에서 프로젝트를 볼 수 있지만 컨테이너 외부에서 프로젝트를 시작하면 (하지만 동일한 디렉토리에서 로컬로) 웹 및 nginx 컨테이너가 올바르게 시작되지 않습니다.

Windows 10 버전 1709에서 도커 머신을 실행 중입니다.

웹 컨테이너 시작의 출력은 아래에 있으며 요청시 서버의 출력이 이어집니다.

건배 Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect web_1 | self.connection = self.get_new_connection(conn_params) web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection web_1 | connection = Database.connect(**conn_params) web_1 | File "/usr/local/lib/python3.6/site-packages/psycopg2/init.py", line 130, in connect web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) web_1 | psycopg2.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "db" (172.19.0.2) and accepting web_1 | TCP/IP connections on port 5432? web_1 | web_1 | web_1 | The above exception was the direct cause of the following exception: web_1 | web_1 | Traceback (most recent call last): web_1 | File "manage.py", line 22, in web_1 | execute_from_command_line(sys.argv) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 364, in execute_from_command_line web_1 | utility.execute() web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 356, in execute web_1 | self.fetch_command(subcommand).run_from_argv(self.argv) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv web_1 | self.execute(*args, **cmd_options) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute web_1 | output = self.handle(*args, **options) web_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 110, in handle web_1 | loader.check_consistent_history(connection) web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 282, in check_consistent_history web_1 | applied = recorder.applied_migrations() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations web_1 | self.ensure_schema() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema web_1 | if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()): web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 254, in cursor web_1 | return self._cursor() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 229, in _cursor web_1 | self.ensure_connection() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 94, in exit web_1 | six.reraise(dj_exc_type, dj_exc_value, traceback) web_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise web_1 | raise value.with_traceback(tb) web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect web_1 | self.connection = self.get_new_connection(conn_params) web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection web_1 | connection = Database.connect(**conn_params) web_1 | File "/usr/local/lib/python3.6/site-packages/psycopg2/init.py", line 130, in connect web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "db" (172.19.0.2) and accepting web_1 | TCP/IP connections on port 5432?

서버 출력

NGINXDOCKERNAME | 2017/11/21 19:56:28 [error] 5#5: *1 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.99.1, server: localhost, request: "GET /polls/ HTTP/1.1", upstream: "http://172.19.0.3:8000/polls/", host: "192.168.99.100:8000"

답변:


1

귀하의 질문에 대한 답변은 https://docs.docker.com/compose/django/#connect-the-database 에서 찾을 수 있습니다 . 링크에 설명 된대로 데이터베이스를 실제로 설정하면 DATABASE 구성이 다음과 같이됩니다.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

아무 문제가 없을 것입니다.

listen_addresses = '*'vim을 사용하여 설정할 수도 있습니다 . 이에 대한 자세한 내용은 http://www.mozmorris.com/2011/11/15/configure-postgresql-to-accept-tcpip-connections.html 링크를 시도하십시오.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.