在Docker和Flask中使用PostgreSQL数据库,它是如何工作的? [英] Using a PostgreSQL database with Docker and Flask, how does it work?

查看:398
本文介绍了在Docker和Flask中使用PostgreSQL数据库,它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Docker,并创建了一个图像,并运行Python3,Flask,UWSGI和nginx。



现在我想在Flask中使用postgresql数据库。我阅读以下页面,链接容器似乎对我来说是合乎逻辑的。 ( https://hub.docker.com/_/postgres/



我还有一些问题,或者Docker的原理对我来说不够清楚。但是,如果我创建一个postgresql映像并运行容器,并将其链接到我的Flask应用程序,会发生什么情况:




  • 我重建图像重新启动容器,我的数据库数据在哪里?它已经没有了?

  • 我想在我的Flask(Docker)应用程序中使用我的数据库,需要在我的配置中放置什么? (DATABASE_URI,NAME等。)

  • 我想备份我的数据库,或者在其中加载数据?我可以连接到它吗?


    您可能已经注意到我显然是与Docker合作的初学者,也许我只是误解原则。真的很感谢,如果有人能指出我的方向正确!

    解决方案

    我重建图像或重新启动容器,我的数据库数据去?它消失了吗?


    • 不,数据不会消失。数据被移除的唯一时间是如果你删除容器: docker rm<我的postgres容器> 。唯一一次这是不正确的是,如果您将容器挂载到容器以暴露数据量: docker run -td -p 5432:5432 -v / mydata / volume:/ var / lib / postgresql / data postgres:9.5.2



    我想在我的Flask(Docker)应用程序,我需要把我的配置? (DATABASE_URI,NAME等)。


    • 这可能是一个争论的话题,但是我会使用你设置的环境变量容器: docker run -td -p 80:5000 -e POSTGRES_URL = 172.12.20.1 mycontainer / flask:latest 在你的配置文件中,你会去 os .getenv('POSTGRES_URL','localhost')。这允许你默认的本地主机,如果容器连接,否则你可以指向另一台机器上运行的另一个容器。这样做更好,因为它可以在部署中提供更大的灵活性。



    我想备份我的数据库,或者在其中加载数据?我可以连接到它吗?


    • 是的,就像其他任何你可以连接到Postgres的 IP:PORT 使用您在容器运行时指定的凭证。


    I just started using Docker and created an image and running container with Python3, Flask, UWSGI and nginx.

    Now I want to use a postgresql database in Flask. I read the following page and linking containers seems logical to me. (https://hub.docker.com/_/postgres/)

    I still have some question or maybe the principle of Docker isn't just clear enough for me. But if I create a postgresql image and running container, and link this to my Flask application, what happens if:

    • I rebuild the image or restart the container, where does my database data go? Is it gone?
    • I want to use my database in my Flask (Docker) application, what do I need to put in my config? (DATABASE_URI, NAME etc..)
    • I want to back-up my database, or load data in it? Can I just connect to it?

    As you may have noticed I am clearly a beginner of working with Docker, maybe I'm just misunderstanding the principle. Really appreciate it if someone could point me in the right direction!

    解决方案

    I rebuild the image or restart the container, where does my database data go? Is it gone?

    • No, the data is not gone. The only time data is removed is if you remove the container: docker rm <my postgres container>. The only time this isn't true is if you mount a volume to the container to expose the data volume: docker run -td -p 5432:5432 -v /mydata/volume:/var/lib/postgresql/data postgres:9.5.2

    I want to use my database in my Flask (Docker) application, what do I need to put in my config? (DATABASE_URI, NAME etc..)

    • This can be a subject of debate but I would use an environment variable that you set when you start the container: docker run -td -p 80:5000 -e POSTGRES_URL=172.12.20.1 mycontainer/flask:latest In your config you would go os.getenv('POSTGRES_URL', 'localhost'). This allows you to default to localhost if the container is linked otherwise you can point it to another container running on another machine. This is better because it allows greater flexibility in your deployment.

    I want to back-up my database, or load data in it? Can I just connect to it?

    • Yes, just like anything else you can connect to Postgres on IP:PORT using the credentials you specified at container runtime.

    这篇关于在Docker和Flask中使用PostgreSQL数据库,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆