Django docker error table not exist

I had a existing Django Rest project with an existing MySQL database (named libraries) which I wanted to Dockerize.

My dockerfile:

FROM python:2.7

RUN mkdir /code
COPY . /code/
RUN pip install -r requirements.txt

My docker-compose:

version: '3'

    image: mysql
      MYSQL_DATABASE: libraries
      MYSQL_USER: root
      MYSQL_PASSWORD: root
    build: .
    command: python runserver
      - .:/code
      - "8000:8000"


  1. I ran: docker-compose build - build was successful
  2. I ran: docker-compose up - had to run this command twice and then I could access my API by hitting localhost:8000

However, whenever I hit any API endpoint I get an error Table "XYZ" does not exist. All the tables are already present.

Why this happens?

2 answers

  • answered 2018-01-11 21:50 wm3ndez

    First you need to run django migrations:

    $ docker exec -it [container] bash
    # python migrate

  • answered 2018-01-11 21:52 dvnguyen

    First of all, it's strange that you had to run docker-compose up twice. I recommend to run docker logs after the first run to see what goes wrong, then start another question if you need help.

    Regarding your main question, keep it mind that docker containers are stateless. That means unless you add persistent volume configurations, you'll get the same "fresh" one every time you start a new container.

    Based on your compose file, there are two containers: a "web" one and a "db" one. A fresh "db" one just contains an empty MySQL instance with db name, db user, and db password settings. There's no data in it. You have two options:

    1. Run migration from your "web" container to set up the db schema in your "db" container.

    2. If you have some data in your local/dev setting and want to use them, consider backing up these data from your local setting then restoring it into your "db" container. In case you don't know how, consult MySQL documents to see how to backup data, and consult the "Initializing a fresh instance" part of the MySQL docker hub to see how to start a new "db" container with some data.