简体   繁体   English

在 Docker 上运行的 Django 中的密码学错误

[英]Cryptography error in Django run on Docker

I'm trying to deploy a web site which has a backend developed in Django.我正在尝试部署一个具有在 Django 中开发的后端的网站。 I've set two Docker containers which contain a Django instance and a MySQL instance.我设置了两个 Docker 容器,其中包含一个 Django 实例和一个 MySQL 实例。 I've set also a network between containers in order to create a communication.我还在容器之间设置了一个网络以创建通信。 The problem is that when I start the MySQL container with the parameter MYSQL_ROOT_PASSWORD Django gives me the error below:问题是,当我使用参数 MYSQL_ROOT_PASSWORD 启动 MySQL 容器时,Django 给了我以下错误:

return Database.connect(**conn_params)
app    |   File "/usr/local/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
app    |     return Connection(*args, **kwargs)
app    |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 325, in __init__
app    |     self.connect()
app    |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 599, in connect
app    |     self._request_authentication()
app    |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 882, in _request_authentication
app    |     auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
app    |   File "/usr/local/lib/python3.7/site-packages/pymysql/_auth.py", line 264, in caching_sha2_password_auth
app    |     data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
app    |   File "/usr/local/lib/python3.7/site-packages/pymysql/_auth.py", line 142, in sha2_rsa_encrypt
app    |     raise RuntimeError("cryptography is required for sha256_password or caching_sha2_password")
app    | RuntimeError: cryptography is required for sha256_password or caching_sha2_password

I've checked that the 3306 port is available on my PC, and yes it is.我已经检查过 3306 端口是否在我的 PC 上可用,是的。 On the other hand, if I start MySQL container with MYSQL_ALLOW_EMPTY_PASSWORD, and set a blank password in settings.py, it works.另一方面,如果我用 MYSQL_ALLOW_EMPTY_PASSWORD 启动 MySQL 容器,并在 settings.py 中设置一个空白密码,它就可以工作。

Here there is my settings.py这是我的 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        'USER': 'username',
        'PASSWORD': 'userpwd',
        'HOST': 'db',
        'PORT': '3306'
    }
}

Instead, this is my docker-compose.yml file相反,这是我的 docker-compose.yml 文件

version: "3"
services:
  app:
    build:
      context: .
    container_name: app
    ports:
      - "8000:8000"
    networks:
      - backend_split
    depends_on:
      - db
    restart: always
    volumes:
      - ./app:/app
    command:
      sh -c "python manage.py runserver 0.0.0.0:8000"
  db:
    image: mysql
    container_name: db
    expose:
      - "3306"
    networks:
      - backend_split
    environment:
      - MYSQL_ROOT_PASSWORD=rootpwd
      - MYSQL_DATABASE=databasename
      - MYSQL_USER=username
      - MYSQL_PASSWORD=userpwd

networks:
  backend_split:

I've been stuck with this error for days.我已经被这个错误困扰了好几天了。

Thank you in advance.先感谢您。

I've solved this problem on my own.我自己解决了这个问题。 I've opened all kinds of stackOverflow topic about this but nothing.我已经打开了关于这个的各种 stackOverflow 主题,但什么也没有。 I searched "pip install cryptography" and the tricky was here.我搜索了“pip install cryptography”,问题就在这里。 So, in Docker which emulate an Alpine environment, the solution is to add these lines of code into your Dockerfile:因此,在模拟 Alpine 环境的 Docker 中,解决方案是将这些代码行添加到您的 Dockerfile 中:

RUN apk add gcc
RUN apk add --no-cache libressl-dev musl-dev libffi-dev
RUN pip install --no-cache-dir cryptography==2.1.4
RUN apk del libressl-dev \
        musl-dev \
        libffi-dev

And... it run.而且……它运行。

You can solve it by installing this dependency:您可以通过安装此依赖项来解决它:

  1. $ pip install django-cryptographic-fields

  2. Add then add "cryptographic_fields" to your INSTALLED_APPS setting like this:添加然后将“cryptographic_fields”添加到您的 INSTALLED_APPS 设置中,如下所示:

INSTALLED_APPS = (
...
'cryptographic_fields',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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