Django + Docker + SQLite3:如何将本地数据库挂载至容器?
Looks like you're on the right track with your docker-compose setup, but let's walk through the key steps to ensure your local database is properly accessible to your Dockerized Django app:
1. Align Django's Database Settings with Container Paths
First, confirm your Django settings.py points to the exact path of the SQLite file inside the container. Based on your current volume mount, that should be:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': '/code/db.sqlite3', # Matches the container path you mounted } }
If you ever decide to mount the entire db_data directory instead of just the file (a cleaner approach for future maintenance), update the path to /code/db_data/db.sqlite3 here and adjust your volumes in docker-compose accordingly.
2. Validate Your Volume Configuration
Your current docker-compose volume setup is mostly correct, but let's double-check the full config to avoid gaps:
version: "3" services: web: build: ./web/ ports: - "8001:8001" volumes: - ./web:/code # Mounts your local Django project to /code in the container - /home/cosmo/db_data/db.sqlite3:/code/db.sqlite3 # Mounts local DB to container path command: python manage.py runserver 0.0.0.0:8001
Just ensure the absolute path to your local db.sqlite3 is accurate—typos here will cause Django to create a new empty database instead of using your existing one.
3. Fix File Permission Issues (Critical!)
Docker containers often run as a specific user (root by default, or a non-root user if your Dockerfile defines one), and this user needs read/write access to your local SQLite file.
- If using root (default): Run these commands on your host machine to grant root access:
sudo chmod 664 /home/cosmo/db_data/db.sqlite3 sudo chown root:root /home/cosmo/db_data/db.sqlite3 - If using a non-root user: If your Dockerfile creates a user with UID 1000 (common for matching host user permissions), update the file ownership on your host:
sudo chown 1000:1000 /home/cosmo/db_data/db.sqlite3
Alternatively, you can add user: "1000:1000" to your web service in docker-compose.yml to run the container as your host user, which avoids permission headaches entirely.
4. Test the Setup
Start your containers with:
docker-compose up --build
Then, verify the database is accessible by running a Django shell inside the container:
docker-compose exec web python manage.py shell
In the shell, run a quick check to confirm your data is present (replace MyModel with one of your actual models):
from myapp.models import MyModel print(MyModel.objects.count())
If this returns the number of records from your local database, you're good to go!
Troubleshooting Common Problems
- Empty database: Double-check the
NAMEpath insettings.pymatches the container's mounted file path, and ensure your host path in docker-compose has no typos. - Permission denied: Adjust file permissions as outlined in step 3, or run the container with a user that has access to the file.
- Database locked: SQLite doesn't handle concurrent writes well—make sure no local processes (like your local Django dev server) are using the database while the container is running.
内容的提问来源于stack exchange,提问作者idature

