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?

  • Lost data docker gitlab on local osx
  • Vagrant docker-exec
  • Error with running simple Elixir app in Docker container
  • Persist users across containers
  • Docker Registry - read-only mode
  • Docker UDP port support?
  • 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
    

  • Docker Container to Container communication with IPv6 only
  • Running Tomcat with PostgreSql using Dockerfile
  • How to use docker image, without mounting the default volumes?
  • Docker build & deploy on Jenkins takes increasingly long amount of time
  • docker-compose up -d error
  • Reuse containers with `docker-compose`
  • 2 Solutions collect form web for “Slow django model instance creation with Docker”

    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.

    Two things, can be a probable cause:

    1. Starting of docker container takes some time, so if you start new container for each instance this can add up.
    2. What storage driver do you use? Docker (often) defaults to device mapper loopback storage driver, which is slow. Here is some context. This will be painfull especially if you start this container often.

    Other than that your config looks sensibly, and there are no obvious causes problems there. So if the above two points don’t apply to you, please add some extra comments — like how you actually add these model instances.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.