Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose python: can't open file './main.py': [Errno 2] No such file or directory

This is my Dockerfile:

FROM python:3.8-slim

WORKDIR /proxy-scraper-checker-master

RUN apt-get update && \
    apt-get install -y --no-install-recommends libc-dev

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . /proxy-scraper-checker-master
CMD [ "python", "main.py" ]

This is my docker-compose.yml:

version: "3.3"

services:
    proxy-app:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: proxy-app

This is structure of my project dir (from which I run my docker commands):

├── docker-compose.yml
├── Dockerfile
├── requirements.txt
├── proxy-scraper-checker-master
     └── main.py

When I run the following command in the above directory:

docker-compose up

I get the following error message:

C:\Projects\myprojects\docker-example>docker-compose up
Creating proxy-app ... done
Attaching to proxy-app
proxy-app    | python: can't open file './main.py': [Errno 2] No such file or directory
proxy-app exited with code 2

Could someone guide me on what I am doing wrong?

like image 473
Danny Avatar asked Oct 27 '25 08:10

Danny


1 Answers

The problem is in the lines.

COPY . /proxy-scraper-checker-master
CMD [ "python", "main.py" ]

You are copying the entire directory into /proxy-scraper-checker-master, so then your main.py file would be /proxy-scraper-checker-master/proxy-scraper-checker-master/main.py.

To debug this, you can enter a bash terminal within the container and look around the directory structure to find main.py. docker-compose will have built the image, so you can find the image name with docker images, or you can rebuild it.

jakub@dash:/tmp/so$ docker build --tag my_python .
jakub@dash:/tmp/so$ docker run --rm -it my_python bash
# At this point, we are inside the Docker container.
root@924a7f854119:/proxy-scraper-checker-master# pwd
/proxy-scraper-checker-master
root@924a7f854119:/proxy-scraper-checker-master# ls
Dockerfile  docker-compose.yml  proxy-scraper-checker-master  requirements.txt
root@924a7f854119:/proxy-scraper-checker-master# realpath proxy-scraper-checker-master/main.py 
/proxy-scraper-checker-master/proxy-scraper-checker-master/main.py

At this point, we have found the path to main.py. To fix the original issue, we can change the CMD in the Dockerfile to the following:

CMD [ "python", "/proxy-scraper-checker-master/proxy-scraper-checker-master/main.py" ]

We can improve the Dockerfile to remove redundant COPY instructions. Using the below Dockerfile, the absolute path to main.py is /app/proxy-scraper-checker-master/main.py. We can reference it with the relative path proxy-scraper-checker-master/main.py because we are currently in /app (thanks to the WORKDIR instruction). We could also reference it with the absolute path.

FROM python:3.8-slim

RUN apt-get update && \
    apt-get install -y --no-install-recommends libc-dev

WORKDIR /app

COPY . .

RUN pip install --no-cache-dir -r requirements.txt

CMD [ "python", "proxy-scraper-checker-master/main.py" ]

I recommend reading the COPY documentation to understand its behavior.

like image 117
jakub Avatar answered Oct 28 '25 22:10

jakub