简体   繁体   中英

I can not Dockerize MySQL and Django App using docker-compose

I am trying to Dockerize mysql and Django app but facing a lot of issues can you help me with it.

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

I was running

docker-compose run web trying\manage.py migrate

but getting a lot of errors

error is I was running the command and getting this error I want my container to connect with my MySQL server and work perfectly but I am facing issues please help

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'

this is the directory structure

在此处输入图片说明

have a look at my project repo. I want to Dockerize this so can you guide me project

You can try like this:

First update the 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

Then update the compose file like this:

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

Finally, in update the DATABASE_SETTINGS :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM