Docker与Django和Postgres一起

时间:2018-11-15 22:00:32

标签: django docker

在settings.py中放置了以下配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql$
        'NAME': 'database1',                   # Or path to database file if $
        # The following settings are not used with sqlite3:
        'USER': 'database1_role',
        'PASSWORD': 'database1_password',
        'HOST': 'database1',                      # Empty for localhost throu$
        'PORT': '5432',                      # Set to empty string for defaul$
        'ATOMIC_REQUESTS': True
    }
}

docker-compose.yml:

version: '3'

services:

  # web container, with django + gunicorn
  djangoapp:
    build: .
    environment:
      - DJANGO_SETTINGS_MODULE
    volumes:
      - .:/opt/services/djangoapp/src
      - static:/opt/services/djangoapp/static
      - media:/opt/services/djangoapp/media
    networks:
      - database1_network
      - nginx_network
    depends_on:
      - database1

  # reverse proxy container (nginx)
  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static:/opt/services/djangoapp/static
      - media:/opt/services/djangoapp/media
    networks:
      - nginx_network
    depends_on:
      - djangoapp

  # database containers, one for each db
  database1:
    image: postgres:10
    environment:
      POSTGRES_USER: database1_role
      POSTGRES_PASSWORD: database1_password
      POSTGRES_DB: database1
    ports:
      - "5432"
    volumes:
      - database1_volume:/var/lib/postgresql/data
    networks:
      - database1_network


networks:
  database1_network:
    driver: bridge
  nginx_network:
    driver: bridge

volumes:
  database1_volume:
  static:
  media:

每当我尝试使用Webbrowser访问我的网站时,我仍然会收到以下错误消息:

  

/ FATAL发生操作错误:用户密码验证失败

     

“ database1_role”

以前,我曾经有一条消息说我的数据库甚至不存在/ psycopg2无法与其建立任何连接。 我已经通过替换两个文件中的数据库名称解决了它: settings.py和POSTGRES_DB,但是目前我还不知道现在该怎么做。

为了运行此设置,我使用以下两个命令:

docker-compose build
docker-compose up

命令:

  

docker-compose ps

明确表示正在运行三个组件:

              Name                             Command               State    Ports
-----------------------------------------------------------------------------------
services_database1_1_f84f6d3c38e0   docker-entrypoint.sh postgres    Exit 0        
services_djangoapp_1_da56c77d50ff   gunicorn -c config/gunicor ...   Exit 0        
services_nginx_1_c6e0edb717c0       nginx -g daemon off;             Exit 0  

0 个答案:

没有答案