3

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. I want to Dockerize it with changing the sqlite3 database for a MySQL database. I've already write this 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:

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:

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. 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.

How can I create the required tables in the MySQL db container while runing the docker-compose? Must I add every single table by hand or there is a way to do it from the django app automatically?

Thanks

Manuel Gijón
  • 138
  • 1
  • 11
  • You need to run migrations. `docker exec -it bash` then run `python manage.py migrate`? – markwalker_ Feb 07 '20 at 10:20
  • I've already done that, ```No migrations to apply.``` The problem maybe is in the conexion with the db? – Manuel Gijón Feb 07 '20 at 10:25
  • Hi Manual, what errors or info do you get back when you run `docker-compose up` in the terminal... – Micheal J. Roberts Feb 07 '20 at 11:00
  • Hi, there is no error, everything seems to work and I can access to the loging page of the app, in fact I can access to the admin profile because the auth databases are created and I can create users from the admin or the command line. The probem are the other tables, the ones that I define in the project that for unknown reason are not created in the MySQL database. – Manuel Gijón Feb 07 '20 at 11:05
  • Haven't you forgotten to mention the name of your app in INSTALLED_APPS in your `settings.py`? – Fomalhaut Feb 07 '20 at 11:27
  • No, I haven't. Everything works nice with the .sqlite3 database in local. The problems started since I try to put everything inside a docker container to take it to production, changing the original db for a MySQL one. I don't know what the problem is, but the must be connected since the auth information is in the new database, no? – Manuel Gijón Feb 07 '20 at 11:32
  • Should be mapping the container names – Hax0r Nov 13 '20 at 09:35

2 Answers2

2

Hi i think this answer helps you ##1.- Reset all your migrations

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

##2.- See and apply your migrations again

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

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:

So if your applications starts at the moment, we need to update your docker-compose file in a way that it uses entrypoint.sh. An ENTRYPOINT allows you to configure a container that will run as an executable. First things first, create your entrypoint.sh file on the same level as docker-compose.yaml. Next, don't forget to add chmod +x entrypoint.sh so entrypoint can be executed

entrypoint.sh file:

#!/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. Change your command line to:

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

You can dump your pip requirements with a command pip freeze > requirements.txt

sebb
  • 1,956
  • 1
  • 16
  • 28