Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"OSError: [Errno 8] Exec format error" when trying to run simple flask app in a docker container

Tags:

docker

flask

I'm trying to start a simple Flask "Hello world" app in a docker container but I keep getting this error: "OSError: [Errno 8] Exec format error: '/app/app.py'"

My host operating system is Windows 10.

My Dockerfile:

FROM python:3.6

ENV PYTHONBUFFERED 1

ADD . /app

WORKDIR /app

RUN pip install -r requirements.txt

I have requirements.txt with Flask==1.0.2.

app.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug=True)

and docker-compose.yml:

version: '3'

services:
  app:
    build: .
    command: python app.py
    ports:
      - "8000:8000"

Whole log of container:

app_1  |  * Serving Flask app "app" (lazy loading)
app_1  |  * Environment: production
app_1  |    WARNING: Do not use the development server in a production environment.
app_1  |    Use a production WSGI server instead.
app_1  |  * Debug mode: on
app_1  |  * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
app_1  |  * Restarting with stat
app_1  | Traceback (most recent call last):
app_1  |   File "app.py", line 9, in <module>
app_1  |     app.run(host='0.0.0.0', port=8000, debug=True)
app_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 943, in run
app_1  |     run_simple(host, port, self, **options)
app_1  |   File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 988, in run_simple
app_1  |     run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
app_1  |   File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
app_1  |     sys.exit(reloader.restart_with_reloader())
app_1  |   File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
app_1  |     exit_code = subprocess.call(args, env=new_environ, close_fds=False)
app_1  |   File "/usr/local/lib/python3.6/subprocess.py", line 287, in call
app_1  |     with Popen(*popenargs, **kwargs) as p:
app_1  |   File "/usr/local/lib/python3.6/subprocess.py", line 729, in __init__
app_1  |     restore_signals, start_new_session)
app_1  |   File "/usr/local/lib/python3.6/subprocess.py", line 1364, in _execute_child
app_1  |     raise child_exception_type(errno_num, err_msg, err_filename)
app_1  | OSError: [Errno 8] Exec format error: '/app/app.py'
flaskdockerproject_app_1 exited with code 1

UPDATE

After I added the shebang in app.py like @larsks said I'm getting this error: "FileNotFoundError: [Errno 2] No such file or directory: '/app/app.py': '/app/app.py'.

All the files are in the container and in the right place.

like image 758
iwannabepythonmaster Avatar asked Mar 23 '19 15:03

iwannabepythonmaster


2 Answers

I hit the same problem (Exec format error, then FileNotFound if I added the shebang).

Adding "RUN chmod 644 app.py" to the Dockerfile fixed it for me, as mentioned here: https://github.com/pallets/werkzeug/issues/1482

like image 109
Richard Chamberlain Avatar answered Nov 02 '22 14:11

Richard Chamberlain


Spent the entire day yesterday putting the pieces together on this problem, forum by forum, so I want to share a more detailed answer to this question. While building my image, I was also seeing the following warning message:

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host.

"That warning was added, because the Windows filesystem does not have an option to mark a file as 'executable'. Building a linux image from a Windows machine would therefore break the image if a file has to be marked executable." - https://github.com/moby/moby/issues/20397

So, as Richard Chamberlain points out in the comment above, adding "RUN chmod 644.py" ensures that the app.py file is properly marked.

Putting all the pieces together, here is the complete Dockerfile that worked for me - Really hope it helps the next person struggling with this issue!

FROM python:3.7-alpine
COPY . /app
WORKDIR /app
RUN apk add --no-cache --virtual .build-deps \
    ca-certificates gcc postgresql-dev linux-headers musl-dev \
    libffi-dev jpeg-dev zlib-dev \
    && pip install --no-cache -r requirements.txt
RUN chmod 644 app.py
CMD ["python","app.py"]
like image 37
Mr. Vanderstelt Avatar answered Nov 02 '22 16:11

Mr. Vanderstelt