英文:
How to resolve "django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known"
问题
我试图在我的本地机器上运行一个使用Django容器化的项目。我对Docker还很陌生,所以我只是尝试运行这个项目来学习Docker的工作原理。我确信项目代码完全可以运行,因为它在我的朋友的本地机器上运行正常。
我尝试了以下命令:
docker-compose -f global-compose.yml up
这是我的global-compose.yml文件内容:
version: '2'
services:
### redis
the-redis:
image: redis:3.2.7-alpine
ports:
- '6379:6379'
volumes:
- ../data/redis:/data
db:
image: postgres:10.20
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_MULTIPLE_DATABASES=postgres,payments,justlegal_attorney_scheduler
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- db_data:/var/lib/postgresql/data/pgdata
- ~/backups:/backups
- ./postgres:/docker-entrypoint-initdb.d
ports:
- '5433:5432'
web:
build: ./gotlaw
env_file: ./env_web
entrypoint: ./django_instances.sh
volumes:
- ./gotlaw:/code
- /static:/static
environment:
REDIS_HOST: the-redis
REDIS_PORT: 6379
REDIS_URL: the-redis:6379
expose:
- '8000'
depends_on:
- db
- the-redis
restart: always
nginx:
hostname: local.got.law
image: nginx:1.16.0
volumes:
- ./deploy/nginx/conf.d:/etc/nginx/conf.d:ro
- ./deploy/nginx/pages/errors:/var/www/errors
- ./deploy/nginx/conf.d/stunnel:/etc/nginx/conf.d/stunnel:ro
- /static:/static
ports:
- '80:80'
- '443:443'
depends_on:
- web
volumes:
db_data:
在我的朋友的本地机器和其他一些本地机器上,它可以正常运行网站,但在我的机器上出现了以下错误:
Starting gotlaw-main_the-redis_1 ... done
Starting gotlaw-main_db_1 ... done
Starting gotlaw-main_web_1 ... done
Starting gotlaw-main_nginx_1 ... done
Attaching to gotlaw-main_the-redis_1, gotlaw-main_db_1, gotlaw-main_web_1, gotlaw-main_nginx_1
nginx_1 | 2023/07/20 14:20:38 [warn] 1#1: the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/default.conf:9
...
web_1 | psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
...
请注意,这是一个错误日志,其中包含了一些警告和错误消息,但它们并没有提供足够的上下文来确定问题的确切原因。根据错误消息看来,可能是与数据库连接相关的问题,特别是与"db"主机名的解析有关。可能需要检查Docker容器网络配置、数据库配置以及主机名解析等问题来解决此问题。如果您需要更详细的帮助,请提供更多的信息和上下文。
英文:
I'm trying to run a Django dockerized project on my local machine. I'm new to dockers so,I'm just trying to run this project to learn how dockers work.I'm sure the project code is absolutely fine to work with because it runs fine on my friend's local machine.
I tried
docker-compose -f global-compose.yml up
This is my global-compose.yml
version: '2'
services:
### redis
the-redis:
image: redis:3.2.7-alpine
ports:
- '6379:6379'
volumes:
- ../data/redis:/data
db:
image: postgres:10.20
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_MULTIPLE_DATABASES=postgres,payments,justlegal_attorney_scheduler
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- db_data:/var/lib/postgresql/data/pgdata
- ~/backups:/backups
- ./postgres:/docker-entrypoint-initdb.d
ports:
- '5433:5432'
web:
build: ./gotlaw
env_file: ./env_web
entrypoint: ./django_instances.sh
volumes:
- ./gotlaw:/code
- /static:/static
environment:
REDIS_HOST: the-redis
REDIS_PORT: 6379
REDIS_URL: the-redis:6379
expose:
- '8000'
depends_on:
- db
- the-redis
restart: always
nginx:
hostname: local.got.law
image: nginx:1.16.0
volumes:
- ./deploy/nginx/conf.d:/etc/nginx/conf.d:ro
- ./deploy/nginx/pages/errors:/var/www/errors
- ./deploy/nginx/conf.d/stunnel:/etc/nginx/conf.d/stunnel:ro
- /static:/static
ports:
- '80:80'
- '443:443'
depends_on:
- web
# - payments-client
# - attorney-scheduler
# - client-scheduler
# links:
# - attorney-scheduler:attorney-scheduler-server
# - client-scheduler:client-scheduler-server
volumes:
db_data:
On my friend's local and some other local machines it just start running the website but on my machine I face this error
Starting gotlaw-main_the-redis_1 ... done
Starting gotlaw-main_db_1 ... done
Starting gotlaw-main_web_1 ... done
Starting gotlaw-main_nginx_1 ... done
Attaching to gotlaw-main_the-redis_1, gotlaw-main_db_1, gotlaw-main_web_1, gotlaw-main_nginx_1
nginx_1 | 2023/07/20 14:20:38 [warn] 1#1: the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/default.conf:9
nginx_1 | nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/default.conf:9
db_1 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
the-redis_1 | 1:C 20 Jul 14:20:37.094 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
gotlaw-main_db_1 exited with code 2
the-redis_1 | _._
the-redis_1 | _.-``__ ''-._
the-redis_1 | _.-`` `. `_. ''-._ Redis 3.2.7 (00000000/0) 64 bit
the-redis_1 | .-`` .-```. ```\/ _.,_ ''-._
the-redis_1 | ( ' , .-` | `, ) Running in standalone mode
the-redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
the-redis_1 | | `-._ `._ / _.-' | PID: 1
the-redis_1 | `-._ `-._ `-./ _.-' _.-'
the-redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
the-redis_1 | | `-._`-._ _.-'_.-' | http://redis.io
the-redis_1 | `-._ `-._`-.__.-'_.-' _.-'
the-redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
the-redis_1 | | `-._`-._ _.-'_.-' |
the-redis_1 | `-._ `-._`-.__.-'_.-' _.-'
the-redis_1 | `-._ `-.__.-' _.-'
the-redis_1 | `-._ _.-'
the-redis_1 | `-.__.-'
the-redis_1 |
the-redis_1 | 1:M 20 Jul 14:20:37.097 # Server started, Redis version 3.2.7
the-redis_1 | 1:M 20 Jul 14:20:37.097 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
the-redis_1 | 1:M 20 Jul 14:20:37.097 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
the-redis_1 | 1:M 20 Jul 14:20:37.097 * DB loaded from disk: 0.000 seconds
the-redis_1 | 1:M 20 Jul 14:20:37.097 * The server is now ready to accept connections on port 6379
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 225, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, 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 "/code/manage.py", line 21, in <module>
web_1 | execute_from_command_line(sys.argv)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
web_1 | utility.execute()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
web_1 | self.fetch_command(subcommand).run_from_argv(self.argv)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
web_1 | self.execute(*args, **cmd_options)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
web_1 | output = self.handle(*args, **options)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/createcachetable.py", line 54, in handle
web_1 | self.create_table(db, cache._table, dry_run)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/createcachetable.py", line 62, in create_table
web_1 | if tablename in connection.introspection.table_names():
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/introspection.py", line 56, in table_names
web_1 | with self.connection.cursor() as cursor:
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 284, in cursor
web_1 | return self._cursor()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 260, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 243, in ensure_connection
web_1 | with self.wrap_database_errors:
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 225, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, 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
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 225, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, 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 |
答案1
得分: 0
错误发生的原因是您的 web_1 容器(运行 Django 应用程序)尝试访问主机名 'db',这是指定运行您的 PostgreSQL 数据库的服务,但它找不到任何正在运行的实例。
提示在日志中:
db_1 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
gotlaw-main_db_1 exited with code 2
这两个日志显示:
- PostgreSQL 容器无法正确启动的问题,
- PostgreSQL 容器退出。
可能会出现权限被拒绝的错误的原因有几种,但目前,请尝试以管理员身份运行 Docker,然后再试一次。
英文:
The error occurs because your web_1 container (running the django app) is trying to access host name 'db', which is the service specified to run your postgreSQL database, but it could not find any instance of it running.
The clue is in the logs:
db_1 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
gotlaw-main_db_1 exited with code 2
These two logs show:
- the problem why the postgres container could not start properly,
- and the postgres container exiting.
There are several reasons why you might encounter a permissions denied error, but for now, try running docker as administrator and try again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论