Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use exec_run in python docker sdk for replacing my docker cli command

I want to replace the below command with docker python sdk

docker exec 6d9c9b679541 /u01/app/oracle/product/12.0.0/dbhome_1/bin/sqlplus sachin/sachin@orcl @1.sql

here is code i am writing and the error i am getting using python3

>>> import docker
>>> client = docker.from_env()
>>> client.exec_run('6d9c9b679541',command='/u01/app/oracle/product/12.0.0/dbhome_1/bin/sqlplus sachin/sachin@orcl @1.sql')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/docker/client.py", line 205, in __getattr__
    raise AttributeError(' '.join(s))
AttributeError: 'DockerClient' object has no attribute 'exec_run'

How to resolve this issue?

like image 893
Sach Avatar asked Oct 17 '25 14:10

Sach


1 Answers

The from_env method returns a DockerClient object (docs here).

You need to get the container first, and then use the exec_run method. If you want to access a running container, you need the following:

container = client.containers.get('your_container_name_or_id')

Now you can run your command in the container:

container.exec_run('your command here')
like image 194
saulotoledo Avatar answered Oct 20 '25 10:10

saulotoledo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!