Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't open file 'app.py': [Errno 2] No such file or directory

Tags:

python

docker

Trying to get started with python & Docker, managed to get Docker working with a PHP example but struggling to get it working with a python file.

I am trying to get a simple hello world docker container running to simply print "Hello world"

Dockerfile

FROM python:2.7
WORKDIR /app
COPY . /app
EXPOSE 80
CMD [ "python", "app.py" ]

app.py

def hello():
    return print("hello world")
hello()

I have run the following commands

docker build -t test .
docker run -p 80:80 test

Expected result: containerised app running on port 80

Actual result:python: can't open file './app.py': [Errno 2] No such file or directory

like image 585
Alex Hesketh Avatar asked Oct 25 '25 20:10

Alex Hesketh


2 Answers

According to your comment, the directory structure seems to be

Dockerfile
src/
   app.py

In this case, the CMD statement in your Dockerfile should be:

CMD [ "python", "src/app.py" ]

This is because of your app.py residing inside src folder with respect to the mentioned WORKDIR.

like image 63
7_R3X Avatar answered Oct 27 '25 08:10

7_R3X


I had the same error, although I used docker-compose up, but I believe the essence is the same, meaning that it couldn't find the file I wanted to run. I fixed the error by changing the file path to python /app/manage.py instead of python manage.py. The corrected Dockerfile code is as follows:

FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app
CMD pwd && ls -l && python /app/manage.py runserver 0.0.0.0:8000
like image 32
Miroslav Zeljković Avatar answered Oct 27 '25 09:10

Miroslav Zeljković



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!