title: Cannot launch gunicorn flask app with torch model on the docker
body: <p>Does anyone has the working example of docker that uses GPU, torch, gunicorn and flask in the one application? Torch 1.4.0 throws an exception. Please find below the configuration</p>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM nvidia/cuda:10.2-base-ubuntu18.04
  
  # Install some basic utilities
  RUN apt-get update &amp;&amp; apt-get install -y \
      curl \
      ca-certificates \
      sudo \
      git \
      bzip2 \
      libx11-6 \
   &amp;&amp; rm -rf /var/lib/apt/lists/*
  
  # Create a working directory
  RUN mkdir /app
  WORKDIR /app
  
  RUN apt-get update
  
  RUN apt-get install -y curl python3.7 python3.7-dev python3.7-distutils
  
  # Register the version in alternatives
  RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
  
  # Set python 3 as the default python
  RUN update-alternatives --set python /usr/bin/python3.7
  
  # Upgrade pip to latest version
  RUN curl -s https://bootstrap.pypa.io/get-pip.py -o get-pip.py &amp;&amp; \
      python get-pip.py --force-reinstall &amp;&amp; \
      rm get-pip.py
  
  # Set the default command to python3
  
  COPY requirements.txt .
  RUN pip --no-cache-dir install -r requirements.txt
  RUN pip install torch torchvision
  
  WORKDIR /usr/src/app
  
  COPY . ./
  
  CMD python ./new_main.py --workers 1
  </code></pre>
  
  <p>And the new_main.py:</p>
  
  <pre><code>if __name__ == '__main__':
      parser = argparse.ArgumentParser()
      parser.add_argument("--test", action='store_true')
      parser.add_argument("--workers", type=int, default=1)
      args = parser.parse_args()
      if check_test_mode(args.test):
          number_of_GPU_workers = args.workers or 1
      options = {
          'bind': '%s:%s' % ('0.0.0.0', str(port)),
          'workers': number_of_GPU_workers,
          'timeout': 300
      }
      StandaloneApplication(app, options).run()
  
  
  init()
  </code></pre>
  
  <p>The route I am using:</p>
  
  <pre><code>@app.route("/api/work", methods=["POST"])
  def work():
      try:
          body = request.get_json()
          if app.worker is None:
              app.worker = worker()
              app.worker.load_models() 
          ...
  </code></pre>
  
  <p>And here it throws the exception:</p>
  
  <pre><code>2020-04-09 11:33:33,544 loading file /mnt/models/best-model
  Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method
  </code></pre>
  
  <p>Command that I am using:</p>
  
  <pre><code>sudo docker run    -p 8889:8888   -e MODELSLOCATION=/mnt/models   --gpus all -v $MODELSLOCATION:/mnt/models cc14ffc68256
  </code></pre>