简体   繁体   English

使用 MySQL 容器对 Django 应用程序进行 Dockerize

[英]Dockerize a Django app with a MySQL container

I have an app developed in Django (2.2.7) with python (3.8.0), Docker (19.03.5) and docker-compose (1.25.2) running in Windows 10 pro.我有一个在 Django (2.2.7) 中开发的应用程序,使用 python (3.8.0)、Docker (19.03.5) 和 docker-compose (1.25.2) 在 Windows 10 专业版中运行。 I want to Dockerize it with changing the sqlite3 database for a MySQL database.我想通过更改 MySQL 数据库的 sqlite3 数据库来对它进行 Dockerize。 I've already write this Dockerfile :我已经写了这个Dockerfile

FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install --upgrade pip && pip install -r requirements.txt
RUN pip install mysqlclient
COPY . /code/

And this docker-compose.yml file:这个docker-compose.yml文件:

version: '3'

services: 
  db:
    image: mysql:5.7
    ports:
      - '3306:3306'
    environment:
       MYSQL_DATABASE: 'my-app-db'
       MYSQL_USER: 'root'
       MYSQL_PASSWORD: 'password'
       MYSQL_ROOT_PASSWORD: 'password'
    volumes:
      - .setup.sql:/docker-entrypoint-initbd.d/setup.sql

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    links: 
      - db 

Also I have change the default database configurations in settings.py for this:我也为此更改了settings.py的默认数据库配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my-app-db',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': 3306,
    }
}

After all of this the docker compose works and the app starts, but the problem is that the tables in the database are not created.在所有这些之后,docker compose 工作并且应用程序启动,但问题是未创建数据库中的表。 I've tried with these How do I add a table in MySQL using docker-compose , Seeding a MySQL DB for a Dockerized Django App or this Seeding a MySQL DB for a Dockerized Django App but I couldn't fix it yet.我已经尝试过这些如何使用 docker-compose 在 MySQL 中添加表、 为 Dockerized Django 应用程序播种 MySQL 数据库为 Dockerized Django 应用程序播种 MySQL 数据库,但我无法修复它。

How can I create the required tables in the MySQL db container while runing the docker-compose?如何在运行 docker-compose 时在 MySQL db 容器中创建所需的表? Must I add every single table by hand or there is a way to do it from the django app automatically?我必须手动添加每个表还是有一种方法可以从 django 应用程序自动添加?

Thanks谢谢

Hi i think this answer helps you ##1.- Reset all your migrations嗨,我认为这个答案可以帮助您##1.- 重置所有迁移

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

##2.- See and apply your migrations again ##2.- 再次查看并应用您的迁移

python manage.py showmigrations
python manage.py makemigrations
python manage.py migrate

You should not reset your migrations unless you want to wipe all of the data completely and start over.除非您想完全擦除所有数据并重新开始,否则不应重置迁移。 Your migrations should exist beforehand.您的迁移应该事先存在。 So if you dont mind about old migrations you can delete them and use python manage.py makemigrations and then proceed to following steps:所以如果你不介意旧的迁移,你可以删除它们并使用python manage.py makemigrations然后继续以下步骤:

So if your applications starts at the moment, we need to update your docker-compose file in a way that it uses entrypoint.sh.因此,如果您的应用程序此时启动,我们需要以使用 entrypoint.sh 的方式更新您的 docker-compose 文件。 An ENTRYPOINT allows you to configure a container that will run as an executable. ENTRYPOINT 允许您配置将作为可执行文件运行的容器。 First things first, create your entrypoint.sh file on the same level as docker-compose.yaml .首先,在与docker-compose.yaml相同的级别上创建entrypoint.sh文件。 Next, don't forget to add chmod +x entrypoint.sh so entrypoint can be executed接下来,不要忘记添加chmod +x entrypoint.sh以便可以执行entrypoint

entrypoint.sh file:入口点.sh文件:

#!/bin/bash

set -e

echo "${0}: running migrations."
python manage.py migrate --noinput

echo "${0}: collecting statics."

python manage.py collectstatic --noinput
python manage.py runserver 0.0.0.0:8000

Afterwards update your docker-compose.yaml file.之后更新您docker-compose.yaml文件。 Change your command line to:command行更改为:

command:
  - /bin/sh
  - '-c'
  - '/code/entrypoint.sh'

Additionally you should store all of your pip requirements in requirements.txt file and in your Dockerfile you should run pip install -r requirements.txt此外,您应该将所有 pip 要求存储在requirements.txt文件中,并在您的Dockerfile运行pip install -r requirements.txt

You can dump your pip requirements with a command pip freeze > requirements.txt您可以使用命令pip freeze > requirements.txt转储您的 pip pip freeze > requirements.txt

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

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