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"
FROM python:2.7
WORKDIR /app
COPY . /app
EXPOSE 80
CMD [ "python", "app.py" ]
def hello():
return print("hello world")
hello()
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With