wger-project / docker

Production...ish docker-compose image for wger
GNU Affero General Public License v3.0
141 stars 47 forks source link

Deploy in RPi using MySQL or storing SQLite db #18

Open quietwalker-libre opened 2 years ago

quietwalker-libre commented 2 years ago

Hello people, thanks for this amazing project. I'm trying to deploy an instance of wger in RPi3 for myself but I cannot install PostgreSQL. So I want to know if it is possible to install it using my existing MYSQL DB or at least use the demo image but storing the SQLite DB in a volume. Regarding the first option (use MYSQL) I didn't find any information of note in the documentation.
Regarding the second option (use the demo image) I tried to attach 3 volume to the container, in the following path:

here is the compose-file:

version: "3.3"
services:
  wger:
    container_name: wger
    image: wger/demo:latest
    volumes:
      - /selfhosted_volume/wger/static/:/home/wger/static
      - /selfhosted_volume/wger/media/:/home/wger/media
      - /selfhosted_volume/wger/db/:/home/wger/db/
    ports:
       - 8000
    environment:
      - DJANGO_MEDIA_ROOT=/home/wger/media
      - ALLOW_REGISTRATION=True
      - ALLOW_GUEST_USERS=False
      - DJANGO_DEBUG=False

The container starts but when I try to access the webapp it give me the following exception:

Environment:

Request Method: GET
Request URL: http://wger.homenetwork.pi/en/software/features

Django Version: 3.2.11
Python Version: 3.9.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.messages',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'django_extensions',
 'storages',
 'wger.config',
 'wger.core',
 'wger.mailer',
 'wger.exercises',
 'wger.gym',
 'wger.manager',
 'wger.nutrition',
 'wger.software',
 'wger.utils',
 'wger.weight',
 'wger.gallery',
 'wger.measurements',
 'captcha',
 'django.contrib.sitemaps',
 'easy_thumbnails',
 'compressor',
 'crispy_forms',
 'rest_framework',
 'rest_framework.authtoken',
 'django_filters',
 'django_bootstrap_breadcrumbs',
 'corsheaders')
Installed Middleware:
('corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'wger.utils.middleware.JavascriptAJAXRedirectionMiddleware',
 'wger.utils.middleware.WgerAuthenticationMiddleware',
 'wger.utils.middleware.RobotsExclusionMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.locale.LocaleMiddleware')

Traceback (most recent call last):
  File "/home/wger/src/wger/utils/language.py", line 54, in load_language
    language = Language.objects.get(short_name=used_language)
  File "/home/wger/venv/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/wger/venv/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
    raise self.model.DoesNotExist(

During handling of the above exception (Language matching query does not exist.), another exception occurred:
  File "/home/wger/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/wger/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/wger/src/wger/software/views.py", line 37, in features
    return render(request, 'features.html', context)
  File "/home/wger/venv/lib/python3.9/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/wger/venv/lib/python3.9/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/wger/venv/lib/python3.9/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/wger/venv/lib/python3.9/site-packages/django/template/base.py", line 168, in render
    with context.bind_template(self):
  File "/usr/lib/python3.9/contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "/home/wger/venv/lib/python3.9/site-packages/django/template/context.py", line 244, in bind_template
    updates.update(processor(self.request))
  File "/home/wger/src/wger/utils/context_processor.py", line 28, in processor
    language = load_language()
  File "/home/wger/src/wger/utils/language.py", line 57, in load_language
    language = Language.objects.get(short_name="en")
  File "/home/wger/venv/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/wger/venv/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
    raise self.model.DoesNotExist(

Exception Type: DoesNotExist at /en/software/features
Exception Value: Language matching query does not exist.

I'm almost sure this problem is caused by the fact that I'm mounting a local directory into the container to store the the sqlite db, because I noticed that if I try to remove the volume from db everything works well. Do you have any suggestion on how I can fix this problem? Thanks in advance

rolandgeider commented 2 years ago

Hi, glad you like the project :)

If the application cant find the language, I would guess that the new database file is emtpy or at least wasn't initialized correctly. I'm not sure how docker works when you mount a folder on a path that already has data, it probably overwrites it. You might need to copy the database and the exercise images from the docker image and then mounting them (just guessing here).

The application works with mysql and any other database that django supports, so if you instal the app "natively" this wont be a problem. What exactly did you mean that you can't install postgres? If you use the docker compose, everything is configured. But that being said, if you really want to use mysql and docker you will need to adapt the dockerfile/docker-compose and add the necessary dependencies and services

quietwalker-libre commented 2 years ago

Hi Roland, thanks for your reply.

You might need to copy the database and the exercise images from the docker image and then mounting them (just guessing here)

Yes you were right: I've tried to copy the database from a demo standalone container using docker cp and then I put it in my host directory which should be mounted into the container and it works now. So problem solved! Thanks you so much.

Anyway I'm curious about the other solution (MySQL). If I install mysqlclient (by pip) and changing the reference from sqlite to mysql (setting django.db.backends.mysql and the correct host and port) I suppose that I should be able to use also MySQL then, am I right? Thanks in advance

rolandgeider commented 2 years ago

Glad it worked!

It's probably possible to make this run with mysql as well, if you already have a db server you will need to open the ports so that the app can talk to it (besides installing the client). You will also need to initialize the db and load the data, but that's two commands. Plus you need to consider that you will have to reinstall the client everytime you pull a new image