我如何解决django.db.utils.OperationalError当我试图迁移数据时,我运行我的docker撰写



我目前正在研究一个项目,使用docker撰写部署django应用程序,但当我运行docker compose up命令时,当我试图迁移数据时,我一直得到django.db.utils.OperationalError

这是我得到的完整错误;

=> ERROR [6/7] RUN python3 manage.py makemigrate --settings=core.settings.production                             4.5s
------
> [6/7] RUN python3 manage.py makemigrate --settings=core.settings.production:
#0 3.964 /usr/local/lib/python3.10/site-packages/django/core/management/commands/makemigrations.py:158: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': could not translate host name "postgres" to address: Name or service not known
#0 3.964 
#0 3.964   warnings.warn(
#0 3.985 No changes detected in app 'tags'
#0 4.016 No changes detected in app 'users'
#0 4.050 No changes detected in app 'content'
#0 4.081 No changes detected in app 'actions'
#0 4.102 Traceback (most recent call last):
#0 4.102   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
#0 4.104     self.connect()
#0 4.104   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
#0 4.105     return func(*args, **kwargs)
#0 4.105   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
#0 4.106     self.connection = self.get_new_connection(conn_params)
#0 4.106   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
#0 4.107     return func(*args, **kwargs)
#0 4.107   File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 269, in get_new_connection
#0 4.108     connection = self.Database.connect(**conn_params)
#0 4.108   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
#0 4.109     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
#0 4.109 psycopg2.OperationalError: could not translate host name "postgres" to address: Name or service not known
#0 4.109 
#0 4.109 
#0 4.109 The above exception was the direct cause of the following exception:
#0 4.109 
#0 4.109 Traceback (most recent call last):
#0 4.109   File "/noted/noted/manage.py", line 22, in <module>
#0 4.110     main()
#0 4.110   File "/noted/noted/manage.py", line 18, in main
#0 4.111     execute_from_command_line(sys.argv)
#0 4.111   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
#0 4.112     utility.execute()
#0 4.112   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
#0 4.113     self.fetch_command(subcommand).run_from_argv(self.argv)
#0 4.113   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 412, in run_from_argv
#0 4.115     self.execute(*args, **cmd_options)
#0 4.115   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 458, in execute
#0 4.115     output = self.handle(*args, **options)
#0 4.117   File "/noted/noted/common/management/commands/makemigrate.py", line 19, in handle
#0 4.118     call_command("migrate")
#0 4.120   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 194, in call_command
#0 4.120     return command.execute(*args, **defaults)
#0 4.120   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 458, in execute
#0 4.121     output = self.handle(*args, **options)
#0 4.121   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 106, in wrapper
#0 4.121     res = handle_func(*args, **kwargs)
#0 4.121   File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 117, in handle
#0 4.124     executor = MigrationExecutor(connection, self.migration_progress_callback)
#0 4.125   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
#0 4.128     self.loader = MigrationLoader(self.connection)
#0 4.128   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
#0 4.129     self.build_graph()
#0 4.130   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 235, in build_graph
#0 4.131     self.applied_migrations = recorder.applied_migrations()
#0 4.132   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
#0 4.132     if self.has_table():
#0 4.134   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 57, in has_table
#0 4.134     with self.connection.cursor() as cursor:
#0 4.134   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
#0 4.135     return func(*args, **kwargs)
#0 4.135   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 330, in cursor
#0 4.135     return self._cursor()
#0 4.135   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 306, in _cursor
#0 4.136     self.ensure_connection()
#0 4.136   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
#0 4.136     return func(*args, **kwargs)
#0 4.137   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
#0 4.138     with self.wrap_database_errors:
#0 4.138   File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
#0 4.141     raise dj_exc_value.with_traceback(traceback) from exc_value
#0 4.142   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
#0 4.144     self.connect()
#0 4.144   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
#0 4.144     return func(*args, **kwargs)
#0 4.144   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
#0 4.145     self.connection = self.get_new_connection(conn_params)
#0 4.145   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
#0 4.145     return func(*args, **kwargs)
#0 4.145   File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 269, in get_new_connection
#0 4.146     connection = self.Database.connect(**conn_params)
#0 4.146   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
#0 4.146     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
#0 4.146 django.db.utils.OperationalError: could not translate host name "postgres" to address: Name or service not known
#0 4.146 
------
failed to solve: process "/bin/sh -c python3 manage.py makemigrate --settings=core.settings.production" did not complete successfully: exit code: 1

这是我在django中的数据库设置

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": get_env_variable("DATABASE_NAME"),
"USER": get_env_variable("DATABASE_USER"),
"PASSWORD": get_env_variable("DATABASE_PASSWORD"),
"HOST": get_env_variable("DATABASE_HOST"),
"PORT": get_env_variable("DATABASE_PORT"),
}
}

这是我的dockerfile内容

FROM python:3.10
WORKDIR /noted
COPY . .
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1 
RUN pip install -r requirements/production.txt 
&& apt-get update 
&& apt-get -y install wkhtmltopdf 
&& apt-get -y autoclean
EXPOSE 8000
WORKDIR /noted/noted
RUN python3 manage.py makemigrate --settings=core.settings.production
RUN manage.py collectstatic --noinput --settings=core.settings.production
# Set the entry point command for gunicorn
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "core.wsgi"]

这是我的docker撰写内容

version: '3'
services:
app:
build:
context: .
container_name: noted_app
env_file: .env
volumes:
- ./noted:/noted
ports:
- 8000:8000
networks:
- noted-network
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres
restart: always
container_name: postgres
volumes:
- ./postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
networks:
- noted-network
env_file:
- .env
environment:
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_DB=${DATABASE_NAME}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}

healthcheck:
test: ["CMD-SHELL", "sh -c 'pg_isready -U ${DATABASE_USER} -d ${DATABASE_NAME}'"]
interval: 10s
timeout: 3s
retries: 3
networks:
noted-network:

这是我的。env文件中的数据库变量

DATABASE_NAME=postgres
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres
DATABASE_HOST=postgres
DATABASE_PORT=5432

我docker-compose。Yml文件和.env文件位于同一位置。请告诉我怎样才能通过这个错误。

为什么命令是这样的?

python3 manage.py makemigrate

应该是

python3 manage.py makemigrations
python3 manage.py migrate

相关内容

  • 没有找到相关文章

最新更新