1

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

enter image description here

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

3
  • 1
    can you share the errors? Commented Apr 1, 2020 at 5:58
  • I guess you want to use trying\trying\settings.py but it is considering \trying\settings.py try changing path Commented Apr 1, 2020 at 6:11
  • @DARK_C0D3R Hi please have a look at my project repo github.com/subban358/songs_listing I want to Dockerize this so can you guide me Commented Apr 1, 2020 at 6:13

1 Answer 1

1

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',
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

you code was showing at first Ports are not available: listen tcp 0.0.0.0:3306: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted. So I changed the MySQL port to 3305 but then when I run docker-compose up manage.py migrate: error: unrecognized arguments: manage.py runserver 0.0.0.0:8000 I am getting this error
Please see the updated answer. Also, probably you don't need to use ports here unless you want to access mysql from your local machine
it is now saying File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 179, in __init__ web_1 | super(Connection, self).__init__(*args, **kwargs2) web_1 | django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
it is showing ERROR: The Compose file '.\docker-compose.yml' is invalid because: Unsupported config option for services.networks: 'djangonetwork' Hey I don't think it will be this much complex can you please go through my repo as and just help me dockerizing it forget about my approach I am new to docker I might be wrong
My bad. I fixed the issue with yml file.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.