Maintain a running container
akanksha-atrey opened this issue · 3 comments
I am trying to start/run a container and pass commands consecutively. However, as soon as I run the container, it exits and no other exec_run() commands go through. The specific error message is as follows:
docker.errors.APIError: 409 Client Error for http+docker://localnpipe/v1.43/containers/{id}/exec: Conflict ("Container {id} is not running")
The code is as follows:
import docker
client = docker.from_env()
image = client.images.pull(docker.io/{}/{})
container = client.containers.run(image = image.id, detach = True, command = 'echo starting container', tty = True, stdin_open = true)
container.exec_run("echo execute command")
I am running version 6.1.3 on Python 3.11.5.
@akanksha-atrey the container will run the command ("echo starting container") and exit as soon as it finishes, so it will not run any other commands, you can try something like command="sleep 9999"
to make it hang, allowing you to run other commands, but when the inital command finishes, the container will exit.
@LombardiDaniel thanks! That is what I have been doing but it is a hacky solution. I was hoping there was an alternative.
This solution might work, put the script that I'm using will stuck on that part also until I terminate it manually with CTRL+C
, is there a better alternative?? detach=true
doesn't work as expected:
container = client.containers.run(
my_img,
command=["tail", "-f", "/dev/null"],
device_requests=[
docker.types.DeviceRequest(device_ids=["0"], capabilities=[['gpu']])
],
tty=True,
detach=True
)