How to configure docker to work with django and po

2019-08-17 19:27发布

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

My docker-compose.yml looks like the following.

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

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.

1条回答
放我归山
2楼-- · 2019-08-17 19:55

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
查看更多
登录 后发表回答