我正在尝试对mysql和Django应用程序进行Dockerize,但是面对很多问题,您可以帮助我。

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'trying',
        'USER': 'root',
        'PASSWORD': '159753',
        'HOST': 'db',
        'PORT': '3306',
    }
}

Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /my_app_dir
WORKDIR /my_app_dir
ADD requirements.txt /my_app_dir/
RUN pip install -r requirements.txt
ADD . /my_app_dir/

docker-compose.yml
version: '3'

services:
  db:
    image: mysql:5.7
    ports:
      - '3305:3306'
    environment:
       MYSQL_DATABASE: 'trying'
       MYSQL_USER: 'root'
       MYSQL_PASSWORD: '159753'
       MYSQL_ROOT_PASSWORD: '159753'
  web:
    build: .
    command: "python trying/manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/my_app_dir
    ports:
      - "8000:8000"
    depends_on:
      - db

我之前在跑步
docker-compose run web trying\manage.py migrate

但出现很多错误

错误是
我正在运行命令并收到此错误,我希望我的容器与我的MySQL服务器连接并正常工作,但我遇到问题,请帮忙
Traceback (most recent call last):
  File ".\trying\manage.py", line 21, in <module>
    main()
  File ".\trying\manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 341, in run_from_argv
    connections.close_all()
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 225, in close_all
    for alias in self:
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 219, in __iter__
    return iter(self.databases)
  File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 153, in databases
    self._databases = settings.DATABASES
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'trying.settings'

这是目录结构

python - 我无法使用docker-compose Dockerize MySQL和Django App-LMLPHP

看看我的项目仓库。我想对此进行Dockerize,以便您可以指导我project

最佳答案

您可以这样尝试:

首先更新dockerfile:

FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /my_app_dir
WORKDIR /my_app_dir
ADD requirements.txt /my_app_dir/
RUN pip install -r requirements.txt

然后像这样更新撰写文件:
version: '3'

services:
  db:
    image: mysql:5.7
    environment:
       MYSQL_DATABASE: 'trying'
       MYSQL_USER: 'root'
       MYSQL_PASSWORD: '159753'
       MYSQL_ROOT_PASSWORD: '159753'
    networks:
      - djangonetwork
  web:
    build: .
    command:
      - /bin/bash
      - -c
      - |
        python manage.py migrate
        python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./trying:/my_app_dir
    ports:
      - "8000:8000"
    depends_on:
      - db
    networks:
      - djangonetwork

networks:
  djangonetwork:
    driver: bridge

最后,在更新DATABASE_SETTINGS:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'trying',
        'USER': 'root',
        'PASSWORD': '159753',
        'HOST': 'db',  # <-- Here
        'PORT': '3306',
    }
}

09-30 00:31