I am learning Docker concept and trying to make a Docker image of my project. I have installed Docker Desktop for Windows and build the image successfully by using below command:
docker build -t ${IMAGE_NAME} . But when I run following command docker run ${IMAGE_NAME}:${TAG} I am getting following file not found error:
D:\Projects\AI360\deep_auto_backbar_api>docker run dsbyprateekg:prateek_gupta python3: can't open file '/Prepare_Dataset/server_engine/server.py': [Errno 2] No such file or directory
My project structure is looks like:
And my Dockerfile.txt has following instructions:
FROM python: 3.6-stretch MAINTAINER PrateekG # install build utilities RUN apt-get update && \ apt-get install -y gcc make apt-transport-https ca-certificates build-essential # check our python environment RUN python3 version RUN pip3 --version # Installing python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy all the files from the project's root to the working directory COPY Prepare_Dataset/ . # Running Python Application CMD ["python3", "/Prepare_Dataset/server_engine/server.py"] I suspect I am missing something related to file path. Please see my Dockerfile and my project structure and help me to find out what I am doing wrong here.
14 Answers
In my case I had to change the line separators from cr/lf (Windows) to lf (Unix/Linux/macOS). To do this in IntelliJ, you have to select your root folder in the Project window and the go to File -> File Properties -> Line Separators -> LF - Unix and macOS (\n)
Also see this answer
When you use COPY Prepare_Dataset/ . this will copy the content of the directory, not the directory itself so CMD path become invalid /Prepare_Dataset/server_engine/server.py.
You need to use
COPY Prepare_Dataset/ ./Prepare_Dataset/ so when you copy you can verify
Step 5/7 : COPY Prepare_Dataset/ ./Prepare_Dataset/ ---> Using cache ---> 2c5c15c23f65 Step 6/7 : RUN ls | grep "Prepare_Dataset" ---> Running in 54147bd4740c Prepare_Dataset Better to keep convention to avoid such error in future.
# SEt workdirectory WORKDIR /app # Now it will copy to /app/ COPY Prepare_Dataset/ ./Prepare_Dataset CMD ["Prepare_Dataset/server_engine/server.py"] You can verify you problem using below steps.
COPY Prepare_Dataset/ . #You will see the content but not the directory RUN ls / You will not able to see the directory but you can grep the any file in it.
Step 5/7 : COPY Prepare_Dataset/ . ---> Using cache ---> e4eec046c860 Step 6/7 : RUN ls | grep "Prepare_Dataset" ---> Running in 23e4b2aab3d1 The command '/bin/sh -c ls | grep "Prepare_Dataset"' returned a non-zero code: 1 0Make sure the value of TAG is proper. Check if container is getting launch or not using following command;
docker ps -a Use an ENTRYPOINT instead of CMD and then you can use command line options in the docker run like in your example.
ENTRYPOINT ["python3", "Prepare_Dataset/server_engine/server.py"] Reference: link
1If others stumble across this, I hit the same error. But my issue was different:
# Error `No such file or directory` ENTRYPOINT ["/bin/bash", "-c", "{$APP}"] # Success ENTRYPOINT ["/bin/bash", "-c", "$APP"] 