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

Start container with timeout? #933

Closed
francisjeanneau opened this issue Feb 9, 2016 · 7 comments
Closed

Start container with timeout? #933

francisjeanneau opened this issue Feb 9, 2016 · 7 comments

Comments

@francisjeanneau
Copy link

Hi everyone,

Is there any way to start a container with a time limit?

@francisjeanneau francisjeanneau changed the title Container timeout Start container with timeout? Feb 9, 2016
@shin-
Copy link
Contributor

shin- commented Feb 9, 2016

Is this what you're looking for?

import docker
from requests.exceptions import ReadTimeout

c = docker.Client()
ctnr = c.create_container('busybox', 'sleep 45')
c.start(ctnr)
try:
  c.wait(ctnr, timeout=5)
except ReadTimeout as e:
  print('too slow!')
  c.kill(ctnr)

@francisjeanneau
Copy link
Author

Not exactly.

In fact, I am executing untrusted code inside a container and I want to
limit that code execution time.

Isn't my script stopping at c.start() and wait for container's result?

Or am I completely lost and my script hangs on c.stop() while the code is
executed inside the container? If yes, then your solution might just work!

Will check that tomorrow and get back to you,

Thank you very much!
Le 9 févr. 2016 15:04, "Joffrey F" notifications@github.com a écrit :

Is this what you're looking for?

import dockerfrom requests.exceptions import ReadTimeout

c = docker.Client()
ctnr = c.create_container('busybox', 'sleep 45')
c.start(ctnr)try:
c.wait(ctnr, timeout=5)except ReadTimeout as e:
print('too slow!')
c.kill(ctnr)


Reply to this email directly or view it on GitHub
#933 (comment).

@shin-
Copy link
Contributor

shin- commented Feb 10, 2016

Yeah, Client.start is non-blocking. Client.stop will typically block until the container is stopped, or the command has timed out (default 10 seconds).

@francisjeanneau
Copy link
Author

Mind = blown. Don't know how I could have missed that. Probably the 'docker run command' that had me suppose Client.start was doing the same thing.

@shin-
Copy link
Contributor

shin- commented Feb 10, 2016

Yeah, docker run is a combination of create_container + start + wait (unless you use the -d parameter).

Glad I could help - feel free to reach out if you're still having trouble.

@shin- shin- closed this as completed Feb 10, 2016
@sahil-b-shah
Copy link

This pattern is not working in the current version.

@kennylajara
Copy link

kennylajara commented Feb 2, 2022

The pattern works, the code needs to be adapted:

import docker
from requests.exceptions import ReadTimeout

. . .

# Create container
container = docker_client.containers.create(
    image_name,
    command,
)
# Run container
container.start()
try:
    # Wait for container to finish
    container.wait(timeout=30)
except ConnectionError as e:
    # If timeout, kill container
    container.kill()

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

No branches or pull requests

4 participants