docker-composeを用いてdjangoのapiサーバーとpostgresqlのdbをdocker-composeで通信を行いたいと考えています。
うまく通信ができていないので、ここで質問させていただきます。
docke-compose.ymlは以下のようになっています。
version: "3" services: db: container_name: db image: postgres:12.0-alpine volumes: - postgres_data:/var/lib/postgresql/data/ environment: - POSTGRES_USER=djangodb_user - POSTGRES_PASSWORD=kao7423 - POSTGRES_DB=django-db ports: - 5423:5423 rest_api: container_name: todo_api build: context: . dockerfile: Dockerfile.api volumes: - ./backend/:/backend tty: true ports: - 8000:8000 - 3000:3000 # - 5423:5423 depends_on: - db command: python manage.py runserver 0.0.0.0:8000 volumes: postgres_data:
で行っております。
また、backend/setting.pyのdbの設定は、
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'django-db', 'USER': 'djangodb_user', 'PASSWORD': 'password', 'HOST': '0.0.0.0', 'PORT': 5423, } }
です。
また、吐かれたエラーは、
db | 2021-02-03 06:52:52.041 UTC [1] LOG: starting PostgreSQL 12.0 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 8.3.0) 8.3.0, 64-bit db | 2021-02-03 06:52:52.041 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 db | 2021-02-03 06:52:52.041 UTC [1] LOG: listening on IPv6 address "::", port 5432 db | 2021-02-03 06:52:52.190 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" db | 2021-02-03 06:52:52.389 UTC [19] LOG: database system was shut down at 2021-02-03 06:50:36 UTC db | 2021-02-03 06:52:52.469 UTC [1] LOG: database system is ready to accept connections todo_api | Watching for file changes with StatReloader todo_api | Performing system checks... todo_api | todo_api | System check identified no issues (0 silenced). todo_api | Exception in thread django-main-thread: todo_api | Traceback (most recent call last): todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection todo_api | self.connect() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner todo_api | return func(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect todo_api | self.connection = self.get_new_connection(conn_params) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner todo_api | return func(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection todo_api | connection = Database.connect(**conn_params) todo_api | File "/opt/conda/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect todo_api | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) todo_api | psycopg2.OperationalError: could not connect to server: Connection refused todo_api | Is the server running on host "0.0.0.0" and accepting todo_api | TCP/IP connections on port 5423? todo_api | todo_api | todo_api | The above exception was the direct cause of the following exception: todo_api | todo_api | Traceback (most recent call last): todo_api | File "/opt/conda/lib/python3.8/threading.py", line 932, in _bootstrap_inner todo_api | self.run() todo_api | File "/opt/conda/lib/python3.8/threading.py", line 870, in run todo_api | self._target(*self._args, **self._kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper todo_api | fn(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run todo_api | self.check_migrations() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/core/management/base.py", line 459, in check_migrations todo_api | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__ todo_api | self.loader = MigrationLoader(self.connection) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__ todo_api | self.build_graph() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph todo_api | self.applied_migrations = recorder.applied_migrations() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations todo_api | if self.has_table(): todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table todo_api | with self.connection.cursor() as cursor: todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner todo_api | return func(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor todo_api | return self._cursor() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor todo_api | self.ensure_connection() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner todo_api | return func(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection todo_api | self.connect() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__ todo_api | raise dj_exc_value.with_traceback(traceback) from exc_value todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection todo_api | self.connect() todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner todo_api | return func(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect todo_api | self.connection = self.get_new_connection(conn_params) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner todo_api | return func(*args, **kwargs) todo_api | File "/opt/conda/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection todo_api | connection = Database.connect(**conn_params) todo_api | File "/opt/conda/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect todo_api | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) todo_api | django.db.utils.OperationalError: could not connect to server: Connection refused todo_api | Is the server running on host "0.0.0.0" and accepting todo_api | TCP/IP connections on port 5423? todo_api |
となっております。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/03 09:45