如何配置docker以使用django和postgres?

时间:2018-05-08 13:15:04

标签: django postgresql docker

我有一个拥有postgresql数据库的django项目。一切都在我的本地端工作正常,但当我尝试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

我的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看起来像这样

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',
        }
    }

我怀疑它是因为端口号错了,但是我正在按照教程完全按照他们的步骤进行操作,所以我不确定我做错了什么。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:0)

数据库主机不再是public class CheckoutConverter : IValueConverter { public object Convert(object entity, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var Baseentity = entity as TblBase; if (Baseentity.COID == MainWindow.LocalUser.ID) { return Brushes.Green; } else if (Baseentity.COID == 0) { return Brushes.Transparent; } else if (Baseentity.COID != 0) { return Brushes.Black; } else return Brushes.Purple; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 。但你可以通过env提供它。请将这些行添加到docker-compose.yml中的localhost

web

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

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