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.
What is “running as root”? Running a container as root means that the software packaged in a container is set to start as the root, or system administrator, user. This user is special in Linux systems, because it has all permissions needed to administer a system.
Rootless Docker in Docker To run Rootless Docker inside “rootful” Docker, use the docker:<version>-dind-rootless image instead of docker:<version>-dind . The docker:<version>-dind-rootless image runs as a non-root user (UID 1000). However, --privileged is required for disabling seccomp, AppArmor, and mount masks.
The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With