Docker -композиция не работает в docker -машина - PullRequest
0 голосов
/ 16 марта 2020

У меня есть приложение cookiecutter- django с docker, которое прекрасно работает, если я запускаю его локально с docker compose -f local.yml up. Теперь я пытаюсь развернуть его, поэтому сначала я создал docker -машин на своем компьютере (используя macOS Catalina) и активировал его.

Теперь внутри docker -машины работает docker-compose build хорошо, но при запуске приложения происходит сбой.

Есть идеи, что может произойти? Я пытался решить эту проблему уже почти неделю ...

Это мои журналы, когда я делаю docker-compose up в docker -машине:

Creating network "innovacion_innsai_default" with the default driver
Creating innovacion_innsai_postgres_1 ... done
Creating innovacion_innsai_django_1   ... done
Creating innovacion_innsai_node_1     ... done
Attaching to innovacion_innsai_postgres_1, innovacion_innsai_django_1, innovacion_innsai_node_1
postgres_1  | 2020-03-16 08:41:12.472 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2020-03-16 08:41:12.472 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2020-03-16 08:41:12.473 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2020-03-16 08:41:12.494 UTC [21] LOG:  database system was shut down at 2020-03-16 08:31:09 UTC
postgres_1  | 2020-03-16 08:41:12.511 UTC [1] LOG:  database system is ready to accept connections
django_1    | PostgreSQL is available
django_1    | Traceback (most recent call last):
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
django_1    |     return self.cursor.execute(sql, params)
django_1    | psycopg2.errors.UndefinedTable: relation "innovation_sector" does not exist
django_1    | LINE 1: ...n_sector"."id", "innovation_sector"."sector" FROM "innovatio...
django_1    |                                                              ^
django_1    | 
django_1    | 
django_1    | The above exception was the direct cause of the following exception:
django_1    | 
django_1    | Traceback (most recent call last):
django_1    |   File "manage.py", line 30, in <module>
django_1    |     execute_from_command_line(sys.argv)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
django_1    |     utility.execute()
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
django_1    |     self.fetch_command(subcommand).run_from_argv(self.argv)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
django_1    |     self.execute(*args, **cmd_options)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 361, in execute
django_1    |     self.check()
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
django_1    |     include_deployment_checks=include_deployment_checks,
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 65, in _run_checks
django_1    |     issues.extend(super()._run_checks(**kwargs))
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
django_1    |     return checks.run_checks(**kwargs)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
django_1    |     new_errors = check(app_configs=app_configs)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
django_1    |     all_namespaces = _load_all_namespaces(resolver)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
django_1    |     url_patterns = getattr(resolver, 'url_patterns', [])
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
django_1    |     res = instance.__dict__[self.name] = self.func(instance)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 584, in url_patterns
django_1    |     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
django_1    |     res = instance.__dict__[self.name] = self.func(instance)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
django_1    |     return import_module(self.urlconf_name)
django_1    |   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
django_1    |     return _bootstrap._gcd_import(name[level:], package, level)
django_1    |   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
django_1    |   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
django_1    |   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
django_1    |   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
django_1    |   File "<frozen importlib._bootstrap_external>", line 728, in exec_module
django_1    |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
django_1    |   File "/app/config/urls.py", line 18, in <module>
django_1    |     path("", include("innovacion_innsai.innovation.urls", namespace="innovation")),
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
django_1    |     urlconf_module = import_module(urlconf_module)
django_1    |   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
django_1    |     return _bootstrap._gcd_import(name[level:], package, level)
django_1    |   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
django_1    |   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
django_1    |   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
django_1    |   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
django_1    |   File "<frozen importlib._bootstrap_external>", line 728, in exec_module
django_1    |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
django_1    |   File "/app/innovacion_innsai/innovation/urls.py", line 2, in <module>
django_1    |     from innovacion_innsai.innovation import views
django_1    |   File "/app/innovacion_innsai/innovation/views.py", line 9, in <module>
django_1    |     from .analytics import alimentacion_cases, agro_cases, turismo_cases, movilidad_cases
django_1    |   File "/app/innovacion_innsai/innovation/analytics.py", line 17, in <module>
django_1    |     for case in Case.objects.filter(sector__sector=sectors[0]):
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 308, in __getitem__
django_1    |     qs._fetch_all()
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
django_1    |     self._result_cache = list(self._iterable_class(self))
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
django_1    |     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1133, in execute_sql
django_1    |     cursor.execute(sql, params)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
django_1    |     return super().execute(sql, params)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
django_1    |     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
django_1    |     return executor(sql, params, many, context)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
django_1    |     return self.cursor.execute(sql, params)
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
django_1    |     raise dj_exc_value.with_traceback(traceback) from exc_value
django_1    |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
django_1    |     return self.cursor.execute(sql, params)
django_1    | django.db.utils.ProgrammingError: relation "innovation_sector" does not exist
django_1    | LINE 1: ...n_sector"."id", "innovation_sector"."sector" FROM "innovatio...
django_1    |                                                              ^
django_1    | 
innovacion_innsai_django_1 exited with code 1
node_1      | 
node_1      | > innovacion_innsai@1.1.0 dev /app
node_1      | > gulp
node_1      | 
node_1      | [08:41:22] Using gulpfile /app/gulpfile.js
node_1      | [08:41:22] Starting 'default'...
node_1      | [08:41:22] Starting 'styles'...
node_1      | [08:41:22] Starting 'scripts'...
node_1      | [08:41:22] Starting 'imgCompression'...
node_1      | [08:41:22] gulp-imagemin: Minified 0 images

Это мой local.yml:

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: innovacion_innsai_local_django
    depends_on:
      - postgres
    volumes:
      - .:/app
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    ports:
      - "8000:8000"
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: innovacion_innsai_production_postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      - local_postgres_data_backups:/backups
    env_file:
      - ./.envs/.local/.postgres
    #Estas dos siguientes lineas las he añadido yo luego
    ports:
      - "5432:5432"


  node:
    build:
      context: .
      dockerfile: ./compose/local/node/Dockerfile
    image: innovacion_innsai_local_node
    depends_on:
      - django
    volumes:
      - .:/app
      # http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
      - /app/node_modules
    command: npm run dev
    ports:
      - "3000:3000"
      # Expose browsersync UI: https://www.browsersync.io/docs/options/#option-ui
      - "3001:3001"

А это мой. postgres файл в папке .envs / local:

# PostgreSQL
# ------------------------------------------------------------------------------
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=innovacion_innsai
POSTGRES_USER=debug
POSTGRES_PASSWORD=debug

1 Ответ

0 голосов
/ 16 марта 2020
File "/app/innovacion_innsai/innovation/analytics.py", line 17, in <module>
django_1    |     for case in Case.objects.filter(sector__sector=sectors[0]):

Это означает, что при инициализации django (в момент загрузки модулей) он выполняет запросы БД к базе данных, к которой не применены миграции. Вам нужно преобразовать все, что ваш код делает в строке 17, чтобы запустить его лениво.

...