Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + Docker + SQLite3 How to mount database to the container?

I had sqlite database with some sort of tables on my localhost and i wanted to copy and paste that database to my server which runs on docker. I created paths like this:

enter image description here

db_data: there is my sqlite database which i want to run in my django project. web: there is my whole django project

in my docker-compose.yml i writed this volume:

version: "3"

services:

  web:
    build: ./web/
    ports:
      - "8001:8001"
    volumes:
      - ./web:/code
      - /home/cosmo/db_data/db.sqlite3:/code/db.sqlite3
    command: python manage.py runserver 0.0.0.0:8001

So i thik that docker will get database in my db_data and will make volume inside my web folder (in my project. There i had database on my localhost so it wouldnt be problem.) But i will paste here settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

So when i will open my db.sqlite3 inside db_data every tables and content are there, but when i will run container the db.sqlite3 in my project folder (web) is empty.

When i will run docker ps command there is no database container maybe this is the problem i dont know. I have there only:

enter image description here

in the red circle is my django container. So when i will run my server and try to login every account from the database is unknown. So I think that container works with that empty db in my project. Someone please has any solution? Thanks.

like image 939
idature Avatar asked Mar 21 '18 13:03

idature


1 Answers

  1. BASE_DIR will be the folder of the settings file, which generally is in a folder at the root. So likely you have a directory issue. If you can move the file directly into the same folder as the settings.py you can at least eliminate those variables.
  2. You won't see a container, its just binding a volume, which you should also make unique so don't nest it like you have /code/db.sqlite3 but put it in its own folder then you can reference that in your settings file. You could see list of volumes using docker volume ls (assumes Docker 1.8+)
like image 72
stormlifter Avatar answered Sep 28 '22 05:09

stormlifter