简体   繁体   中英

Dockerfile can't find app.py

I have the following folder structure in a flask app that I am embedding in a docker image.

├── Dockerfile
├── README.md
├── app
│   ├── __init__.py
│   ├── app.py
│   ├── db
│   ├── imgcomp
│   │   └── __init__.py
│   ├── static
│   └── templates
│       ├── comparison_result.html
│       ├── display_images.html
│       ├── index.html
│       └── upload.html
├── docker-compose.yml
├── requirements.txt
└── tests

These are the contents of the docker-compose.yml:

version: '2'
services:
    web:
    build: .
    ports:
       - "5000:5000"
    volumes:
       - .:/code

These are the contents of Dockerfile:

FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

When I run docker-compose up, I get the error:

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

Simply because its in the subfolder. Changing the argument to app/app.py doesn't help. Also, when I run docker-compose down, then edit the file, it doesn't seem to be registering any changes to the file when I run docker-compose up again. What am I missing?

You have the same problem in issue 1616

the issue appears to primarily be a consequence of the ADD/WORKDIR in the Dockerfile:

ADD . /code
WORKDIR /code

Conflicting with the volume mapping in the docker-compose.yml

volumes:
 - .:/code

I may have misread the instructions but had both.

The reason:

When running an app on a remote host, you'll need to remove any volumes entries that mount anything in from a local directory.

The reason we promote both adding your code to the image and mounting it as a volume is so that doing a docker-compose build gets you an image that's ready to deploy to a remote server, but whose code can still be mounted when developing locally so it can be live-reloaded.

If you want to use Compose to both develop an app locally and deploy it to the cloud, the best practice is to have a file for each - docker-compose.yml for local development and something else (eg remote.yml ) for remote deployment - and use the -f flag to pass the alternate file in when deploying.

You can use the extends option to reduce duplication between both files.


On Windows, be careful about your path and its shared state: see issue 3157 .

For windows:

One solution is removing the "volume" in your "docker-compose.yml" .

The problem is, it will not update/sync your files in your host. (That is my case) I tried playing around with volumes and found out that the "." in volume ( ".:/code" ) is not working. It makes my code directory in docker empty.

What I would suggest to do is transfer your project in C:/Users/YourUsername/ (Can be a deeper directory, as long it's under the path), then in volume parameter in docker-compose, change the ".:/code" to "C:/Users/YourUsername/project_folder:/code"

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