Pycharm Docker调试器无法连接到Docker中的数据库

时间:2018-09-28 11:49:04

标签: django postgresql docker debugging pycharm

我正在运行PyCharm 2018.2版本。 我的django代码在docker实例上运行,而Postgres在另一个docker实例上运行。 我的docker-compose.yml文件在这里

    version: "2.3"

services:
  cv_db:
    container_name: cv_db
    image: postgres:10.3-alpine
    restart: always
    ports:
      - "9081:5432"
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: ****
      POSTGRES_DB: cv
    volumes:
      - cv_db:/var/lib/postgresql/data
    expose:
      - "9081:5432"

  cv_redis:
    container_name: cv_redis
    image: redis:4.0.5-alpine
    restart: always
    ports:
      - "6379:6379"

  cv:
    container_name: cv
    image: cv
    restart: always
    depends_on:
      - cv_redis
      - cv_db
    ports:
      - "9080:9080"
      - "8080:8080"
    expose:
      - "9080"
    build:
      context: .
      args:
        http_proxy:
        https_proxy:
        no_proxy:
        TF_ANNOTATION: "no"
        USER: "django"
        DJANGO_CONFIGURATION: "production"
        WITH_TESTS: "no"
    environment:
      DJANGO_MODWSGI_EXTRA_ARGS: ""
      DJANGO_LOG_SERVER_URL: ""
    volumes:
      - cvat_data:/home/django/data
      - cvat_keys:/home/django/keys
      - cvat_logs:/home/django/logs
      - ./share:/home/django/share

volumes:
  cv_db:
  cv_data:
  cv_keys:
  cv_logs:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        # 'HOST': 'cv_db',
        'HOST': 'localhost',
        'PORT': '9801',
        'NAME': 'cv_dev',
        'USER': 'root',
        'PASSWORD': os.getenv('POSTGRES_PASSWORD', '****'),
    }

我正在使用Django调试器: 编辑配置设置如下:

Host: 0.0.0.0 Port: 9080
Run Browser: http://0.0.0.0:9080/

当我单击debug时,出现错误,提示它无法连接到数据库:

django.db.utils.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 9081?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 9081?

出什么问题了?

0 个答案:

没有答案