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.

11
  • 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? Commented Sep 22, 2018 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? Commented Sep 22, 2018 at 14:04
  • Are you sure you have told Django to use MySQL? Show your DATABASES setting. Commented Sep 22, 2018 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. Commented Sep 22, 2018 at 14:21
  • Can you share the results when you ran docker-compose run web python manage.py migrate ? Commented Sep 22, 2018 at 14:35

4 Answers 4

6
+50

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
I think these SO posts will help more :) What is the difference between a Docker image and a container? and [What is the difference between docker-compose up and docker-compose start? ](stackoverflow.com/questions/33715499/…)
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 Comment

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.