4

I have the following file structure

project 
├── app
│   ├── main.py
│   └── combine.py
│   └── scrape.py
├── resources
|    └── secrets.py
|    └── config.py
|    └── requirements.txt
|--- Dockerfile

My Dockerfile looks like

FROM python:3.8
RUN mkdir /project
WORKDIR /project

COPY ./resources/requirements.txt ./
ADD ./resources/. /resources/
ADD ./app/. /app/

RUN pip install -r ./requirements.txt

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

I build my image using docker build -t my-app . and run using docker run my-app

I get the following error python: can't open file './app/main.py': [Errno 2] No such file or directory

Can I keep subdirectory file structure and successfully run a Docker image? All the tutorials/ questions I have seen previously have the Dockerfile in the same directory as all the code that it depends on - is this a requirement? Thanks in advance!

1 Answer 1

5

Your WORKDIR is set to /project.

Your CMD is run as './app', so that translates to '/project/app'.

You're copying app to '/app', so use this:

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can use relative paths for the destination argument of COPY and ADD, so COPY ./resources/ ./resources/ and keeping your current directory layout will address this too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.