0

i'm new in docker and very like to learn that and for this i created a django app that use mysql for save users but when run docker-compose up it's show me below error:

WARNING: Found orphan containers (uservalidation_admin_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Starting uservalidation_db_1 ... done
Recreating uservalidation_web_1 ... done
Attaching to uservalidation_db_1, uservalidation_web_1
db_1   | 2021-03-09 16:22:33+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1   | 2021-03-09 16:22:33+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1   | 2021-03-09 16:22:33+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1   | 2021-03-09T16:22:33.865176Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
db_1   | 2021-03-09T16:22:34.262041Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1   | 2021-03-09T16:22:36.803850Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1   | 2021-03-09T16:22:36.807125Z 1 [ERROR] [MY-011096] [Server] No data dictionary version number found.
db_1   | 2021-03-09T16:22:36.807436Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
db_1   | 2021-03-09T16:22:36.807683Z 0 [ERROR] [MY-010119] [Server] Aborting
db_1   | 2021-03-09T16:22:37.332256Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
uservalidation_db_1 exited with code 1
db_1   | 2021-03-09 16:22:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1   | 2021-03-09 16:22:49+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1   | 2021-03-09 16:22:49+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1   | 2021-03-09T16:22:49.552755Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
db_1   | 2021-03-09T16:22:49.595687Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1   | 2021-03-09T16:22:50.785069Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1   | 2021-03-09T16:22:50.838098Z 1 [ERROR] [MY-011096] [Server] No data dictionary version number found.
db_1   | 2021-03-09T16:22:50.838509Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
db_1   | 2021-03-09T16:22:50.838816Z 0 [ERROR] [MY-010119] [Server] Aborting
db_1   | 2021-03-09T16:22:51.381748Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | Exception in thread django-main-thread:
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
web_1  |     return Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect
web_1  |     return Connection(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
web_1  |     super().__init__(*args, **kwargs2)
web_1  | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
web_1  | 
web_1  | The above exception was the direct cause of the following exception:
web_1  | 
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
web_1  |     self.run()
web_1  |   File "/usr/local/lib/python3.9/threading.py", line 892, in run
web_1  |     self._target(*self._args, **self._kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
web_1  |     self.check_migrations()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 459, in check_migrations
web_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
web_1  |     self.loader = MigrationLoader(self.connection)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
web_1  |     self.build_graph()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
web_1  |     self.applied_migrations = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
web_1  |     if self.has_table():
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
web_1  |     with self.connection.cursor() as cursor:
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
web_1  |     return self._cursor()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
web_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
web_1  |     return Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect
web_1  |     return Connection(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
web_1  |     super().__init__(*args, **kwargs2)
web_1  | django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
db_1   | 2021-03-09 16:22:55+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1   | 2021-03-09 16:22:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1   | 2021-03-09T16:22:55.963940Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
db_1   | 2021-03-09T16:22:55.986407Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.

docker-compose:

version: "3.9"

services: 
  db:
    image: mysql
    ports: 
      - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      - MYSQL_DATABASE=userdatavalidation
      - MYSQL_USER=root
      - MYSQL_PASSWORD=mjs
      - MYSQL_ROOT_PASSWORD=mjs
      - MYSQL_HOST=localhost
      - MYSQL_TCP_PORT=3306

    volumes: 
      -  /UserValidation/my_mysql/datadir:/var/lib/mysql
      -  /UserValidation/my_mysql/mysqld:/var/run/mysqld
    

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes: 
      - ./:/UserValidation
      - /UserValidation/my_mysql/mysqld:/run/mysqld
    ports: 
      - "8000:8000"
    depends_on: 
      - db

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'userdatavalidation',
        'USER': 'root',
        'PASSWORD': 'mjs',
        'HOST': 'db',
        'PORT': '3306',
        'default-character-set': 'utf8',
        'OPTIONS': {
            'sql_mode': 'traditional',
        }
    }
}

Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /UserValidation
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

in above Dockerfile i use python:3 and then create a directoy in docker and copy everything in my directory to them, with pip install -r requirements.txt in install all dependencies. but for install mysql-client in python i install : sudo apt-get install python3-dev default-libmysqlclient-dev build-essential then install mysql-client

Show me the above error in Django ERROR:

web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
db_1   | 2021-03-10T07:48:06.773044Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1   | 2021-03-10T07:48:06.776334Z 1 [ERROR] [MY-011096] [Server] No data dictionary version number found.
db_1   | 2021-03-10T07:48:06.776700Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
db_1   | 2021-03-10T07:48:06.776988Z 0 [ERROR] [MY-010119] [Server] Aborting
web_1  | System check identified no issues (0 silenced).
web_1  | Exception in thread django-main-thread:
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
web_1  |     return Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect
web_1  |     return Connection(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
web_1  |     super().__init__(*args, **kwargs2)
web_1  | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
web_1  | 
web_1  | The above exception was the direct cause of the following exception:
web_1  | 
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
web_1  |     self.run()
web_1  |   File "/usr/local/lib/python3.9/threading.py", line 892, in run
web_1  |     self._target(*self._args, **self._kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
web_1  |     self.check_migrations()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 459, in check_migrations
web_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
web_1  |     self.loader = MigrationLoader(self.connection)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
web_1  |     self.build_graph()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
web_1  |     self.applied_migrations = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
web_1  |     if self.has_table():
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
web_1  |     with self.connection.cursor() as cursor:
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
web_1  |     return self._cursor()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
web_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web_1  |     return func(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
web_1  |     return Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect
web_1  |     return Connection(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
web_1  |     super().__init__(*args, **kwargs2)
web_1  | django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
db_1   | 2021-03-10T07:48:07.315233Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23)  MySQL Community Server - GPL.
uservalidation_db_1 exited with code 1

django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db'

I really do not know what to do anymore :(

Thank you for your help

Mohammad Js
  • 29
  • 1
  • 6
  • Your application is starting up before the database is fully ready (note the `db_1` lines after the `web_1` error in the Compose log). You need to either explicitly wait for the database to be ready with a script or a retry loop (`depends_on:` isn't enough) or set your application container to restart if it hits a failure like this. – David Maze Mar 09 '21 at 18:25
  • I added this (**restart: on-failure**) but it did not change anything – Mohammad Js Mar 10 '21 at 07:58

0 Answers0