Docker can't find the file it just copied in build

I have been given a project that is in a Docker container. I have managed to build the Docker container image and tag it, but when I run it I have problems.

bash-5.1$ docker build -t game:0.0.1 -t game:latest .
Sending build context to Docker daemon  2.584MB
Step 1/12 : FROM nvidia/cuda:10.2-base-ubuntu18.04
 ---> 84b82c2f5736
Step 2/12 : MAINTAINER me
 ---> Using cache
 ---> b8a86a8860d5
Step 3/12 : EXPOSE 5006
 ---> Using cache
 ---> fabdfc06768c
Step 4/12 : EXPOSE 8888
 ---> Using cache
 ---> a6f8585ce52d
Step 5/12 : ENV DEBIAN_FRONTEND noninteractive
 ---> Using cache
 ---> c4dd4de87fdc
Step 6/12 : ENV WD=/home/game/
 ---> Using cache
 ---> 871163f5db29
Step 7/12 : WORKDIR ${WD}
 ---> Using cache
 ---> 36678a12e551
Step 8/12 : RUN apt-get -y update &&     apt-get -y upgrade &&     apt-get -y install git ssh pkg-config python3-pip python3-opencv
 ---> Using cache
 ---> 4b83b4944484
Step 9/12 : COPY requirements.txt  /requirements.txt
 ---> Using cache
 ---> 8e1db9206e80
Step 10/12 : RUN cd / &&      python3 -m pip install --upgrade pip &&     pip3 install -r requirements.txt
 ---> Using cache
 ---> e096029d458a
Step 11/12 : CMD ["start.py"]
 ---> Using cache
 ---> 795bb5a65bc8
Step 12/12 : ENTRYPOINT ["python3"]
 ---> Using cache
 ---> 59b472b693f2
Successfully built 59b472b693f2
Successfully tagged game:0.0.1
Successfully tagged game:latest
bash-5.1$ docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix game:latest
Unable to find image 'game:latest' locally
docker: Error response from daemon: pull access denied for game, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
bash-5.1$ sudo docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix game:latest

It doesn’t seem to find the game:latest image even though the output of the above command says it just created it.

I also try to do this after logging into my session.

Here is the list of available images:

bash-5.1$ docker images
REPOSITORY    TAG                     IMAGE ID       CREATED         SIZE
game          0.0.1                   f4dae2ac9231   6 minutes ago   1.77GB
game          latest                  f4dae2ac9231   6 minutes ago   1.77GB
ubuntu        latest                  ba6acccedd29   7 weeks ago     72.8MB
hello-world   latest                  feb5d9fea6a5   2 months ago    13.3kB
nvidia/cuda   10.2-base-ubuntu18.04   84b82c2f5736   2 months ago    107MB

I tried to run the image:

bash-5.1$ docker run f4dae2ac9231
python3: can't open file 'start.py': [Errno 2] No such file or directory
bash-5.1$ ^C
bash-5.1$ ls
data_collection      demo.py	 examples   requirements.txt  start.py
demonstrateur.ipynb  Dockerfile  README.md  serious_game      test

I tried to add it in the Dockerfile but still got the same error:

Removing intermediate container 10f2d7506d17
 ---> 1b776923e5a9
Step 11/13 : COPY start.py /start.py
 ---> 172c81ff16e9
Step 12/13 : CMD ["start.py"]
 ---> Running in c7217e2e0f21
Removing intermediate container c7217e2e0f21
 ---> eaf947ffa0b1
Step 13/13 : ENTRYPOINT ["python3"]
 ---> Running in 77e2e7b90658
Removing intermediate container 77e2e7b90658
 ---> 924d8c473e36
Successfully built 924d8c473e36
Successfully tagged seriousgame:0.0.1
Successfully tagged seriousgame:latest
bash-5.1$ docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix seriousgame:latest
python3: can't open file 'start.py': [Errno 2] No such file or directory

Here is my Dockerfile:

FROM nvidia/cuda:10.2-base-ubuntu18.04
MAINTAINER me

EXPOSE 5006
EXPOSE 8888

ENV DEBIAN_FRONTEND noninteractive
ENV WD=/home/game/
WORKDIR ${WD}

# Add git and ssh
RUN apt-get -y update && \
    apt-get -y upgrade && \
    apt-get -y install git ssh pkg-config python3-pip python3-opencv

# DĂ©pendances python
COPY requirements.txt  /requirements.txt
RUN cd / && \
     python3 -m pip install --upgrade pip && \
    pip3 install -r requirements.txt

COPY start.py /start.py
CMD ["start.py"]
ENTRYPOINT ["python3"]

I don’t understand the image not found issue but you should use full path in the CMD instruction. Your workdir is not the root directory where you copied start.py.

Try

CMD ["/start.py"]
1 Like

You can also try to go into your docker container and try to launch your app with:

docker exec -it appname
$ ls
$ python start.py

The working directory is still not the root, so your command would not work either without fixing the path by adding / to the beginning. WORKDIR also affects `docker exec

1 Like

I am very new to Focker but shouldn’t WORKDIR make my local working directory copied in the image so I can run start.py ?

Anyway, adding / seemd to make some thins work. The out put of Docker run is now:

bash-5.1$ docker run e4f7d47366cd
Run bokeh server? ([y]/n) 
Traceback (most recent call last):
  File "/start.py", line 24, in <module>
    main()
  File "/start.py", line 10, in main
    inpt = input("Run bokeh server? ([y]/n) \n")
EOFError: EOF when reading a line

You might want to make it a habit to read the Dockerfile reference in case something is unclear: https://docs.docker.com/engine/reference/builder/#workdir

WORKDIR sets the directory context to the specified folder. All following instructions will use it as current folder, unless you change the folder (like you did with RUN cd / && .... If you use relativ paths they must be relative to the WORKDIR path.

I forget to mention the relevant part: the WORKDIR instruction is not responsible to copy any files - neither from your host’s working directory, nor from any other folder. The COPY instruction is responsible to copy files from the host into the image. N.B:: the ADD instruction could be used as well, though it will extract archived files and as such might result in a different outcomme than expected. COPY is the prefered instruction to copy the files, as it realy “just” copies the files (and allows to modify the ownership of the files if needed)

1 Like