简体   繁体   English

在docker上运行python网站

[英]Run python website on docker

I have created a Python + Flask website on docker. 我在docker上创建了一个Python + Flask网站。 If I do "docker-compose up" the website is available on 0.0.0.0:5000 . 如果我做“docker-compose up”,该网站可在0.0.0.0:5000

I have tagged and pushed the container to docker and I tried doing docker run mydomain/mycontainer from a different computer. 我已经标记并将容器推送到docker run mydomain/mycontainer ,我尝试从另一台计算机上docker run mydomain/mycontainer It starts downloading everything, but at the end it displays Status: Downloaded newer image for mydomain/mycontainer:latest and nothing happens. 它开始下载所有内容,但最后它显示Status: Downloaded newer image for mydomain/mycontainer:latest ,没有任何反应。

I tried doing docker run mydomain/mycontainer python app.py and it displays "Running on http://0.0.0.0:5000 " but if I open that URL I get "This website is not available". 我尝试使用docker run mydomain/mycontainer python app.py并显示“在http://0.0.0.0:5000上运行”,但如果我打开该URL,我会收到“此网站不可用”。 I cannot see any processes running on port 5000 either. 我也看不到任何进程在端口5000上运行。

Any ideas? 有任何想法吗?

The ip 0.0.0.0 is the local address inside the docker image. ip 0.0.0.0是docker镜像的本地地址。 If you want to access it outside the image (ie. locally on your computer), you have to expose the given port. 如果要在映像外部访问它(即在计算机本地),则必须公开给定端口。 Here is the EXPOSE parameter documentation . 这是EXPOSE参数文档

If you run the command: 如果您运行该命令:

docker run -p 5000:5000 mydomain/mycontainer python app.py

Your server will be accessible on: 您的服务器可在以下位置访问:

http://localhost:5000 HTTP://本地主机:5000

BTW, I made a public (and open source) Docker image with all the bells and whistles that you can use to build a Python Flask web application. 顺便说一句,我制作了一个公共(和开源)Docker镜像,其中包含可用于构建Python Flask Web应用程序的所有铃声和口哨声。

It has uWSGI for running the application, Nginx to serve HTTP and Supervisord to control them, so you don't have to learn how to install and configure all those to build your Python Flask web app. 它有uWSGI用于运行应用程序,Nginx用于服务HTTP,Supervisord用于控制它们,因此您不必学习如何安装和配置所有这些来构建Python Flask Web应用程序。

It seems like uWSGI with Nginx is one of the more robust (and with great performance) ways to deploy a Python web app. 使用Nginx的uWSGI似乎是部署Python Web应用程序的更强大(并且性能更好)的方法之一。 Here are the benchmarks: http://nichol.as/benchmark-of-python-web-servers . 以下是基准测试: http//nichol.as/benchmark-of-python-web-servers

There are even some template projects you can use to bootstrap your own. 甚至还有一些模板项目可用于引导您自己的项目。 And also, you don't have to clone the full project or something, you can just use it as a base image. 而且,您不必克隆整个项目或其他东西,您只需将其用作基本图像。

Docker Hub: https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/ Docker Hub: https//hub.docker.com/r/tiangolo/uwsgi-nginx-flask/

GitHub: https://github.com/tiangolo/uwsgi-nginx-flask-docker GitHub: https//github.com/tiangolo/uwsgi-nginx-flask-docker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM