![](/img/trans.png)
[英]Docker django.db.utils.OperationalError: could not translate host name “db” to address: Temporary failure in name resolution
[英]Django Docker: django.db.utils.OperationalError: could not translate host name “db” to address: Name or service not known
我是 Docker 和 Postgres 的新手,並且正在關注 William S. Vincent 的“專業 Django”一書來學習這一點。 我試圖用 Docker 容器連接到 PostgreSQL。
我做了什么:
1> 編寫 Docker 文件:
# Pull base image
FROM python:3.7
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Copy project
COPY . /code/
2> 編寫 docker-compose.yml 文件:
version: "3.7"
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
3> 更改設置.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',docker-compose up -d --build
'PORT': 5432
}
}
4> 安裝 psycopg2
docker-compose exec web pipenv install psycopg2-binary==2.8.3
5> 運行 docker-compose 重建鏡像:
docker-compose up -d --build
當我檢查 docker-compose 日志時會引發錯誤:
Attaching to postgresql_web_1, postgresql_db_1
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | Exception in thread django-main-thread:
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 195, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 126, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
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 "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
web_1 | self.run()
web_1 | File "/usr/local/lib/python3.7/threading.py", line 870, in run
web_1 | self._target(*self._args, **self._kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
web_1 | self.check_migrations()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 453, in check_migrations
web_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1 | File "/usr/local/lib/python3.7/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.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
web_1 | self.build_graph()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/loader.py", line 212, in build_graph
web_1 | self.applied_migrations = recorder.applied_migrations()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
web_1 | if self.has_table():
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 56, 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.7/site-packages/django/db/backends/base/base.py", line 256, in cursor
web_1 | return self._cursor()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 233, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/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.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 195, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 126, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
db_1 | Error: Database is uninitialized and superuser password is not specified.
db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
db_1 |
db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
db_1 | connections without a password. This is *not* recommended.
db_1 |
db_1 | See PostgreSQL documentation about "trust":
db_1 | https://www.postgresql.org/docs/current/auth-trust.html
web_1 |
web_1 |
我在 Stack Overflow 上嘗試過各種類似問題的答案,但似乎沒有什么對我有用。 我試過添加:
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
像這樣到 db
以及其他各種答案。
我遵循與您相同的教程,並且在添加時遇到了同樣的問題:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
到 db 服務結束
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.