如何配置docker与django和postgres一起使用? [英] How to configure docker to work with django and postgres?

查看:88
本文介绍了如何配置docker与django和postgres一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有PostgreSQL数据库的Django项目。一切在我的本地计算机上都可以正常工作,但是当我尝试 docker-compose up 时,它会抛出这样的错误

I have a django project that has a postgresql database. All works fine on my local end but when i tried to docker-compose up it throws an error like this

Starting 71a52ffe37d1_locallibrary_db_1 ... done
Starting locallibrary_web_1             ... done
Attaching to 71a52ffe37d1_locallibrary_db_1, locallibrary_web_1
71a52ffe37d1_locallibrary_db_1 | 2018-05-08 13:11:51.451 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
71a52ffe37d1_locallibrary_db_1 | 2018-05-08 13:11:51.451 UTC [1] LOG:  listening on IPv6 address "::", port 5432
71a52ffe37d1_locallibrary_db_1 | 2018-05-08 13:11:51.463 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
71a52ffe37d1_locallibrary_db_1 | 2018-05-08 13:11:51.630 UTC [21] LOG:  database system was shut down at 2018-05-08 12:59:50 UTC
71a52ffe37d1_locallibrary_db_1 | 2018-05-08 13:11:51.636 UTC [1] LOG:  database system is ready to accept connections
web_1  | /usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
web_1  |   """)
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, 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 168, 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 "localhost" (127.0.0.1) and accepting
web_1  |        TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  |        Is the server running on host "localhost" (::1) 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 "/code/manage.py", line 15, in <module>
web_1  |     execute_from_command_line(sys.argv)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, 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 365, 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 288, 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 335, in execute
web_1  |     output = self.handle(*args, **options)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 79, in handle
web_1  |     executor = MigrationExecutor(connection, self.migration_progress_callback)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
web_1  |     self.loader = MigrationLoader(self.connection)
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
web_1  |     self.build_graph()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 206, in build_graph
web_1  |     self.applied_migrations = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
web_1  |     if self.has_table():
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table
web_1  |     return 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 255, in cursor
web_1  |     return self._cursor()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
web_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, 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 168, 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 "localhost" (127.0.0.1) and accepting
web_1  |        TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  |        Is the server running on host "localhost" (::1) and accepting
web_1  |        TCP/IP connections on port 5432?
web_1  |
locallibrary_web_1 exited with code 1

我的 docker-compose.yml 如下所示。

version: '3'

services:
  db:
    image: postgres:10.1
    volumes:
      - postgres_data:/var/lib/postgresql/data/
  web:
    build: .
    command: bash -c "python /code/manage.py migrate --noinput && python /code/manage.py runserver 0.0.0.$
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes:
  postgres_data:

我的settings.py看起来像这样

And my settings.py looks like this

if 'RDS_DB_NAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'db1',
            'USER': 'rdeng',
            'PASSWORD': 'walterfedy',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }

我怀疑这是因为端口号错误,但是我遵循了一个教程并严格按照他们的步骤进行操作所以我不确定我做错了什么。

I am suspecting that it's because the port number is wrong but I was following a tutorial and exactly followed their steps of doing it so i'm not sure what i did wrong. Any help is highly appreciated.

推荐答案

数据库主机不是 localhost 还有。但是您可以通过env提供它。请在docker-compose.yml中将这些行添加到 web

The database host is not localhost any more. But you can provide it via env. Please add these lines to web in your docker-compose.yml:

web:
  env:
    RDS_DB_NAME=<your db name>
    RDS_HOSTNAME=db
    RDS_DB_NAME=...
    ...

或者,您也可以通过文件来提供它们:

Alternatively you can provide them via file:

web:
  env_file:
    - web-variables.env

这篇关于如何配置docker与django和postgres一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆