1

I have Flask application which is running docker container, I have to run host OS commands from Flask Application,

How to execute Host OS commands from Docker container?

My Docker image File:

FROM python:3.8-slim
RUN mkdir /app
WORKDIR /app
ADD requirements.txt /app
RUN pip3 install -r requirements.txt
ADD . /app
RUN chmod +x ./entrypoint.sh
ENTRYPOINT ["sh", "entrypoint.sh"]

app.py

import subprocess
from flask import Flask
app = Flask(__name__)

app.config['DEBUG'] = True

# Run host command from container
def run_os_cmd():
    return subprocess.checkout(["/usr/bin/some-cmd", "few-options"])

@app.route("/")
def hello_world():
    cmd_output = run_os_cmd()
    return cmd_output

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Host is Linux

Note, I cannot install host command package inside docker container.

Vidya
  • 547
  • 1
  • 10
  • 26
  • Why do you isolate the application in a Docker container then? – Klaus D. Nov 24 '20 at 04:52
  • My host does not allow to install any packages. my host comes with inbuilt docker installed .Thats the only reason we are trying with docker images – Vidya Nov 24 '20 at 04:56
  • 1
    A specific design goal of Docker is that you can't run commands on the host from inside a container. The linked question has a couple of workarounds, including a detailed setup of using a named pipe as a way to pass commands back to the host, but it'd be better to run this Flask application directly on the host. (Do you already have Python, and can you create a virtual environment to install packages as an unprivileged user?) – David Maze Nov 24 '20 at 05:33
  • I am trying with host flask with gunicorn , then i will use nginx container – Vidya Nov 24 '20 at 07:20

0 Answers0