简体   繁体   中英

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

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

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 .

WORKDIR makes any subsequent commands run within that work dir you specified. So, the next line COPY. /app COPY. /app will copy files to this directory ./app/app not ./app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM