Muthu Krishnan
Muthu Krishnan

Reputation: 1

How to serve static images from host system to the docker container?

I need help making Flask use images from a directory (path is passed via program arguments using argparse). The problem is, i need to make this work with Docker to use a host directory inside the container.

Check my directory structure:

Static/0.png
main.py

Flask Code: main.py

import argparse
import flask

parser = argparse.ArgumentParser()
parser.add_argument("--path",default="static")
args = parser.parse_args()

app = Flask(__name__,static_folder=args.path)
app.route("/")

def init():
    path = os.path.join(args.path,"0.png")
    return "<img src="+path+"/>"

Dockerfile:

FROM python:3-onbuild
EXPOSE 5000
ENTRYPOINT ["python","main.py"]

Observe that when I use the Static/ directory inside the container, it works, but when I use a path outside the container, it does not work.

Upvotes: 0

Views: 631

Answers (1)

user7503126
user7503126

Reputation:

You could create a VOLUME to do that.

In your Dockerfile:

VOLUME /app/static_files
...
ENTRYPOINT ["python","main.py", "/app/static_files"]

And while running your container, you have to specify the path of the static files with the argument -p <path>:/app/static_files

Upvotes: 1

Related Questions