2017-05-08 3 views
2

Django로 구동되는 PostgreSQL 데이터베이스와 REST API를 별도의 Docker 컨테이너에 설치하여 응용 프로그램을 실행하고 싶습니다. 지금까지 API가 Docker에서 SQLite 데이터베이스에 연결되어 실행 중이었지만 대신 PostgreSQL 데이터베이스에 연결하려고하는데 문제가 있습니다.Docker 컨테이너의 PostgreSQL 데이터베이스에 연결

부두 노동자의 docker-compose.yml 파일 :

version: '2' 
services: 
    postgres: 
     image: postgres 
    api: 
     build: . 
     command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:1337" 
     volumes: 
      - .:/usr/src/app 
     ports: 
      - "1337:1337" 
     depends_on: 
      - postgres 

장고의 settings.py (기본 postgres 이미지가 문서에 따라 작동 기본 설정을 사용하여) :

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

나는 결국 docker-compose up로 응용 프로그램을 실행하면 이 오류가 throw됩니다.

api_1  |  connection = Database.connect(**conn_params) 
api_1  | File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect 
api_1  |  conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 
api_1  | django.db.utils.OperationalError: could not connect to server: Connection refused 
api_1  | Is the server running on host "localhost" (::1) and accepting 
api_1  | TCP/IP connections on port 5432? 
api_1  | could not connect to server: Connection refused 
api_1  | Is the server running on host "localhost" (127.0.0.1) and accepting 
api_1  | TCP/IP connections on port 5432? 
api_1  | 
orchestrator_api_1 exited with code 1 

내가 뭘 잘못하고 있니?

+1

응용 프로그램 컨테이너가 postgres를 실행하지 않아 localhost를 참조하면 안됩니다. https://docs.docker.com/compose/django/#create-a-django-project –

답변

3

docker-compose를 사용하면 호스트 이름을 통해 서비스를 "발견"합니다. 데이터베이스 서비스는 레이블 포스트 그레스으로 정의됩니다. 응용 프로그램 구성에서 호스트 이름으로 사용하십시오.

또한 암호와 DB 이름이 앱 구성과 일치해야합니다. 이 포스트 그레스 서비스에 대한 환경 변수를 통해 수행됩니다

services: 
    postgres: 
    environment: 
     - POSTGRES_PASSWORD: "mysecretpassword" 
     - POSTGRES_DB: "wgomanager" 
    # rest of docker-compose.yml 

Reffer을 image docs 얼마나 다양한 ENV에. vars는 서비스 구성에 영향을줍니다.

+0

서비스 검색을 원할 것입니다. 고마워, 알았어! – faviouz

관련 문제