Permission Denied while building Django project with Docker

I'm trying to build a Django project with Docker, but I always get this error and I don't know how to handle it:

/usr/local/lib/python3.13/site-packages/django/coremanagement/commands
makemigrations.py:160: RuntimeWarning: Got an error checking a consistent 
migration history performed for database connection 'default': unable to open 
database file

There are more errors down the line, but all of them are about permission to create a db.sqlite3 database.

docker-compose.yml:

version: "3.9"

services:
  puppint:
    build: .
    container_name: puppint_app
    ports:
      - "8000:8000"
    volumes:
      - .:/app
      - ./static:/app/static
    env_file:
      - ./api.env
    working_dir: /app
    command: >
      sh -c "python puppint.py"

Dockerfile:

FROM python:3.13-slim AS builder

RUN mkdir /app

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN pip install --upgrade pip

COPY requirements.txt /app/

RUN pip install --no-cache-dir -r requirements.txt

FROM python:3.13-slim

RUN useradd -m -r appuser && \
   mkdir /app && \
   chown -R appuser /app

COPY --from=builder /usr/local/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin/

WORKDIR /app

COPY --chown=appuser:appuser . .

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

USER appuser

EXPOSE 8000

CMD ["python3 puppint.py"]

puppint.py is a script that starts making migrations. I've created it mainly for checking whether the user is a superuser or not. puppint.py:

import os
import subprocess
from time import sleep

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'puppint.settings')

import django
django.setup()
from django.contrib.auth.models import User

def run_command(command):
    try:
        subprocess.run(command, shell=True, check=True)
    except subprocess.CalledProcessError as e:
        print(f"Error when executing '{command}': {e}")

def check_superuser():
    if not User.objects.filter(is_superuser=True).exists():
        print("No superuser in database!\n")
        username = input("Enter username: ")
        email = input("Enter e-mail: ")
        password = input("Enter password: ")
        User.objects.create_superuser(username, email, password)

def main():

    print_banner()
    sleep(1)

    run_command("python manage.py makemigrations app")
    run_command("python manage.py migrate")
    run_command("python manage.py collectstatic --noinput")

    check_superuser()

    run_command("python manage.py runserver 0.0.0.0:8000")

if __name__ == "__main__":
    main()

Do you know how can I allow this permission for creating the database etc. when building the Django application? Thanks!

You're running into this error because the Docker container is trying to create or write to the db.sqlite3 file, but the user it's running as (appuser) doesn't have permission. This usually happens when you mount your local project directory (.) into the container (/app), which overrides the internal folder's permissions. To fix it, you can either run the container as root, change the permissions of your local folder with chmod -R 777 ., or make sure the /app directory inside the container is owned by the right user by using COPY --chown=appuser:appuser . . and setting write permissions if needed.

Вернуться на верх