title: FileNotFoundError: [Errno 2] No such file or directory: docker
body: <p>I'm trying to create and write a file in a docker container. The python script works fine in my local machine but I don't know how to make it work in docker. I created the app.py and the dockerfile with the official docker documentation.</p>
  <p>Then I created a volume with <strong>docker volume create my-vol</strong>.
  The hello world method works fine.
  This are the files:</p>
  <p>Dockerfile</p>
  <pre><code># syntax=docker/dockerfile:1
  FROM python:3.8-slim-buster
  WORKDIR /app
  COPY requirements.txt requirements.txt
  RUN pip3 install -r requirements.txt
  COPY . .
  CMD [ &quot;python3&quot;, &quot;-m&quot; , &quot;flask&quot;, &quot;run&quot;, &quot;--host=0.0.0.0&quot;]
  </code></pre>
  <p>docker-compose.yml</p>
  <pre><code>version: &quot;3.9&quot;  # optional since v1.27.0
  services:
    web:
      build: .
      ports:
        - &quot;5000:5000&quot;
      volumes:
        - my-vol
  volumes:
    logvolume01: {}
  </code></pre>
  <p>App.py</p>
  <pre><code>app = Flask(__name__)
  @app.route('/api/hello')
  def hello_world():
      return 'Hello, Docker!'
      
  
  @app.route('/api/ask')
  def ask():
      try:
          my_file=open(&quot;/my-vol/newfile.txt&quot;,&quot;r&quot;)
          print(my_file.read())
          newString=input(&quot;Ingresa un nuevo string&quot;)
          new_file=open(&quot;/my-vol/newfile.txt&quot;,mode=&quot;w&quot;,encoding=&quot;utf-8&quot;)
          new_file.write(newString)
          new_file.close()
          my_file=open(&quot;/my-vol/newfile.txt&quot;,&quot;r&quot;)
          print(my_file.read())
      except IOError:
          new_file=open(&quot;/my-vol/newfile.txt&quot;,mode=&quot;w&quot;,encoding=&quot;utf-8&quot;)
          new_file.write(&quot;Archivo creado exitosamente \n&quot;)
          new_file.close()
          my_file=open(&quot;/my-vol/newfile.txt&quot;,&quot;r&quot;)
          print(my_file.read())
      finally:
          print(&quot;exit&quot;)
  </code></pre>
  <p>Traceback</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/app/app.py&quot;, line 12, in ask
      my_file=open(&quot;/my-vol/newfile.txt&quot;,&quot;r&quot;)
  FileNotFoundError: [Errno 2] No such file or directory: '/my-vol/newfile.txt'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 2070, in wsgi_app
      response = self.full_dispatch_request()
    File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1515, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1513, in full_dispatch_request
      rv = self.dispatch_request()
    File &quot;/usr/local/lib/python3.8/site-packages/flask/app.py&quot;, line 1499, in dispatch_request
      return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
    File &quot;/app/app.py&quot;, line 21, in ask
      new_file=open(&quot;/my-vol/newfile.txt&quot;,mode=&quot;w&quot;,encoding=&quot;utf-8&quot;)
  FileNotFoundError: [Errno 2] No such file or directory: '/my-vol/newfile.txt'
  172.17.0.1 - - [20/Aug/2021 10:59:32] &quot;GET /api/ask HTTP/1.1&quot; 500 -
  </code></pre>