retspen / webvirtcloud

WebVirtCloud is virtualization web interface for admins and users
1.7k stars 372 forks source link

How can i set Dynamic Variable Docker #482

Closed anhnvme closed 2 years ago

anhnvme commented 2 years ago

Hello,

I want to set postgres information as variables for Docker images. What should I edit for settings.py ?

i'm tried with code

from pathlib import Path
import os

...

DATABASES = {
    "default": {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ['POSTGRES_DB'],
        'USER': os.environ['POSTGRES_USER'],
        'PASSWORD': os.environ['POSTGRES_PASSWORD'],
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

docker command

docker run --name postgres --net=host -e POSTGRES_USER=user -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_DB=database #postgres
docker run --name webcloud --net=host -e POSTGRES_USER=user -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_DB=database #webvircloud

then I'm exec to webvircloud docker, run

cd /srv/webvirtcloud
source venv/bin/activate
python manage.py makemigrations
python manage.py migrate

It's migrate successs

root@cloud-webvirtcloud-694c79848d-ffmsj:/srv/webvirtcloud# source venv/bin/activate
(venv) root@cloud-webvirtcloud-694c79848d-ffmsj:/srv/webvirtcloud# python manage.py makemigrations
Migrations for 'accounts':
  accounts/migrations/0006_auto_20211223_0453.py
    - Alter field id on userattributes
    - Alter field id on userinstance
    - Alter field id on usersshkey
Migrations for 'appsettings':
  appsettings/migrations/0006_alter_appsettings_id.py
    - Alter field id on appsettings
Migrations for 'computes':
  computes/migrations/0004_alter_compute_id.py
    - Alter field id on compute
Migrations for 'instances':
  instances/migrations/0010_auto_20211223_0453.py
    - Alter field id on flavor
    - Alter field id on instance
Migrations for 'logs':
  logs/migrations/0004_alter_logs_id.py
    - Alter field id on logs
Migrations for 'otp_totp':
  venv/lib/python3.8/site-packages/django_otp/plugins/otp_totp/migrations/0003_alter_totpdevice_id.py
    - Alter field id on totpdevice
(venv) root@cloud-webvirtcloud-694c79848d-ffmsj:/srv/webvirtcloud# python manage.py migrate
Operations to perform:
  Apply all migrations: accounts, admin, appsettings, auth, computes, contenttypes, instances, logs, otp_totp, sessions
Running migrations:
  Applying computes.0001_initial... OK
  Applying instances.0001_initial... OK
  Applying instances.0002_permissionset... OK
  Applying instances.0003_auto_20200615_0637... OK
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying accounts.0001_initial... OK
  Applying accounts.0002_permissionset... OK
  Applying accounts.0003_auto_20200604_0930... OK
  Applying accounts.0004_auto_20200615_0637... OK
  Applying accounts.0005_auto_20200616_1039... OK
  Applying accounts.0006_auto_20211223_0453... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_auto_20200609_0830... OK
  Applying appsettings.0001_initial... OK
  Applying appsettings.0002_auto_20200527_1603... OK
  Applying appsettings.0003_auto_20200615_0637... OK
  Applying appsettings.0004_auto_20200716_0637... OK
  Applying appsettings.0005_auto_20200911_1233... OK
  Applying appsettings.0006_alter_appsettings_id... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying computes.0002_auto_20200529_1320... OK
  Applying computes.0003_auto_20200615_0637... OK
  Applying computes.0004_alter_compute_id... OK
  Applying instances.0004_auto_20200618_0817... OK
  Applying instances.0005_flavor... OK
  Applying instances.0006_addFlavors... OK
  Applying instances.0007_auto_20200624_0821... OK
  Applying instances.0008_auto_20200708_0950... OK
  Applying instances.0009_auto_20200717_0524... OK
  Applying instances.0010_auto_20211223_0453... OK
  Applying logs.0001_initial... OK
  Applying logs.0002_auto_20200615_0637... OK
  Applying logs.0003_logs_host... OK
  Applying logs.0004_alter_logs_id... OK
  Applying otp_totp.0001_initial... OK
  Applying otp_totp.0002_auto_20190420_0723... OK
  Applying otp_totp.0003_alter_totpdevice_id... OK
  Applying sessions.0001_initial... OK
* Creating default admin user
! SHOW_PROFILE_EDIT_PASSWORD is found inside settings.py
* Applying permission can_change_password for all users
* Warning!!! Setting to False for all users
! Don`t forget to remove the option from settings.py
* Migrating can_clone_instaces user attribute to permission
* Applying permission passwordless_console for all users

But while i'm check postgres database, it's empty

root@cloud-webvirtcloud-694c79848d-ffmsj:/# psql -U postgres database
psql (14.1 (Debian 14.1-1.pgdg110+1))
Type "help" for help.

database=# \dt
Did not find any relations.
catborise commented 2 years ago

did you install "psycopg2" package with pip? add it to conf/requirements.txt then install it again

anhnvme commented 2 years ago

did you install "psycopg2" package with pip? add it to conf/requirements.txt then install it again

sure, i have done this step, if hard code database information, it's work correctly, just error when working via docker environment

catborise commented 2 years ago

os.environ['POSTGRES_DB'], os.environ['POSTGRES_USER'], os.environ['POSTGRES_PASSWORD'],

did you check these environment variables if they are accessible or not.

(run python command import os then print os.environs to see if they are accessible in docker)

anhnvme commented 2 years ago

os.environ['POSTGRES_DB'], os.environ['POSTGRES_USER'], os.environ['POSTGRES_PASSWORD'],

did you check these environment variables if they are accessible or not.

(run python command import os then print os.environs to see if they are accessible in docker)

yes, i'm tested with that variable, i use print os.environ['POSTGRES_DB'] it's show variable fine at /var/log/webvirtcloud.log

catborise commented 2 years ago

to use env variables may be you should look at django-environ package: https://pypi.org/project/django-environ-docker/

django has different style for environment variables usages.

anhnvme commented 2 years ago

to use env variables may be you should look at django-environ package:

thank you