majorcoder majorcoder - 3 years ago 162
Linux Question

Bizarre "No such file or directory" error when running a command within a docker container

I am trying to run a very simple python file that simply prints "woof" within a docker container. As far as I know I have created a docker container called:

c5d3c4c383d1


I then run the following command, in an attempt to tell myself what directory I am running things from in docker:

sudo docker run c5d3c4c383d1 pwd


This returns the following value:

/


Which I assume to be my root directory, so I go to my root directory. Typing pwd shows:

/


I then create a file called meow.py via the nano command and enter in this a single line that is:

print("Woof!")


I save this and confirm this is in the / directory with an ls command.

I then enter the following:

sudo docker run c5d3c4c383d1 python meow.py


Which returns:

python: can't open file 'meow.py': [Errno 2] No such file or directory


I don't understand this. Obviously I am not in the root directory when running a command with the docker as the meow.py file is DEFINETLY in the root directory but it is saying this file cannot be found. What the heck... As i said when I run pwd within the docker container it says i am in the / directory, but I cannot be given this file not found error.

Answer Source

docker is a container ... thats its root directory ... think of it like a totally different machine that you would normally ssh into... try something like this

docker run -it c5d3c4c383d1 bash

thats basically like you have just ssh'd into your remote machine

go ahead and try some commands (ls,pwd,etc)

now run echo print("hello world")>test.py

now run ls you should see your test.py ... go ahead and run it with python test.py

now you can exit you container ... if you launch the same container again you should still have your test.py file there... although i think its more common that people write a dockerfile that sets up their environment and then they just treat each session as disposable, as opposed to keeping the same container

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download