简体   繁体   English

如何配置docker与django和postgres一起使用?

[英]How to configure docker to work with django and postgres?

I have a django project that has a postgresql database. 我有一个具有PostgreSQL数据库的django项目。 All works fine on my local end but when i tried to docker-compose up it throws an error like this 在我的本地终端上一切正常,但是当我尝试docker-compose up它会抛出这样的错误

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

My docker-compose.yml looks like the following. 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:

And my settings.py looks like this 我的settings.py看起来像这样

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. 非常感谢您的帮助。

The database host is not localhost any more. 数据库主机不再是localhost But you can provide it via env. 但是您可以通过env提供它。 Please add these lines to web in your docker-compose.yml: 请在docker-compose.yml中将以下行添加到web

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM