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?

  • Can't to connect to postgres container
  • Error connecting to RDS Postgreql DB from inside Docker container
  • Unable to list kafka topics in openwhisk setup
  • Storing in an ENV the outcome of a git command
  • How to specify different .dockerignore files for different builds in the same project?
  • How to automate Quay.io login in a shell script?
  • 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
    

  • Using Openblas with R in Reproducible R container
  • Can't reach Asp.Net Core inside Docker
  • Storing secret env variables as separate data container in Docker
  • How to disable all network connections inside Docker?
  • Error while running the ASP.NET Core WebApp on Docker in Windows 7 (Debugging)
  • sharing code between microservices
  • 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.