I am attempting to dockerize my flask + postgres app, but I am running into this problem (none of the answers in that question helped me). Basically I want the docker-compose file to create a database with a different name than postgres. I would like to keep all of my docker setup contained in the dockerfile, docker-compose, and .env file. Is there anyone who can help?
Dockerfile:
FROM python:3.12-slim
RUN apt-get -q -y update
RUN apt-get install -y gcc
ENV USERNAME=app_user
ENV WORKING_DIR=/home/app_db
WORKDIR ${WORKING_DIR}
COPY app app
COPY requirements.txt .
COPY service_entrypoint.sh .
RUN groupadd ${USERNAME} && useradd -g ${USERNAME} ${USERNAME}
RUN chown -R ${USERNAME}:${USERNAME} ${WORKING_DIR}
RUN chmod -R u=rwx,g=rwx ${WORKING_DIR}
USER ${USERNAME}
ENV PATH "$PATH:/home/${USERNAME}/.local/bin"
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV FLASK_APP=app_db:serve
RUN chmod +x service_entrypoint.sh
EXPOSE 5000
RUN flask db init
ENTRYPOINT [ "./service_entrypoint.sh" ]
docker-compose.yml:
services:
db:
image: postgres
container_name: db
restart: always
ports:
- 5123:5432
env_file:
- path: ./.env
volumes:
- ./pData:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB} -t 1"]
interval: 10s
timeout: 10s
retries: 10
start_period: 10s
app:
container_name: app
build:
context: .
dockerfile: Dockerfile
ports:
- 8008:5000
depends_on:
db:
condition: service_healthy
links:
- db
A .env file:
POSTGRES_DB=app_db
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
I don’t know if this means anything, but I also get this message:
db | PostgreSQL Database directory appears to contain a database; Skipping initialization
The error message i get (I am assuming that it is from the healthcheck) is FATAL: database "app_db" does not exist
. As a last note, when I attach to the postgres image in a tty shell, there is only the database postgres and a couple of templates.