I am trying to create Docker solution which connects my Python script (for creating tables and inserting rows) into Postgres database. Please have on mind I am new into Docker.
I followed tutorial which I found on the internet and I managed to connect Python script with database, at least according to output in terminal. It seems rows are inserting. But I cannot see them in the database and I am pretty sure I am looking into wrong place, but can't find out where I need to look for.
Structure of my project is following:
Folder 'app' containing:
app/app.py - python script where database connection is specified (and SQL queries are performed, but not relevant for issue to copy here):
db_name = 'apps7_db'
db_user = 'apps7_user'
db_pass = 'apps7'
db_host = 'db'
#db_host = 'localhost'
db_port = '5432'
# Connecto to the database
db_string = 'postgresql://{}:{}@{}:{}/{}'.format(db_user, db_pass, db_host, db_port, db_name)
db = create_engine(db_string)
app/Dockerfile:
FROM python:latest
WORKDIR /code
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
CMD ["python", "-u", "app.py"]
Folder 'database':
database/Dockerfile:
FROM postgres:latest
ENV POSTGRES_PASSWORD=apps7
ENV POSTGRES_USER=apps7_user
ENV POSTGRES_DB=apps7_db
COPY create_fixtures.sql /docker-entrypoint-initdb.d/create_fixtures.sql
And in root folder I have docker-compose.yml:
version: "3.8"
services:
app :
build: ./app/
db:
build: ./database/
I am building container with the command 'docker-compose up --build' which will build docker containers without errors (apps7_ad_network-db-1, apps7_ad_network-app-1), and I can see my Python script output in terminal where it seems that rows are added to database table 'numbers' created with the script.
My question is, based on following DB connection and parameters provided above, how can I access/query my DB table 'numbers' to confirm that script is working fine. I tried to access it through Docker CLI of apps7_ad_network-db-1 container using command 'psql -d postgres -U postgres' or with 'psql -d apps7_user -U apps7' but I am getting following error:
'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role does not exist'
I also had container called postgre-container where I can access using 'psql -d postgres -U postgres'. But my database and table are not visible there.
So, why I can't access to apps7_ad_network-db-1 container using postgres user? How am I suppose to access it with any user credentials? Can I add user somehow?
Or another solution, can I connect my project to postgre-container container where I have access with postgres user?