【问题标题】:Django Server wont launch in Docker-ComposeDjango 服务器不会在 Docker-Compose 中启动
【发布时间】:2016-02-07 19:56:23
【问题描述】:

我正在尝试将 Django 项目移植到 Docker-Compose 以进行部署。

我在网上学习了各种教程,据我了解,我的应用程序需要的唯一服务是 Django 和 Postgres。这是我的 docker-compose.yml 文件:

db:
  image: postgres:latest
  ports:
    - "5432"
  environment:
    POSTGRES_PASSWORD: postgres
    POSTGRES_USER: postgres
web:
  build: .
  ports:
   - "8000:8000"
  volumes:
   - .:/code
  links:
   - db
  command: python3 manage.py runserver 0.0.0.0:8000

我已经在我的 virtualenv 中本地测试了 python 命令并启动了服务器,但是,当我在应用程序副本所在的文件夹中运行“docker-compose up”时,我得到以下输出:

Creating hpr_db_1
Creating hpr_web_1
Attaching to hpr_db_1, hpr_web_1
db_1  | The files belonging to this database system will be owned by user "postgres".
db_1  | This user must also own the server process.
db_1  | 
db_1  | The database cluster will be initialized with locale "en_US.utf8".
db_1  | The default database encoding has accordingly been set to "UTF8".
db_1  | The default text search configuration will be set to "english".
db_1  | 
db_1  | Data page checksums are disabled.
db_1  | 
db_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1  | creating subdirectories ... ok
db_1  | selecting default max_connections ... 100
db_1  | selecting default shared_buffers ... 128MB
db_1  | selecting dynamic shared memory implementation ... posix
db_1  | creating configuration files ... ok
db_1  | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
db_1  | initializing pg_authid ... ok
db_1  | initializing dependencies ... ok
web_1 | /usr/local/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
web_1 |   return f(*args, **kwds)
web_1 | 
db_1  | creating system views ... ok
db_1  | loading system objects' descriptions ... ok
db_1  | creating collations ... ok
db_1  | creating conversions ... ok
db_1  | creating dictionaries ... ok
db_1  | setting privileges on built-in objects ... ok
db_1  | creating information schema ... ok
db_1  | loading PL/pgSQL server-side language ... ok
web_1 | /usr/local/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
web_1 |   return f(*args, **kwds)
web_1 | 
db_1  | vacuuming database template1 ... ok
db_1  | copying template1 to template0 ... ok
db_1  | copying template1 to postgres ... ok
db_1  | syncing data to disk ... ok
db_1  | 
db_1  | Success. You can now start the database server using:
db_1  | 
db_1  |     postgres -D /var/lib/postgresql/data
db_1  | or
db_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1  | 
db_1  | 
db_1  | WARNING: enabling "trust" authentication for local connections
db_1  | You can change this by editing pg_hba.conf or using the option -A, or
db_1  | --auth-local and --auth-host, the next time you run initdb.
db_1  | waiting for server to start....LOG:  database system was shut down at 2015-11-06 15:18:39 UTC
db_1  | LOG:  MultiXact member wraparound protections are now enabled
db_1  | LOG:  autovacuum launcher started
db_1  | LOG:  database system is ready to accept connections
db_1  |  done
db_1  | server started
db_1  | ALTER ROLE
db_1  | 
db_1  | 
db_1  | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1  | 
db_1  | LOG:  received fast shutdown request
db_1  | LOG:  aborting any active transactions
db_1  | LOG:  autovacuum launcher shutting down
db_1  | waiting for server to shut down....LOG:  shutting down
db_1  | LOG:  database system is shut down
db_1  |  done
db_1  | server stopped
db_1  | 
db_1  | PostgreSQL init process complete; ready for start up.
db_1  | 
db_1  | LOG:  database system was shut down at 2015-11-06 15:18:41 UTC
db_1  | LOG:  MultiXact member wraparound protections are now enabled
db_1  | LOG:  database system is ready to accept connections
db_1  | LOG:  autovacuum launcher started

没有关于服务器启动或类似内容的消息,由于有关弃用的警告消息,我知道它正在编译我的代码,但我在这里很困惑。谁能看到我的错误?

更新:

在仔细检查并更改 DJango 服务器的端口后,我发现访问网页会返回“在页面加载时与服务器的连接已重置”错误。这表明该页面上正在设置某些内容。但我仍然无法访问它。我知道 Docker-Compose 与教程中的简单 Django 应用程序一样工作得很好。

【问题讨论】:

  • 您能否为您的 django 网络应用插入您的 dockerfile

标签: python django postgresql docker docker-compose


【解决方案1】:

EXPOSE django 容器的端口了吗?

【讨论】:

  • 这让我找到了正确的区域,所以我将其标记为正确,我的 yml 文件中没有选择正确的端口,如我的 Django 项目 settings.py 的数据库部分中定义的那样
【解决方案2】:

使用 .yml 文件创建构建。

您应该将文件移动到部署环境并继续运行服务器。

因此,当 .yml 成功运行时,您将成功构建,并且您将能够将文件移动到部署环境(您有最后一次构建运行的地方)。

无论如何,我不建议在运行器中执行python3 manage.py runserver 0.0.0.0:8000。因为你的运行器将运行并且它不会完成(获得成功或失败的构建)。

【讨论】:

  • 你能详细说明最后一句话吗?为什么运行python3 manage.py runserver 0.0.0.0:8000不好?你会推荐什么来代替运行?我有一个问题,当出现python错误时,没有输出,服务器只是没有启动。
猜你喜欢
  • 2022-01-23
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2020-06-30
  • 1970-01-01
  • 2018-12-16
相关资源
最近更新 更多