import docker client = docker.from_env() container = client.containers.run('alpine', detach=True) exec_instance = container.exec_run('echo "Hello, World!"') # Start the execution instance response = exec_instance.exec_start() # Print the response print(response.decode())
import docker client = docker.from_env() container = client.containers.run('python:3', detach=True) # Copy a script file into the container container.put_archive('/app', tar) # Execute the script file in the container exec_instance = container.exec_run('python /app/script.py') # Start the execution instance response = exec_instance.exec_start() # Print the response print(response.decode())In this example, the code runs a Python 3 image in a container and copies a script file (`script.py`) into the container using `put_archive`. It then executes the script file in the container using `exec_run` and prints the response using `exec_start`. Package library: `docker` (https://pypi.org/project/docker/)