'can't access/download a generated folder that is inside a Docker container using REST API calls
I've been trying to solve this issue for quite some time, and I'm pretty sure it's just a really stupid mistake but I'm really giving up on life.
Background: I have a Flask python app providing RESTful services. This app is running inside a docker container. some GET functions return Json responses and they're simple and work fine. some, however, are a bit more complex. There's a file inside the docker container called "generated" what get_files does here is it compresses it in a Zip file, then downloads the generated zip file.
from flask import Flask, render_template, request, jsonify, redirect, url_for, send_from_directory
import zipfile
app = Flask(__name__)
#app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#gets all elements
@app.route('/elementsdb/ecu',methods=['GET'])
def getAllElements():
return jsonify({'ecus':elementsDB})
#-------------------------------------------#
#Get files
@app.route('/get_files', methods=['GET'])
def download():
zf = zipfile.ZipFile('gen.zip', mode = 'w')
zf.write('generated/one.c')
zf.write('generated/two.c')
zf.write('generated/three.c')
zf.write('generated/four.h')
zf.close()
return send_from_directory("/Users/abeer/downloads/testD", "gen.zip", as_attachment=True)
Issue: /get_files works fine when running independently on localhost. http://localhost:5000/get_files but because the app is in a docker container, it downloads the file inside the docker image. When I try to access the file via postman or curl, I get a 404 error I've also tried to change the path to download the file outside the image but that doesn't work either. It's probably a publish/expose port thing in Docker but I'm so clueless in that matter.
This is my dockerfile:
FROM python:2.7
ADD . /todo
WORKDIR /todo
RUN pip install -r requirements.txt
my docker-compose.yml
web:
build: .
command: python -u flaskdb.py
ports:
- "5000:5000"
volumes:
- .:/todo
links:
- db
db:
image: mongo:3.0.2
Solution 1:[1]
This is a little too late to help you, but for others about to give up on life, it doesn't work on docker because the path /Users/abeer/downloads/testD does not exist on the container, hence the 404
You need to tell the container to use that path by mapping the path to it using volumes, then it will work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Purusartha |
