使用MySQL的Django Rest Docker创建一个超级用户

时间:2018-02-13 10:21:42

标签: django docker django-rest-framework django-mysql

我已将我现有的使用MySQL数据库的Django Rest项目暂存。

我的dockerfile:

FROM python:2.7

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

我的docker-compose.yml文件:

version: '3'

services:
  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"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: libraries
      MYSQL_USER: root
      MYSQL_PASSWORD: root

我的命令docker-compose builddocker-compose up成功,后面的输出是:

D:\Development\personal_projects\library_backend>docker-compose up
Starting librarybackend_db_1 ... done
Starting librarybackend_web_1 ... done
Attaching to librarybackend_db_1, librarybackend_web_1
db_1   | 2018-02-13T10:11:48.044358Z 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-02-13T10:11:48.045250Z 0 [Note] mysqld (mysqld 5.7.20) starting as process 1 ...
db_1   | 2018-02-13T10:11:48.047697Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1   | 2018-02-13T10:11:48.047857Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1   | 2018-02-13T10:11:48.048076Z 0 [Note] InnoDB: Uses event mutexes
db_1   | 2018-02-13T10:11:48.048193Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1   | 2018-02-13T10:11:48.048297Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
db_1   | 2018-02-13T10:11:48.048639Z 0 [Note] InnoDB: Using Linux native AIO
db_1   | 2018-02-13T10:11:48.048928Z 0 [Note] InnoDB: Number of pools: 1
db_1   | 2018-02-13T10:11:48.049119Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1   | 2018-02-13T10:11:48.050256Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1   | 2018-02-13T10:11:48.056054Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1   | 2018-02-13T10:11:48.058064Z 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-02-13T10:11:48.069243Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1   | 2018-02-13T10:11:48.081867Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1   | 2018-02-13T10:11:48.082237Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1   | 2018-02-13T10:11:48.096687Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1   | 2018-02-13T10:11:48.097392Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1   | 2018-02-13T10:11:48.097433Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1   | 2018-02-13T10:11:48.097666Z 0 [Note] InnoDB: Waiting for purge to start
db_1   | 2018-02-13T10:11:48.147792Z 0 [Note] InnoDB: 5.7.20 started; log sequence number 13453508
db_1   | 2018-02-13T10:11:48.148222Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1   | 2018-02-13T10:11:48.148657Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1   | 2018-02-13T10:11:48.151181Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180213 10:11:48
db_1   | 2018-02-13T10:11:48.152154Z 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-02-13T10:11:48.152545Z 0 [Warning] CA certificate ca.pem is self signed.
db_1   | 2018-02-13T10:11:48.153982Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
db_1   | 2018-02-13T10:11:48.154147Z 0 [Note] IPv6 is available.
db_1   | 2018-02-13T10:11:48.154261Z 0 [Note]   - '::' resolves to '::';
db_1   | 2018-02-13T10:11:48.154373Z 0 [Note] Server socket created on IP: '::'.
db_1   | 2018-02-13T10:11:48.160505Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.160745Z 0 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.160859Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.161025Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.161147Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.161266Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.168523Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.168734Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2018-02-13T10:11:48.172735Z 0 [Note] Event Scheduler: Loaded 0 events
db_1   | 2018-02-13T10:11:48.173195Z 0 [Note] mysqld: ready for connections.
db_1   | Version: '5.7.20'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
db_1   | 2018-02-13T10:11:48.173365Z 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-02-13T10:11:48.173467Z 0 [Note] Beginning of list of non-natively partitioned tables
db_1   | 2018-02-13T10:11:48.180866Z 0 [Note] End of list of non-natively partitioned tables
web_1  | Operations to perform:
web_1  |   Apply all migrations: account, admin, auth, authtoken, contenttypes, libraries, sessions, sites
web_1  | Running migrations:
web_1  |   No migrations to apply.
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  | February 13, 2018 - 10:11:50
web_1  | Django version 1.10.3, using settings 'config.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

我现在可以通过点击localhost:8000来访问我的应用。但是,由于它在容器中创建了一个新的数据库实例,我不知道如何在那里创建超级用户并登录到我的管理界面。通常没有docker,我运行命令python manage.py createsuperuser,它启动交互式命令提示符以输入admin用户的凭据。

  1. 我该怎么处理?
  2. 如果我有一个包含数据的现有数据库,我如何在容器中的这个数据库中使用它(填充表格)?

1 个答案:

答案 0 :(得分:0)

我可以通过简单地运行命令来创建一个超级用户:docker-compose run web python manage.py createsuperuser打开交互式命令提示符以输入管理员凭据,然后我可以通过我的管理界面登录