1

I am able to get inside a docker container using below command

os.system('docker exec -ti $(docker ps -q -a --filter "name=XXXXX") /bin/bash')

and then i need to change to the following directory in the container.

/u01/oracle/weblogic/*****

when i use os.chdir("/u01/oracle/weblogic/*****"), i am getting No such Directory error.

I could see that os.getcwd() is still printing the previous directory where the python script is running instead of docker directory.

Could someone let me know what i am missing ?

4
  • 1
    When you run both commands after each other, you will change the working directory on your host machine where the script is on and not inside the container Commented Dec 11, 2015 at 9:59
  • So, how to get inside the container folder structure which is something like this /u01/oracle/weblogic Commented Dec 11, 2015 at 10:08
  • Use use the subprocess module instead of os.system() and pipe the command you want to use in. But in general: You should not do automated tasks from outside the docker container, that breaks the concept of docker. Commented Dec 11, 2015 at 10:39
  • Thanks a lot @KlausD. i will think in docker way ! Commented Dec 11, 2015 at 10:47

1 Answer 1

3

Why don't you use the container name as exec parameter, instead of using a query to get the id?

You can use the -c parameter of bash to execute multiple commands. Like:

os.system('docker exec -ti XXXXX /bin/bash -c "cd /tmp;ls -alrt"')
Sign up to request clarification or add additional context in comments.

2 Comments

This is amazing ! The -c flag did the magic for me. Thanks Robert :) In my case, the docker container name and id are not consistent. Only consistent part is IMAGE. But i am unable to filter based on IMAGE. IMAGE will be something like this systemname.com:xxxx/xxx/xxxx:latest
Got to see this thread which solved my other problem too :) stackoverflow.com/questions/29406871/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.