This is not a "I need to run a SQL script right after the PostgreSQL container is built" question.
I have a FastAPI app with a dockerized PostgreSQL. My problem is: I want to populate the database upon app launch, but my tables are all generated by SQLAlchemy when I run uvicorn. So I need to be able to run the SQL with the INSERT statements right after uvicorn is executed.
To do that, I tried creating an entrypoint.sh
file to run psql commands, but at the end of docker compose up
I always get:
psql: command not found
I figured it's because Docker isn't inside the PostgreSQL container when it's executing entrypoint.sh
, but rather inside the main app container.
entrypoint.sh
:
#!/bin/bash
uvicorn backend.main:app --host 0.0.0.0 --port 8000 &
sleep 3
# Execute the populate.sql script
psql postgresql://postgres:postgres@db:5432/wikiprof
psql -a -f ./backend/populate.sql
Dockerfile
:
FROM python:3.11
WORKDIR /wikiprof
COPY ./requirements.txt /wikiprof/requirements.txt
COPY ./backend/ /wikiprof/backend/
COPY ./frontend/ /wikiprof/frontend/
COPY ./entrypoint.sh /wikiprof/
RUN chmod +x /wikiprof/entrypoint.sh
RUN pip install --no-cache-dir --upgrade -r /wikiprof/requirements.txt
CMD ["/wikiprof/entrypoint.sh"]
compose.yml
:
services:
db:
image: postgres
ports:
- "5434:5432"
environment:
- "POSTGRES_USER=postgres"
- "POSTGRES_PASSWORD=postgres"
- "POSTGRES_DB=wikiprof"
volumes:
- "/home/gabriel/wikiprof_bd:/var/lib/postgresql/data"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres", "-d", "wikiprof"]
interval: 3s
timeout: 50s
retries: 10
app:
build: .
ports:
- "8000:8000"
depends_on:
db:
condition: "service_healthy"
Is there any way I get inside the db
container from entrypoint.sh
and run psql? The main goal is running the SQL INSERTs upon launch, with no manual command running.
Am I technically doing something wrong or shouldn't I be having this approach altogether?