在浏览器中打开时,Docker上的Django runserver不响应

时间:2018-04-11 19:28:45

标签: django docker

我只是这个

的初学者

所以当我跑

sudo docker-compose run web python manage.py runserver    

显示

Starting thirddj_db_1 ... done  
usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: 
UserWarning: The psycopg2 wheel package will be renamed from release 
2.8; in order to keep installing from binary please use "pip install 
psycopg2-binary" instead. For details see: 
<http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: 
UserWarning: The psycopg2 wheel package will be renamed from release 
2.8; in order to keep installing from binary please use "pip install 
psycopg2-binary" instead. For details see: 
<http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Performing system checks...

System check identified no issues (0 silenced).
April 11, 2018 - 19:15:59
Django version 1.11.12, using settings 'composeexample.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

然后当我在浏览器中打开它时,它显示网站无法到达

但是当我跑步时

docker-compose up

显示

web_1  | /usr/local/lib/python3.6/site- 
packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel 
package will be renamed from release 2.8; in order to keep installing 
from binary please use "pip install psycopg2-binary" instead. For 
details see: <http://initd.org/psycopg/docs/install.html#binary- 
install-from-pypi>.
web_1  |   """)
web_1  | /usr/local/lib/python3.6/site- 
packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel 
package will be renamed from release 2.8; in order to keep installing 
from binary please use "pip install psycopg2-binary" instead. For 
details see: <http://initd.org/psycopg/docs/install.html#binary- 
install-from-pypi>.
web_1  |   """)
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | April 11, 2018 - 19:21:41
web_1  | Django version 1.11.12, using settings   
'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

然后当我在浏览器中打开它时,它可以正常工作

所以我想知道为什么sudo docker-compose运行web python manage.py runserver不工作,两个命令之间有什么区别。

这是我的docker-compose文件

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

2 个答案:

答案 0 :(得分:0)

运行Web容器时,必须运行命令,并启用服务端口并映射到主机。

试试这个:

sudo docker-compose run --service-ports web python manage.py runserver 

答案 1 :(得分:0)

docker-compose up将启动或重新启动docker-compose.yml中定义的所有服务。

当您运行docker-compose up时,它将使用主机和来宾计算机绑定已定义的端口,而docker-compose run web python manage.py runserver将仅运行该特定服务(web,而不进行任何端口映射< /强>

您可以使用此命令代替run命令:

sudo docker-compose run --service-ports web python manage.py runserver 

端口映射示例:

web:
  links:
    - mysqldb
    - redis
  ports:
    - "8000:8000"
  command: python manage.py runserver 0.0.0.0:8000

<强>参考