Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain a running container #3214

Open
akanksha-atrey opened this issue Jan 4, 2024 · 3 comments
Open

Maintain a running container #3214

akanksha-atrey opened this issue Jan 4, 2024 · 3 comments

Comments

@akanksha-atrey
Copy link

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.

@LombardiDaniel
Copy link
Contributor

@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.

@akanksha-atrey
Copy link
Author

@LombardiDaniel thanks! That is what I have been doing but it is a hacky solution. I was hoping there was an alternative.

@belalhmedan90
Copy link

belalhmedan90 commented Feb 16, 2024

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
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants