1

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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

I see! Conceptually this makes a ton of sense. Sadly when I try and run docker -it run c5d3c4c383d1 bash, i am told unknown shorthand flag: 'i' in -it. when i remove the -it flag i do not appear to connect to the docker container. thoughts?
@majotcoder, it's docker run -it, not docker -it run
thanks! it seems the changes I make to the image when I connect to it like this do not save. Any idea how I could get them to save?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.