Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow django model instance creation with Docker

I have django application with some model. I have manage.py command that creates n models and saves it to db. It runs with decent speed on my host machine.

But if I run it in docker it runs very slow, 1 instance created and saved in 40-50 seconds. I think I am missing something on how Docker works, can somebody point out why performance is low and what can i do with it?

docker-compose.yml:

version: '2'

services:
  db:
    restart: always
    image: "postgres:9.6"
    ports:
      - "5432:5432"
    volumes:
      - /usr/local/var/postgres:/var/lib/postgresql
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=my_db
      - POSTGRES_USER=postgres

  web:
    build: .
    command: bash -c "./wait-for-it.sh db:5432 --timeout=15; python manage.py migrate; python manage.py runserver 0.0.0.0:8000; python manage.py mock 5"
    ports:
      - "8000:8000"
    expose:
      - "8000"
    depends_on:
      - db

dockerfile for web service:

FROM python:3.6
ENV PYTHONBUFFERED 1
ADD . .
WORKDIR .
RUN pip install -r requirements.txt
RUN chmod +x wait-for-it.sh
like image 202
IgorNikolaev Avatar asked Feb 09 '17 12:02

IgorNikolaev


1 Answers

The problem here is most likely the volume /usr/local/var/postgres:/var/lib/postgresql as you are using it on Mac. As I understand the Docker for Mac solution, it uses file sharing to implement host volumes, which is a lot slower then native filesystem access.

A possible workaround is to use a docker volume instead of a host volume. Here is an example:

version: '2'

volumes:
  postgres_data:

services:
  db:
    restart: always
    image: "postgres:9.6"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=my_db
      - POSTGRES_USER=postgres

  web:
    build: .
    command: bash -c "./wait-for-it.sh db:5432 --timeout=15; python manage.py migrate; python manage.py runserver 0.0.0.0:8000; python manage.py mock 5"
    ports:
      - "8000:8000"
    expose:
      - "8000"
    depends_on:
      - db

Please note that this may complicate management of the postgres data, as you can't simply access the data from your Mac. You can only use the docker CLI or containers to access, modify and backup this data. Also, I'm not sure what happens if you uninstall Docker from your Mac, it may be that you lose this data.

like image 151
Alexander Block Avatar answered Sep 19 '22 23:09

Alexander Block