Docker 容器中的 Django 和 uWSGI

Docker 容器中的 Django 和 uWSGI

我正在尝试在 Docker 容器中运行一个使用 Django 和 uWSGI 的项目。但我对 Docker 和 uWSGI 还不熟悉,所以我不太清楚应该怎么做。

首先我使用以下命令构建项目:

docker build -t saleor .

然后我运行它:

docker run --env SECRET_KEY="<the key>" -p 4000:80 saleor

这就是我得到的:

[uWSGI] getting INI configuration from /app/saleor/wsgi/uwsgi.ini
[uwsgi-static] added mapping for /static => /app/static
*** Starting uWSGI 2.0.15 (64bit) on [Tue Dec 19 14:02:19 2017] ***
compiled with version: 4.9.2 on 19 December 2017 09:11:22
os: Linux-4.10.0-42-generic #46~16.04.1-Ubuntu SMP Mon Dec 4 15:57:59 UTC 2017
nodename: 88adfd3f2e93
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /app
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
building mime-types dictionary from file /etc/mime.types...547 entry found
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.5.4 (default, Dec 12 2017, 16:43:39)  [GCC 4.9.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x12277b0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x12277b0 pid: 1 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1)
spawned uWSGI worker 1 (pid: 10, cores: 1)

现在我不知道该怎么办了。通常使用 Django 应用程序时,我只会localhost:4000在浏览器中检查。但在这种情况下它不起作用,我什么也没得到。那么我该怎么办呢?

答案1

我刚刚明白了一点。“docker run”只能启动一个容器。但有些项目使用多个容器来提供不同的服务(比如这个),对于这些项目来说,应该使用 docker-compose。问题是数据库在另一个容器中。因此使用“docker-compose up”解决了这个问题。

虽然我现在有另一个问题,似乎需要查看数据库内部。有人知道如何登录容器内的数据库吗?

答案2

我不习惯使用docker。你知道从哪里获取源代码吗?

https://github.com/mirumee/saleor-demo

例如在这里,我可以看到Dockerfile

EXPOSE 8000
ENV PORT 8000

在 Django 中,服务器也指向 8000(参见docker-compose.yml

我会尝试 :

docker run --env SECRET_KEY="<the key>" -p 4000:8000 saleor

并监听 4000 端口

curl http://localhost:4000

端口 5432 似乎是 postgres 端口

答案3

运行命令时

docker build -t saleor .

这意味着你用Dockerfile虽然有添加用户和组在流程上。

RUN groupadd -r saleor && useradd -r -g saleor saleor

以及该目录归该用户/组所有:saleor/saleor

RUN mkdir -p /app/media /app/static  && chown -R saleor:saleor /app/

你需要添加--user saleor:saleordocker run,因此 uWSGI 将不会以 root 身份运行

docker run --env SECRET_KEY="<the key>" -p 4000:8000 --user saleor:saleor saleor

本地主机:4000然后就可以正常运行了。输出如下:

[uWSGI] getting INI configuration from /app/saleor/wsgi/uwsgi.ini
[uwsgi-static] added mapping for /static => /app/static
*** Starting uWSGI 2.0.17.1 (64bit) on [Tue Mar 12 15:10:03 2019] ***
compiled with version: 6.3.0 20170516 on 08 February 2019 17:14:12
os: Linux-4.4.0-142-generic #168~14.04.1-Ubuntu SMP Sat Jan 19 11:26:28 UTC 2019
nodename: xxxxxx
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /app
detected binary path: /usr/local/bin/uwsgi
your processes number limit is 524288
your memory page size is 4096 bytes
detected max file descriptor number: 524288
building mime-types dictionary from file /etc/mime.types...554 entry found
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :4000 fd 3
Python version: 3.6.8 (default, Feb  6 2019, 03:44:09)  [GCC 6.3.0 20170516]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x5XX
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364600 bytes (356 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x5XX pid: 1 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1)
spawned uWSGI worker 1 (pid: 9, cores: 1)
spawned uWSGI worker 2 (pid: 10, cores: 1)
spawned uWSGI worker 3 (pid: 11, cores: 1)
spawned uWSGI worker 4 (pid: 12, cores: 1)

相关内容