postgresql-hosting / Dockerfile
pritmanvar-bacancy's picture
Upload 3 files
f491ef3 verified
# Use a base image
FROM ubuntu:20.04
# Set environment variable to non-interactive mode
ENV DEBIAN_FRONTEND=noninteractive
# Install PostgreSQL and dependencies
RUN apt-get update && apt-get install -y \
postgresql postgresql-contrib \
python3 python3-pip \
tzdata && \
rm -rf /var/lib/apt/lists/*
# Reset DEBIAN_FRONTEND
ENV DEBIAN_FRONTEND=interactive
# Set environment variables for PostgreSQL
ENV POSTGRES_DB=mydatabase
ENV POSTGRES_USER=myuser
ENV POSTGRES_PASSWORD=mypassword
# Install application dependencies
COPY requirements.txt /app/requirements.txt
RUN pip3 install -r /app/requirements.txt
# Copy FastAPI application code
COPY app.py /app/app.py
# Set up the PostgreSQL database and user
RUN service postgresql start && \
su - postgres -c "psql -c \"CREATE USER myuser WITH PASSWORD 'mypassword';\"" && \
su - postgres -c "createdb mydatabase" && \
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;\""
# Expose PostgreSQL and FastAPI ports
EXPOSE 5432 8080
# Start PostgreSQL and FastAPI app together
CMD service postgresql start && cd app && uvicorn app:app --host 0.0.0.0 --port 8080