Docker-compose 在 docker-机器中不工作

Docker-compose does not work in docker-machine

我有一个带有 docker 的 cookiecutter-django 应用程序,如果我 运行 在本地使用 docker compose -f local.yml up 它可以正常工作。现在我正在尝试部署它,所以首先我在我的计算机中创建了一个 docker-machine(使用 macOS Catalina)并激活了它。

现在,在 docker 机器中,docker-compose build 工作正常,但是当我 运行 它时,应用程序崩溃了。

知道会发生什么吗?我已经尝试解决这个问题将近一个星期了...

这是我在 docker-机器上执行 docker-compose up 时的日志:

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"

这是我在 .envs/local 文件夹中的 .postgres 文件:

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

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 行所做的任何事情转换为 运行 它是惰性的。