HemChe
HemChe

Reputation: 2337

Unable to change the working directory inside a docker container using python script

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 ?

Upvotes: 1

Views: 2506

Answers (1)

Rob van Laarhoven
Rob van Laarhoven

Reputation: 8905

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"')

Upvotes: 3

Related Questions