Docker with Python and PostgreSQL

I some questions about Docker. I have very less knowledge about it, so kindly bear with me.

I have a python script that does something and writes into a PostgreSQL DB. Both are run on Docker. Python uses python:3.8.8-buster and PostgreSQL postgres:13. Using docker-compose up, I am able to instantiate both these services and I see the items inserted in the PostgreSQL table. When I docker-compose down, as usual, the services shut down as expected. Here are the questions I have:

  1. When I run the container of the PostgreSQL service by itself (not using docker-compose up, but docker run then docker exec) then login into db using PSQL, it doesn’t take the db name as the db name mentioned in the docker-compose.yml file. It takes localhost, but with the username mentioned per the docker-compose.yml file. It also doesn’t ask me for the password, although it’s mentioned in the Dockerfile itself(not docker-compose.yml – for each of the services, I have a Dockerfile that I build in the docker-compose.yml). Is that expected? If so, why?
  2. After I’ve logged in, when I SELECT * FROM DB_NAME; it displays 0 records. So, basically it doesn’t display the records written in the DB in the previous run. Why’s that? How can I see the contents of the DB when it’s not up? When the container is running (when I docker-compose up), I know I can see the records from PG Admin (which BTW is also a part of my docker-compose.yml file, and I have it only to make it easier to see if the records have been written into the DB).
  3. So after my script runs, and it writes into the db, it stops. Is there a way to restart with without docker-compose down then docker-compose up? (On VSCode) when I simply run the script, while still docker-compose is up it says it cannot find the db (that’s mentioned in the docker-compose.yml file). So I have to go back and change the db name in the script to point localhost – This circles back to the question #1.

I am new to docker, and I am trying my best to wrap my head around all this. Any help would be greatly appreciated.

Source: Docker Questions