Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Mysql Database is not created in Docker

I setup a django project in docker container and every thing is working as expected, except I don't find the project database in mysql image.

Dockerfile

FROM python:3

RUN mkdir /django-website
WORKDIR /django-website
COPY . /django-website
RUN pip install -r requirements.txt

docker-compose.yml

version: '3'

services:
    db:
        image: mysql:5.7
        restart: always
        environment:
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_DATABASE=mywebsite
            - MYSQL_USER=root
            - MYSQL_PASSWORD=root
        ports:
            - '33060:3306'
        volumes:
            - /var/lib/mysql
    web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - .:/django-website
        ports:
            - '8000:8000'
        links:
            - db

settings.py

DATABASES = {
    'default': {
        'ENGINE': "django.db.backends.mysql",
        'NAME': "mywebsite",
        'USER': "root",
        'PASSWORD': "root",
        'HOST': 'db',
        'PORT': '3306',
    }
}

I ran migrate and it worked:

docker-compose run web python manage.py migrate

I createdsuperuser:

docker-compose run web python manage.py createsuperuser

The development server is working docker-compose up and the site is working as expected, the issue when I navigate in mysql image I don't find my project related database which is mywebsite .

can you please tell me what is missing? if the database is not created, where has the migration been applied?

Thanks in advance.

like image 447
Radico Avatar asked Sep 22 '18 13:09

Radico


2 Answers

I'm not sure what you mean by "I logged in mysql image shell but didn't find mywebsite database"

You are migrated the DB successfully, which means, the DB connections are valid and working.

In your docker-compose.yml file, the port mapping done like this, '33060:3306', which means the db's port 3306 is mapped to host machine's port 33060. So, this may be the issue (it's not an issue, kind of typo)

How to check the DB contents?

METHOD-1: check through django-shell of web container
1. run docker-compose up
2. open a new terminal in the same path and run docker ps
you'll get something like below

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
795093357f78        django_1_11_web     "python manage.py ru…"   34 minutes ago      Up 11 minutes       0.0.0.0:8000->8000/tcp    django_1_11_web_1
4ae48f291e34        mysql:5.7           "docker-entrypoint.s…"   34 minutes ago      Up 12 minutes       0.0.0.0:33060->3306/tcp   django_1_11_db_1

3.Get into the web container by docker exec -it 795093357f78 bash command, where 795093357f78 is the respective container id
4. now you're inside the container. Then, run the command python manage.py dbshell. Now you will be in MYSQL shell of mywebsite (Screenshot)
5. run the command show tables;. It will display all the tables inside the mywebsite DB

METHOD-2: check through db container
1. repeat the steps 1 and 2 in above section
2. get into db container by docker exec -it 4ae48f291e34 bash
3. Now you'll be in bash terminal of MYSQL. Run the following commmand mysql -u root -p and enter the password when prompt
4. now you're in MYSQL server. run the command, show databases;. This will show all the databases in the server.

like image 125
JPG Avatar answered Nov 14 '22 23:11

JPG


Have you tried defining the database image in the dockerfile? The following link is somewhat related to your problem: https://medium.com/@lvthillo/customize-your-mysql-database-in-docker-723ffd59d8fb

like image 28
Chris Procak Avatar answered Nov 14 '22 23:11

Chris Procak