首页 > 解决方案 > 意外关闭后的 Django 项目 docker:服务器是否在主机“db”(172.19.0.3)上运行并接受端口 5432 上的 TCP/IP 连接?

问题描述

我正在使用 popOs20 电池电量不足并发生意外关机。与 docker 相同的项目成功构建在其他设备上。我尝试过:docker system prune、docker volume prune、杀死所有 swap 和 ram 进程然后重新启动、更改 docker 网络以及其他一些我不记得的事情,甚至不再记得了。有什么想法我能做什么?项目正在使用 postgresql13

完整错误:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out
        Is the server running on host "db" (172.19.0.3) and accepting
        TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check_migrations()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 458, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
    if self.has_table():
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 260, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 236, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection timed out
        Is the server running on host "db" (172.19.0.3) and accepting
        TCP/IP connections on port 5432?

码头工人-compose.yml:

version: "3.3"
services:
  backend: &be
    container_name: my_be
    build:
      context: ./backend
    command: bash -c "python manage.py runserver 0.0.0.0:8080"
    expose:
      - 8080
    volumes:
      - "./backend:/code/"
    depends_on:
      - db
    restart: unless-stopped
    environment:
      - DEBUG=1
      - SECRET_KEY=7km0&m!ffj$n3bre-sdbc(97920(k$yfl^x992(t(s0zp)0jb8
      - POSTGRES_DB=dev
      - POSTGRES_USER=dev
      - POSTGRES_PASSWORD=mysekretpassword
      - POSTGRES_SERVICE_HOST=db
      - POSTGRES_SERVICE_PORT=5432
      - REDIS_PORT=6379
      - REDIS_HOST=redis
      - REDIS_DB=0
    networks:
      - main

  db:
    container_name: my_db
    image: postgres:13-alpine
    environment:
      - POSTGRES_DB=dev
      - POSTGRES_USER=dev
      - POSTGRES_PASSWORD=mysekretpassword
    networks:
      - main

  pgadmin:
    container_name: my_pgadmin
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@admin.com
      - PGADMIN_DEFAULT_PASSWORD=admin
    depends_on:
      - db
    ports:
      - "5050:80"
    networks:
      - main

  nginx:
    container_name: my_nginx
    build:
      context: ./nginx
    ports:
      - "80:80"
    depends_on:
      - backend
    volumes:
      - ./nginx/dev.conf:/etc/nginx/nginx.conf:ro
    restart: unless-stopped
    networks:
      - main

  redis:
    container_name: my_redis
    image: redis:alpine
    volumes:
      - redis_data:/data
    networks:
      - main

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    volumes:
      - ./frontend:/code
      - /code/node_modules
      - /code/.next
    networks:
      - main

networks:
  main:
    driver: bridge

volumes:
  redis_data:

数据库日志:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok


Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2021-05-18 16:53:49.383 UTC [45] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-05-18 16:53:49.387 UTC [45] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-05-18 16:53:49.398 UTC [46] LOG:  database system was shut down at 2021-05-18 16:53:49 UTC
2021-05-18 16:53:49.404 UTC [45] LOG:  database system is ready to accept connections
 done
server started
CREATE DATABASE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down....2021-05-18 16:53:49.625 UTC [45] LOG:  received fast shutdown request
2021-05-18 16:53:49.629 UTC [45] LOG:  aborting any active transactions
2021-05-18 16:53:49.630 UTC [45] LOG:  background worker "logical replication launcher" (PID 52) exited with exit code 1
2021-05-18 16:53:49.630 UTC [47] LOG:  shutting down
2021-05-18 16:53:49.652 UTC [45] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2021-05-18 16:53:49.747 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-05-18 16:53:49.748 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2021-05-18 16:53:49.748 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2021-05-18 16:53:49.757 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-05-18 16:53:49.766 UTC [73] LOG:  database system was shut down at 2021-05-18 16:53:49 UTC
2021-05-18 16:53:49.772 UTC [1] LOG:  database system is ready to accept connections

标签: djangolinuxdatabasedockernetworking

解决方案


推荐阅读