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 with networks and “internal” property example
  • Forward Ports from boot2docker using the Vagrant Docker provider
  • Mounting a gcePersistentDisk kubernetes volume is very slow
  • How to detect fully interactive shell in bash from docker?
  • Docker run command error
  • Docker compose environment and paths
  • 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: In Dockerfile, copy files temporarily, but not for final image
  • Understanding docker -v command
  • How can I detect if docker for mac is installed?
  • How to configure a Docker container for acquiring DHCP IP/s from dhcp server running on ESX
  • Kubernetes Guestbook Example Not Loading Page
  • Is there a way to rename network interfaces in Docker swarm?
  • 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.