Using A Postgresql Database With Docker And Flask, How Does It Work?
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
Solution 1:
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 goos.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.
Post a Comment for "Using A Postgresql Database With Docker And Flask, How Does It Work?"