0

I use docker-compose to manage a django app and it's database. My problem is when I add a specific type of object in the database I have a DoesNotExist error : error screenshot.

What I don't understand is that the data IS in the database and i can request it from the django app docker without any problem.

I don't have the issue when I run the app in dev mode with python manage.py runserver with a local database.

Here is my docker-compose.yml :

version: '3'

services:
  dojodb:
    image: mysql:5
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: root
      MYSQL_DATABASE: dojodb
   volumes:
      - dojodbvolume:/var/lib/mysql

  dojo:
    build: .
    environment: 
      - SQLHOST=dojodb
      - SQLPORT=3306
      - SQLUSER=root
      - SQLPWD=password
      - DBNAME=dojodb
    ports:
      - "8000:8000"
    depends_on:
      - dojodb
volumes:
  dojodbvolume:

I really don't see where the problem comes from.

EDIT : The problem does not come from the connection to the database since I can create and retrieve other types of objects. The DoesNotExist error only occurs when I ask for products.

Antwane
  • 20,760
  • 7
  • 51
  • 84
Kaelors
  • 69
  • 7
  • Don't know if this is related, but you have a missing space on line 10 of your compose file, before `volumes:` – Antwane Dec 04 '18 at 15:34

2 Answers2

0

Because in your example (screenshot) you request database as 127.0.0.1. It works on localhost, but then you build docker-compose, you have your services split into several hosts.

Thus, to access database from dojo, you have to request host dojodb instead of localhost

grapes
  • 8,185
  • 1
  • 19
  • 31
  • My app already request dojodb instead of localhost. The localhost url is my app url not the db url. And the problem does not occur for all types of data but only when I want to see my products. Which means that the connection to the database works. – Kaelors Dec 04 '18 at 13:57
0

You have depends_on section but you missed links so the dojodb isn't known host for dojo (check in /etc/hosts in dojo container).

Basically add to dojo service

links:
  - dojodb

Also remember to configure database host name to dojodb.

Konrad
  • 952
  • 10
  • 25
  • I tried it, but it doesn't solve the problem. It does not surprise me since my app connect properly to the database to create or retrieve other type of objects. – Kaelors Dec 04 '18 at 15:15
  • You may want to read https://stackoverflow.com/questions/16181188/django-doesnotexist. Probably you want to access product with id 1 which isn't created in the database. – Konrad Dec 04 '18 at 16:16
  • Thanks for that, it helped me figuring out the problem. In fact something was missing in the database but I didn't knew it was requested by the webapp. – Kaelors Dec 05 '18 at 08:37