简体   繁体   中英

Django Docker MySQL docker-compose up error

I am dockerizing my existing Django rest project which uses MySQL as database. I already have an exiting database with some data in it.

my dockerfile:

FROM python:2.7

ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r requirements.txt

My docker-compose:

version: '3'

services:
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: libraries
      MYSQL_USER: root
      MYSQL_PASSWORD: root
  web:
    build: .
    command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    volumes: 
      - .:/code
    ports:
      - "8000:8000"

My settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', #django.db.backends.mysql 
        'NAME': 'libraries', #local: libraries #server: 
        'USER': 'root', #localhost:root #root
        'PASSWORD': 'root', #local: root #server: 
        'HOST': 'db', #docker: db #local: localhost  #server:
        'PORT': '3306',

} }

My docker-compose build was successful.

However, docker-compose up gives me error with following log:

D:\Development\Personal projects\library_backend>docker-compose up
Creating librarybackend_web_1
Creating librarybackend_db_1
Attaching to librarybackend_db_1, librarybackend_web_1
db_1   | Initializing database
db_1   | 2018-01-15T09:33:20.031132Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1   | 2018-01-15T09:33:20.225632Z 0 [Warning] InnoDB: New log files created, LSN=45790
db_1   | 2018-01-15T09:33:20.259942Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
db_1   | 2018-01-15T09:33:20.304181Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1fb0980b-f9d7-11e7-a333-0242ac120002.
db_1   | 2018-01-15T09:33:20.310216Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
db_1   | 2018-01-15T09:33:20.311813Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
web_1  | Traceback (most recent call last):
web_1  |   File "manage.py", line 10, in <module>
web_1  |     execute_from_command_line(sys.argv)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
web_1  |     utility.execute()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
web_1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
web_1  |     self.execute(*args, **cmd_options)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 342, in execute
web_1  |     self.check()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check
web_1  |     include_deployment_checks=include_deployment_checks,
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 61, in _run_checks
web_1  |     issues = run_checks(tags=[Tags.database])
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
web_1  |     new_errors = check(app_configs=app_configs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
web_1  |     issues.extend(conn.validation.check(**kwargs))
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
web_1  |     issues.extend(self._check_sql_mode(**kwargs))
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
web_1  |     with self.connection.cursor() as cursor:
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 231, in cursor
web_1  |     cursor = self.make_debug_cursor(self._cursor())
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
web_1  |     six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 263, in get_new_connection
web_1  |     conn = Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
web_1  |     return Connection(*args, **kwargs)
web_1  |   File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
web_1  |     super(Connection, self).__init__(*args, **kwargs2)
web_1  | django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'db' (111)")
librarybackend_web_1 exited with code 1
db_1   | 2018-01-15T09:33:22.105314Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105400Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105421Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105455Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105463Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105485Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105541Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:22.105560Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | Database initialized
db_1   | Initializing certificates
db_1   | Generating a 2048 bit RSA private key
db_1   | .....+++
db_1   | ................+++
db_1   | unable to write 'random state'
db_1   | writing new private key to 'ca-key.pem'
db_1   | -----
db_1   | Generating a 2048 bit RSA private key
db_1   | .............................+++
db_1   | ...................................................+++
db_1   | unable to write 'random state'
db_1   | writing new private key to 'server-key.pem'
db_1   | -----
db_1   | Generating a 2048 bit RSA private key
db_1   | ...........................................+++
db_1   | .........................................................+++
db_1   | unable to write 'random state'
db_1   | writing new private key to 'client-key.pem'
db_1   | -----
db_1   | Certificates initialized
db_1   | MySQL init process in progress...
db_1   | 2018-01-15T09:33:24.906502Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1   | 2018-01-15T09:33:24.907479Z 0 [Note] mysqld (mysqld 5.7.20) starting as process 88 ...
db_1   | 2018-01-15T09:33:24.910342Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1   | 2018-01-15T09:33:24.910423Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1   | 2018-01-15T09:33:24.910438Z 0 [Note] InnoDB: Uses event mutexes
db_1   | 2018-01-15T09:33:24.910464Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1   | 2018-01-15T09:33:24.910477Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
db_1   | 2018-01-15T09:33:24.910678Z 0 [Note] InnoDB: Using Linux native AIO
db_1   | 2018-01-15T09:33:24.911096Z 0 [Note] InnoDB: Number of pools: 1
db_1   | 2018-01-15T09:33:24.911363Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1   | 2018-01-15T09:33:24.912837Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1   | 2018-01-15T09:33:24.918731Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1   | 2018-01-15T09:33:24.920395Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1   | 2018-01-15T09:33:24.931813Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1   | 2018-01-15T09:33:24.951614Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1   | 2018-01-15T09:33:24.951919Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1   | 2018-01-15T09:33:24.975721Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1   | 2018-01-15T09:33:24.977199Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1   | 2018-01-15T09:33:24.977267Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1   | 2018-01-15T09:33:24.977999Z 0 [Note] InnoDB: Waiting for purge to start
db_1   | 2018-01-15T09:33:25.028348Z 0 [Note] InnoDB: 5.7.20 started; log sequence number 2565377
db_1   | 2018-01-15T09:33:25.029193Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1   | 2018-01-15T09:33:25.029854Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1   | 2018-01-15T09:33:25.031013Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180115  9:33:25
db_1   | 2018-01-15T09:33:25.034240Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db_1   | 2018-01-15T09:33:25.034617Z 0 [Warning] CA certificate ca.pem is self signed.
db_1   | 2018-01-15T09:33:25.042691Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.042766Z 0 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.042788Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.042820Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.042930Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.042991Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.044138Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.044179Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:25.049567Z 0 [Note] Event Scheduler: Loaded 0 events
db_1   | 2018-01-15T09:33:25.050237Z 0 [Note] mysqld: ready for connections.
db_1   | Version: '5.7.20'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server (GPL)
db_1   | 2018-01-15T09:33:25.050281Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
db_1   | 2018-01-15T09:33:25.050295Z 0 [Note] Beginning of list of non-natively partitioned tables
db_1   | 2018-01-15T09:33:25.058196Z 0 [Note] End of list of non-natively partitioned tables
db_1   | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1   | 2018-01-15T09:33:27.674996Z 5 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675066Z 5 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675096Z 5 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675301Z 5 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675335Z 5 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675438Z 5 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675600Z 5 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-01-15T09:33:27.675630Z 5 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | mysql: [Warning] Using a password on the command line interface can be insecure.
db_1   | mysql: [Warning] Using a password on the command line interface can be insecure.
db_1   | ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
librarybackend_db_1 exited with code 1

If I run the command second time then it works. Why is it happening?

Since the issue seems to be intermittent, I suspect the services/containers have a dependency like your Django service has a dependency on DB service hence you need to tell/define this in compose so that the DB container is always brought up prior to your Django application. You can do this by adding depends_on to your web service in compose as below -

version: '3'

services:
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: libraries
      MYSQL_USER: root
      MYSQL_PASSWORD: root

  web:
    build: .
    command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    volumes: 
      - .:/code
    depends_on:
      - db
    ports:
      - "8000:8000"

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