Search code examples
pythondockerdockerpy

Create Docker container with Python SDK and execute commands in it


I'm trying to create one Docker container using Docker Python SDK, and keep executing some commands in it and get some results.

I found in docs that I can run the container execute a command and then this container is gone

import docker
client = docker.from_env()
client.containers.run('alpine', 'echo hello world')

or create a container and make it run as daemon

container = client.containers.run('bfirsh/reticulate-splines',
                                  detach=True)

but still I don't know how to keep the container running and send it commands to execute, Is this possible? or Am I missing something? or Am I misunderstand something?


Solution

  • I'm not sure to understand what you want to achieve. But if that helps, here is how I create a docker container from an image:

    import docker as docker_sdk
    
    
    docker = docker_sdk.from_env()
    
    docker.containers.run(image='name_of_your_image',                                                                                                                                                      
                          command='/usr/sbin/your_command --arg 123 --abc',                                                                                                                              
                          name=CONTAINER_NAME,                                                                                                                                              
                          hostname='host',                                                                                                                                                   
                          volumes={                                                                                                                                                         
                              '/container_directory': {                                                                                                                                       
                                  'bind': '/host/home/user/directory',                                                                                                                             
                                  'mode': 'rw'                                                                                                                                              
                              }                                                                                                                                                             
                          },                                                                                                                                                                
                          detach=True)
    

    Docs and examples are available here: https://docker-py.readthedocs.io/en/stable/containers.html