How to resolve "django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known"

huangapple go评论131阅读模式
英文:

How to resolve "django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known"

问题

我试图在我的本地机器上运行一个使用Django容器化的项目。我对Docker还很陌生,所以我只是尝试运行这个项目来学习Docker的工作原理。我确信项目代码完全可以运行,因为它在我的朋友的本地机器上运行正常。

我尝试了以下命令:

  1. docker-compose -f global-compose.yml up

这是我的global-compose.yml文件内容:

  1. version: '2'
  2. services:
  3. ### redis
  4. the-redis:
  5. image: redis:3.2.7-alpine
  6. ports:
  7. - '6379:6379'
  8. volumes:
  9. - ../data/redis:/data
  10. db:
  11. image: postgres:10.20
  12. environment:
  13. - PGDATA=/var/lib/postgresql/data/pgdata
  14. - POSTGRES_MULTIPLE_DATABASES=postgres,payments,justlegal_attorney_scheduler
  15. - POSTGRES_USER=postgres
  16. - POSTGRES_PASSWORD=postgres
  17. volumes:
  18. - db_data:/var/lib/postgresql/data/pgdata
  19. - ~/backups:/backups
  20. - ./postgres:/docker-entrypoint-initdb.d
  21. ports:
  22. - '5433:5432'
  23. web:
  24. build: ./gotlaw
  25. env_file: ./env_web
  26. entrypoint: ./django_instances.sh
  27. volumes:
  28. - ./gotlaw:/code
  29. - /static:/static
  30. environment:
  31. REDIS_HOST: the-redis
  32. REDIS_PORT: 6379
  33. REDIS_URL: the-redis:6379
  34. expose:
  35. - '8000'
  36. depends_on:
  37. - db
  38. - the-redis
  39. restart: always
  40. nginx:
  41. hostname: local.got.law
  42. image: nginx:1.16.0
  43. volumes:
  44. - ./deploy/nginx/conf.d:/etc/nginx/conf.d:ro
  45. - ./deploy/nginx/pages/errors:/var/www/errors
  46. - ./deploy/nginx/conf.d/stunnel:/etc/nginx/conf.d/stunnel:ro
  47. - /static:/static
  48. ports:
  49. - '80:80'
  50. - '443:443'
  51. depends_on:
  52. - web
  53. volumes:
  54. db_data:

在我的朋友的本地机器和其他一些本地机器上,它可以正常运行网站,但在我的机器上出现了以下错误:

  1. Starting gotlaw-main_the-redis_1 ... done
  2. Starting gotlaw-main_db_1 ... done
  3. Starting gotlaw-main_web_1 ... done
  4. Starting gotlaw-main_nginx_1 ... done
  5. Attaching to gotlaw-main_the-redis_1, gotlaw-main_db_1, gotlaw-main_web_1, gotlaw-main_nginx_1
  6. 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
  7. ...
  8. web_1 | psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
  9. ...

请注意,这是一个错误日志,其中包含了一些警告和错误消息,但它们并没有提供足够的上下文来确定问题的确切原因。根据错误消息看来,可能是与数据库连接相关的问题,特别是与"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

  1. version: '2'
  2. services:
  3. ### redis
  4. the-redis:
  5. image: redis:3.2.7-alpine
  6. ports:
  7. - '6379:6379'
  8. volumes:
  9. - ../data/redis:/data
  10. db:
  11. image: postgres:10.20
  12. environment:
  13. - PGDATA=/var/lib/postgresql/data/pgdata
  14. - POSTGRES_MULTIPLE_DATABASES=postgres,payments,justlegal_attorney_scheduler
  15. - POSTGRES_USER=postgres
  16. - POSTGRES_PASSWORD=postgres
  17. volumes:
  18. - db_data:/var/lib/postgresql/data/pgdata
  19. - ~/backups:/backups
  20. - ./postgres:/docker-entrypoint-initdb.d
  21. ports:
  22. - '5433:5432'
  23. web:
  24. build: ./gotlaw
  25. env_file: ./env_web
  26. entrypoint: ./django_instances.sh
  27. volumes:
  28. - ./gotlaw:/code
  29. - /static:/static
  30. environment:
  31. REDIS_HOST: the-redis
  32. REDIS_PORT: 6379
  33. REDIS_URL: the-redis:6379
  34. expose:
  35. - '8000'
  36. depends_on:
  37. - db
  38. - the-redis
  39. restart: always
  40. nginx:
  41. hostname: local.got.law
  42. image: nginx:1.16.0
  43. volumes:
  44. - ./deploy/nginx/conf.d:/etc/nginx/conf.d:ro
  45. - ./deploy/nginx/pages/errors:/var/www/errors
  46. - ./deploy/nginx/conf.d/stunnel:/etc/nginx/conf.d/stunnel:ro
  47. - /static:/static
  48. ports:
  49. - '80:80'
  50. - '443:443'
  51. depends_on:
  52. - web
  53. # - payments-client
  54. # - attorney-scheduler
  55. # - client-scheduler
  56. # links:
  57. # - attorney-scheduler:attorney-scheduler-server
  58. # - client-scheduler:client-scheduler-server
  59. volumes:
  60. 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

  1. Starting gotlaw-main_the-redis_1 ... done
  2. Starting gotlaw-main_db_1 ... done
  3. Starting gotlaw-main_web_1 ... done
  4. Starting gotlaw-main_nginx_1 ... done
  5. Attaching to gotlaw-main_the-redis_1, gotlaw-main_db_1, gotlaw-main_web_1, gotlaw-main_nginx_1
  6. 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
  7. nginx_1 | nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/default.conf:9
  8. db_1 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
  9. 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
  10. gotlaw-main_db_1 exited with code 2
  11. the-redis_1 | _._
  12. the-redis_1 | _.-``__ ''-._
  13. the-redis_1 | _.-`` `. `_. ''-._ Redis 3.2.7 (00000000/0) 64 bit
  14. the-redis_1 | .-`` .-```. ```\/ _.,_ ''-._
  15. the-redis_1 | ( ' , .-` | `, ) Running in standalone mode
  16. the-redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  17. the-redis_1 | | `-._ `._ / _.-' | PID: 1
  18. the-redis_1 | `-._ `-._ `-./ _.-' _.-'
  19. the-redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
  20. the-redis_1 | | `-._`-._ _.-'_.-' | http://redis.io
  21. the-redis_1 | `-._ `-._`-.__.-'_.-' _.-'
  22. the-redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
  23. the-redis_1 | | `-._`-._ _.-'_.-' |
  24. the-redis_1 | `-._ `-._`-.__.-'_.-' _.-'
  25. the-redis_1 | `-._ `-.__.-' _.-'
  26. the-redis_1 | `-._ _.-'
  27. the-redis_1 | `-.__.-'
  28. the-redis_1 |
  29. the-redis_1 | 1:M 20 Jul 14:20:37.097 # Server started, Redis version 3.2.7
  30. 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.
  31. 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.
  32. the-redis_1 | 1:M 20 Jul 14:20:37.097 * DB loaded from disk: 0.000 seconds
  33. the-redis_1 | 1:M 20 Jul 14:20:37.097 * The server is now ready to accept connections on port 6379
  34. web_1 | Traceback (most recent call last):
  35. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
  36. web_1 | self.connect()
  37. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  38. web_1 | return func(*args, **kwargs)
  39. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 225, in connect
  40. web_1 | self.connection = self.get_new_connection(conn_params)
  41. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  42. web_1 | return func(*args, **kwargs)
  43. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
  44. web_1 | connection = Database.connect(**conn_params)
  45. web_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
  46. web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  47. web_1 | psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
  48. web_1 |
  49. web_1 |
  50. web_1 | The above exception was the direct cause of the following exception:
  51. web_1 |
  52. web_1 | Traceback (most recent call last):
  53. web_1 | File "/code/manage.py", line 21, in <module>
  54. web_1 | execute_from_command_line(sys.argv)
  55. web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
  56. web_1 | utility.execute()
  57. web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
  58. web_1 | self.fetch_command(subcommand).run_from_argv(self.argv)
  59. web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
  60. web_1 | self.execute(*args, **cmd_options)
  61. web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
  62. web_1 | output = self.handle(*args, **options)
  63. web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/createcachetable.py", line 54, in handle
  64. web_1 | self.create_table(db, cache._table, dry_run)
  65. web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/createcachetable.py", line 62, in create_table
  66. web_1 | if tablename in connection.introspection.table_names():
  67. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/introspection.py", line 56, in table_names
  68. web_1 | with self.connection.cursor() as cursor:
  69. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  70. web_1 | return func(*args, **kwargs)
  71. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 284, in cursor
  72. web_1 | return self._cursor()
  73. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 260, in _cursor
  74. web_1 | self.ensure_connection()
  75. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  76. web_1 | return func(*args, **kwargs)
  77. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 243, in ensure_connection
  78. web_1 | with self.wrap_database_errors:
  79. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
  80. web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
  81. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
  82. web_1 | self.connect()
  83. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  84. web_1 | return func(*args, **kwargs)
  85. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 225, in connect
  86. web_1 | self.connection = self.get_new_connection(conn_params)
  87. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  88. web_1 | return func(*args, **kwargs)
  89. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
  90. web_1 | connection = Database.connect(**conn_params)
  91. web_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
  92. web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  93. web_1 | django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
  94. web_1 |
  95. web_1 | Traceback (most recent call last):
  96. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
  97. web_1 | self.connect()
  98. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  99. web_1 | return func(*args, **kwargs)
  100. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 225, in connect
  101. web_1 | self.connection = self.get_new_connection(conn_params)
  102. web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
  103. web_1 | return func(*args, **kwargs)
  104. web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
  105. web_1 | connection = Database.connect(**conn_params)
  106. web_1 | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
  107. web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  108. web_1 | psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
  109. web_1 |

答案1

得分: 0

错误发生的原因是您的 web_1 容器(运行 Django 应用程序)尝试访问主机名 'db',这是指定运行您的 PostgreSQL 数据库的服务,但它找不到任何正在运行的实例。

提示在日志中:

  1. db_1 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
  2. gotlaw-main_db_1 exited with code 2

这两个日志显示:

  1. PostgreSQL 容器无法正确启动的问题,
  2. 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:

  1. db_1 | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
  2. gotlaw-main_db_1 exited with code 2

These two logs show:

  1. the problem why the postgres container could not start properly,
  2. 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.

huangapple
  • 本文由 发表于 2023年7月20日 22:22:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定