Open marqpdx opened 3 years ago
Thank you @marqpdx! Great tutorial! :)
The database in docker is ok for development or for hobby projects. For production projects, I will recommend using with RDS service (or similar).
If you have DB connection setup ready, I would recommend for you for the next step to use python-decouple (to hide passwords from code).
Thank you @marqpdx once again!
Thanks @pplonski - i'll look into both of those and add any writeups that might be useful.
The main thing it seems is Postgresql outside of Docker, either using RDS or similar service, or even a dedicated VPS with the db setup and well tuned, yes?
All the best!
Nothing is broken at all, but i went ahead and setup a connection to a persistent postgresql database, and wanted to lay out here what i did in case it's of use to anyone.
It's possible that there are better ways to do the steps below, but it works :)
Plus, feel free to use any of this if there are any additional articles in this great series!
Updates to files
Dockerfile
WORKDIR /app
:and (note, i had to add gcc for compile reasons)
docker-compose.yml
nginx:
volumes
section at the bottom:service/backend
section:settings.py
updates to django's
backend/server/server/settings.py
file. add this after the default sqlite3 db is set (change name of db/user/pwd as you wish; just make sure to change it to match in thedocker-compose.yml
file as noted above):requirements.txt
addition to
backend/requirements.txt
:Note, you'll likely need to comment this line out for locally. ideally we can add a server check so this doesn't get added locally when using sqlite3 default db.
That's it.
Doing those steps, it auto created the volume first time i launched, and the database connected successfully, and the db persists across sessions, even if i rebuild the docker images.
Working w/ the postgres db directly
This is only useful if you can login to your docker server via ssh and work through the command line. Once logged into the server via ssh, you do:
docker ps
to get container iddocker exec -it [container_id_here] sh
to get into postgres container as ansh
shellsu postgres
to change to the user who owns the db situationpsql
to take you into psql so you can work with db.to get out, just type 'exit' a few times.
Conclusion.
I really appreciated this tutorial, one of the most useful and i wanted to help w/ folks interested in persistent db connection.