尝试在docker容器中安装postgres并容器化django项目

qxsslcnc  于 2023-06-22  发布在  Docker
关注(0)|答案(1)|浏览(113)

我对Docker和Web开发都是新手。当尝试容器化我的django项目时,我遇到了一个错误,并且几天都无法解决它。这个错误可能很愚蠢,但我找不到解决方案。如果有人帮我解决这个问题,我会很高兴的。先谢谢你。作为操作系统,我使用Ubuntu 22.04
以下是执行docker compose builddocker compose up命令后的输出:

[+] Running 2/0
 ⠿ Container test_task-db_task-1  Created                                                                                                               0.0s
 ⠿ Container test_task_container  Created                                                                                                               0.0s
Attaching to test_task-db_task-1, test_task_container
test_task-db_task-1  | 
test_task-db_task-1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
test_task-db_task-1  | 
test_task-db_task-1  | 2023-06-07 10:46:07.124 UTC [1] LOG:  starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
test_task-db_task-1  | 2023-06-07 10:46:07.125 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
test_task-db_task-1  | 2023-06-07 10:46:07.125 UTC [1] LOG:  listening on IPv6 address "::", port 5432
test_task-db_task-1  | 2023-06-07 10:46:07.147 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
test_task-db_task-1  | 2023-06-07 10:46:07.163 UTC [21] LOG:  database system was shut down at 2023-06-07 10:45:29 UTC
test_task-db_task-1  | 2023-06-07 10:46:07.218 UTC [1] LOG:  database system is ready to accept connections
test_task_container  | Watching for file changes with StatReloader
test_task_container  | Exception in thread django-main-thread:
test_task_container  | Traceback (most recent call last):
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
test_task_container  |     self.connect()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container  |     return func(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
test_task_container  |     self.connection = self.get_new_connection(conn_params)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container  |     return func(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
test_task_container  |     connection = self.Database.connect(**conn_params)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
test_task_container  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
test_task_container  | psycopg2.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
test_task_container  |  Is the server running locally and accepting connections on that socket?
test_task_container  | 
test_task_container  | 
test_task_container  | The above exception was the direct cause of the following exception:
test_task_container  | 
test_task_container  | Traceback (most recent call last):
test_task_container  |   File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
test_task_container  |     self.run()
test_task_container  |   File "/usr/local/lib/python3.10/threading.py", line 953, in run
test_task_container  |     self._target(*self._args, **self._kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
test_task_container  |     fn(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 136, in inner_run
test_task_container  |     self.check_migrations()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 574, in check_migrations
test_task_container  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
test_task_container  |     self.loader = MigrationLoader(self.connection)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
test_task_container  |     self.build_graph()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 235, in build_graph
test_task_container  |     self.applied_migrations = recorder.applied_migrations()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
test_task_container  |     if self.has_table():
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 57, in has_table
test_task_container  |     with self.connection.cursor() as cursor:
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container  |     return func(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 330, in cursor
test_task_container  |     return self._cursor()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 306, in _cursor
test_task_container  |     self.ensure_connection()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container  |     return func(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
test_task_container  |     with self.wrap_database_errors:
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
test_task_container  |     raise dj_exc_value.with_traceback(traceback) from exc_value
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
test_task_container  |     self.connect()
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container  |     return func(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
test_task_container  |     self.connection = self.get_new_connection(conn_params)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container  |     return func(*args, **kwargs)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
test_task_container  |     connection = self.Database.connect(**conn_params)
test_task_container  |   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
test_task_container  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
test_task_container  | django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
test_task_container  |  Is the server running locally and accepting connections on that socket?
test_task_container  |

下面是我的docker-compose文件:

services:
  task:
    build: .
    volumes:
      - .:/test_task
    ports:
      - 8000:8000
    image: app:django
    container_name: test_task_container
    command: python manage.py runserver 0.0.0.0:8000

  db_task:
    image: postgres:14.1-alpine
    restart: always
    env_file:
      - .env
    ports:
      - '5432:5432'
    volumes:
      - db:/var/lib/postgresql/data
volumes:
  db:
    driver: local

下面是dockerfile本身:

FROM python:3.10-slim-buster

ENV PYTHONBUFFERED=1

WORKDIR /test_task

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

.env文件:

POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
DJANGO_SETTINGS_MODULE=core.settings

以及www.example.com中的数据库设置settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': os.getenv('POSTGRES_HOST'),
        'PORT': os.getenv('POSTGRES_PORT'),
        'USER': os.getenv('POSTGRES_USER'),
        'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
        'NAME': os.getenv('POSTGRES_DB')
    }
}

我试图容器化我的项目,并在我的本地服务器上打开它

iaqfqrcu

iaqfqrcu1#

需要考虑的几件事:

  • 你的compose设置中的服务(在创建的docker网络中)-默认情况下-解析为它们的名称。因此,POSTGRES_HOST应为"db_task"
  • 如果.envdocker-compose.yaml位于相同的位置,则应自动拾取.env。因此,为了保持一致性,请为两者指定env_file,或者都不指定。
  • django应用程序依赖于数据库。如果数据库尚未启动并运行,django应用程序就无法连接到它。因此,您应该将depends_on添加到task服务,以便在数据库准备就绪之前不会启动应用程序:
task:
    # ...
    depends_on:
      - db_task

相关问题