Search code examples
dockerpython-docker

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?


Solution

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