django - docker容器已构建并正在运行但无法正常工作

标签 django docker docker-compose dockerfile

我叫奥马尔。我有一个刚创建并运行的项目的docker镜像和容器。因此,我目前正在尝试将其推送到在线环境之前在本地计算机上对其进行测试。我在没有先前缓存的情况下构建了它,并且它构建了在线运行的新docker。为什么它在我的浏览器中不起作用。这就是我所拥有的。

(MySplit) omars-mbp:mysplit omarjandali$ docker build --no-cache -t validation_test_1 .
Sending build context to Docker daemon  56.07MB
Step 1/7 : FROM python:3
 ---> 79e1dc9af1c1
Step 2/7 : WORKDIR users/
Removing intermediate container 7030351beb91
 ---> 30ac3f4ccbae
Step 3/7 : COPY requirements.txt ./
 ---> 57cbbd7335ab
Step 4/7 : EXPOSE 80
 ---> Running in 592407a176ff
Removing intermediate container 592407a176ff
 ---> 523945ea529f
Step 5/7 : RUN pip install -r requirements.txt
 ---> Running in 48347f772fbe
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/18/2d/b477232dd619d81766064cd07ba5b35e956ff8a8c5c5d41754e0392b96e3/Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Collecting gunicorn==19.7 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/96/4b/bc4bc2dad60defaa3f7d8590dc51331a225a5399380c161047c1224cd86d/gunicorn-19.7.0-py2.py3-none-any.whl (112kB)
Collecting django-localflavor==1.5.3 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/da/47/69e53f69fb50a38766aa929c1f81fd2e6315edc7f3945174ead24ffcf6df/django-localflavor-1.5.3.tar.gz (4.7MB)
Collecting synapse_pay_rest_native==3.1.1 (from -r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/43/27/b73d83dd50d4dfec1680d22896b800ddbb6bf8fb3f3a1755a916a6e4f732/synapse_pay_rest_native-3.1.1.tar.gz
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
Collecting requests (from synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting idna<2.7,>=2.5 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
Collecting urllib3<1.23,>=1.21.1 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
Collecting certifi>=2017.4.17 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
Building wheels for collected packages: django-localflavor, synapse-pay-rest-native
  Running setup.py bdist_wheel for django-localflavor: started
  Running setup.py bdist_wheel for django-localflavor: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/36/41/a8/30c023fe29300f1a19d0520aaff1faf9e4c7ab176c53913ab5
  Running setup.py bdist_wheel for synapse-pay-rest-native: started
  Running setup.py bdist_wheel for synapse-pay-rest-native: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/33/93/7f/6bd8dd39a1c22e2ef85a259a45e603dff2b0ab4d6209a7a976
Successfully built django-localflavor synapse-pay-rest-native
Installing collected packages: pytz, Django, gunicorn, django-localflavor, chardet, idna, urllib3, certifi, requests, synapse-pay-rest-native
Successfully installed Django-1.11.5 certifi-2018.4.16 chardet-3.0.4 django-localflavor-1.5.3 gunicorn-19.7.0 idna-2.6 pytz-2018.4 requests-2.18.4 synapse-pay-rest-native-3.1.1 urllib3-1.22
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 48347f772fbe
 ---> 5749b63a2fef
Step 6/7 : COPY . .
 ---> 7754d9db1f38
Step 7/7 : CMD ["python", "manage.py", "runserver"]
 ---> Running in 362648ae0426
Removing intermediate container 362648ae0426
 ---> c26bf2de11b4
Successfully built c26bf2de11b4
Successfully tagged validation_test_1:latest
(MySplit) omars-mbp:mysplit omarjandali$ docker run -d -P validation_test_1:latest
b038c688ba4e681c03d8e093cf630a564d2f7cea27812b55dd0d1c33aa0f27b4
(MySplit) omars-mbp:mysplit omarjandali$ docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                   NAMES
b038c688ba4e        validation_test_1:latest   "python manage.py ru…"   5 seconds ago       Up 4 seconds        0.0.0.0:32768->80/tcp   zealous_mcnulty
(MySplit) omars-mbp:mysplit omarjandali$

我已经尝试了以下localhost:32768127.0.0.1:32768有人知道为什么会这样。

我已在django项目中允许所有主机。

docker文件
FROM python:3
WORKDIR users/
COPY requirements.txt ./
EXPOSE 80
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver"]

最佳答案

这是因为Django的运行服务器默认情况下仅接受来自127.0.0.1的连接,并且当您从浏览器中命中localhost:32768时,该连接不是来自127.0.0.1 / localhost,而是来自您的实际主机IP。

将您的CMD更改为:

CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]

关于django - docker容器已构建并正在运行但无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146503/

相关文章:

python - 如何限制 Django raw_id_field 的 ForeignKey 选择的选择

docker - 将mysql客户端容器连接到mysql服务器容器Docker

docker - Keycloak 无法在 Docker 撰写文件中工作。我该如何修复它?

php - docker php,nginx 与 wordpress super 慢

node.js - 等待 node.js 直到 Logstash 准备好使用容器

sql-server - 在容器中附加 SQL 数据库不起作用

sql - 如何提供测试 Django 应用程序所需的 SQL 函数和 View

python - django form.is_valid 返回 false

python - 如何为 django QuerySet 的每个条目添加一些上下文

ssl - jwilder/nginx 代理 : Not able to integrate ssl with Nginx