5

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.

Radico
  • 336
  • 1
  • 5
  • 19
  • What do you mean, when you "navigate the image"? MySQL databases are not created as files on disk, where are you expecting to see it? – Daniel Roseman Sep 22 '18 at 13:46
  • I know it won't be a file created on the disk. I navigate in mysql image means that I connected to mysql shell in the docker image and used `SHOW DATABASES` commands but I didn't find `mywebsite` database. should I find it somewhere else? – Radico Sep 22 '18 at 14:04
  • Are you sure you have told Django to use MySQL? Show your DATABASES setting. – Daniel Roseman Sep 22 '18 at 14:15
  • The question has been updated. I am wondering how the migrations been applied correctly and the development server is working without any issue. – Radico Sep 22 '18 at 14:21
  • Can you share the results when you ran `docker-compose run web python manage.py migrate` ? – Arihant Sep 22 '18 at 14:35
  • @Arihant The result is I mentioned above, the migrations have been successfully applied. – Radico Sep 22 '18 at 15:03
  • Where is the `CREATE DATABASE mywebsite`? – Rick James Sep 25 '18 at 04:12
  • There is already a question raised: https://stackoverflow.com/questions/39175194/docker-compose-persistent-data-mysql Did you try it? – Nagaraj Tantri Sep 25 '18 at 10:52
  • @RickJames, actually this is the question, migrations have been successfully applied and the development server is working that means db exists, I logged in **mysql image shell** but didn't find mywebsite database. – Radico Sep 25 '18 at 13:15
  • How do "navigate in mysql image" exactly? I spinned up a fresh project with your config files and I manage to connect with Pycharm without any issues. The migrations have been applied and the DB is there. – Bruno A. Sep 25 '18 at 17:41
  • @BrunoA. “The DB is there” where exactly? This what I’m looking for, I don’t see any database in MySQL image shell – Radico Sep 25 '18 at 17:47

4 Answers4

6

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.

Bruno A.
  • 1,765
  • 16
  • 17
JPG
  • 82,442
  • 19
  • 127
  • 206
  • Thanks @JPG. **I logged in mysql image shell but didn't find mywebsite database** is the same **Get into the web container by docker exec -it 795093357f78 bash**. However, I was able to find the database. In my machine `docker images` shows three results, one of them `mysql:5.7` I got into this container that's why didn't find `mywebsite` database. As I understood, a new container will be created and associated with the project whenever run `docker-compose up`, is that right? – Radico Sep 26 '18 at 13:02
  • 1
    An instance of an image is called a container. So whenever run `docker-compose up` it builds, (re)creates, starts, and attaches to containers for a service. – JPG Sep 26 '18 at 13:13
  • I think these SO posts will help more :) [What is the difference between a Docker image and a container?](https://stackoverflow.com/questions/23735149/what-is-the-difference-between-a-docker-image-and-a-container) and [What is the difference between docker-compose up and docker-compose start? ](https://stackoverflow.com/questions/33715499/what-is-the-difference-between-docker-compose-up-and-docker-compose-start) – JPG Sep 26 '18 at 13:16
2

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

  • 1
    Thanks for sharing but this is not the case. mysql image works perfectly and I am able to login in the shell. I was able to create project database manually but unfortunately this database has no tables after performing successful migrations. – Radico Sep 22 '18 at 15:07
0

I supposed that ports value of host container should be 3306 not 33060. Use docker-compose.yml with value 3306 :

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:
            - '3306: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

Hope this works!

Suresh
  • 489
  • 3
  • 7
0

You should change the compose specification to version '2'. Take down the container and bring it back up with docker-compose up -d. Or if you intend to stay with version 3, you can instead use the following specification for database environment parameters

```
  environment:
    MYSQL_ROOT_PASSWORD: root
    MYSQL_DATABASE: mywebsite
    MYSQL_USER: root
    MYSQL_PASSWORD: root
```

When you have problems with containers not coming up, docker logs <container-name> --tail 25 -f can give you a lot of information about the cause.

Akongnwi Devert
  • 1,177
  • 12
  • 10