Keeping container alive with Docker Python SDK
I am using the Docker Sdk for Python to run my container.
I am trying to start a docker container, and then run a command using the api exec_run (I need the exit code).
The exec_run needs to be executed on a started container.
This is my code:
import docker
client = docker.from_env()
container = client.containers.run('e7d8452ce5f5', command="echo starting", detach=True)
container.exec_run("echo execute command")
This raises an exception:
docker.errors.APIError: 409 Client Error: Conflict ("Container b65acd40f589819f490564dcb4e25f3055d85712cb7b2834ede5f2c4d57f2da6 is not running")
I tried running with no command when invoking client.containers.run, same exception..
Seems the container exists when the command is finished, even though in their documentation it is stated that the command run with detach=True is same as the cli docker run -d (when using docker run -d the container stays alive)
Any ideas on how to keep the container alive in order to call exec_run on it?
python docker
|
show 1 more comment
I am using the Docker Sdk for Python to run my container.
I am trying to start a docker container, and then run a command using the api exec_run (I need the exit code).
The exec_run needs to be executed on a started container.
This is my code:
import docker
client = docker.from_env()
container = client.containers.run('e7d8452ce5f5', command="echo starting", detach=True)
container.exec_run("echo execute command")
This raises an exception:
docker.errors.APIError: 409 Client Error: Conflict ("Container b65acd40f589819f490564dcb4e25f3055d85712cb7b2834ede5f2c4d57f2da6 is not running")
I tried running with no command when invoking client.containers.run, same exception..
Seems the container exists when the command is finished, even though in their documentation it is stated that the command run with detach=True is same as the cli docker run -d (when using docker run -d the container stays alive)
Any ideas on how to keep the container alive in order to call exec_run on it?
python docker
Your container stops because the command you use, i.e.echo starting
doesn't keep running. Why don't you try with a command likebash
orsh
?
– tgogos
Nov 28 '18 at 11:11
1
...or just launching the container with the actual command you want to run?
– David Maze
Nov 28 '18 at 11:28
Tried running sh -- same thing happens
– dana racah
Nov 28 '18 at 11:33
I can't launch it with the actual command because I need the exit code and run doesn't return it.. Only exec_run
– dana racah
Nov 28 '18 at 11:34
1
If you try withsh
you also need to use the-t
and-i
options of thedocker run
command. Not using them will makesh
exit immediately too.
– tgogos
Nov 28 '18 at 11:43
|
show 1 more comment
I am using the Docker Sdk for Python to run my container.
I am trying to start a docker container, and then run a command using the api exec_run (I need the exit code).
The exec_run needs to be executed on a started container.
This is my code:
import docker
client = docker.from_env()
container = client.containers.run('e7d8452ce5f5', command="echo starting", detach=True)
container.exec_run("echo execute command")
This raises an exception:
docker.errors.APIError: 409 Client Error: Conflict ("Container b65acd40f589819f490564dcb4e25f3055d85712cb7b2834ede5f2c4d57f2da6 is not running")
I tried running with no command when invoking client.containers.run, same exception..
Seems the container exists when the command is finished, even though in their documentation it is stated that the command run with detach=True is same as the cli docker run -d (when using docker run -d the container stays alive)
Any ideas on how to keep the container alive in order to call exec_run on it?
python docker
I am using the Docker Sdk for Python to run my container.
I am trying to start a docker container, and then run a command using the api exec_run (I need the exit code).
The exec_run needs to be executed on a started container.
This is my code:
import docker
client = docker.from_env()
container = client.containers.run('e7d8452ce5f5', command="echo starting", detach=True)
container.exec_run("echo execute command")
This raises an exception:
docker.errors.APIError: 409 Client Error: Conflict ("Container b65acd40f589819f490564dcb4e25f3055d85712cb7b2834ede5f2c4d57f2da6 is not running")
I tried running with no command when invoking client.containers.run, same exception..
Seems the container exists when the command is finished, even though in their documentation it is stated that the command run with detach=True is same as the cli docker run -d (when using docker run -d the container stays alive)
Any ideas on how to keep the container alive in order to call exec_run on it?
python docker
python docker
asked Nov 28 '18 at 11:07
dana racahdana racah
1
1
Your container stops because the command you use, i.e.echo starting
doesn't keep running. Why don't you try with a command likebash
orsh
?
– tgogos
Nov 28 '18 at 11:11
1
...or just launching the container with the actual command you want to run?
– David Maze
Nov 28 '18 at 11:28
Tried running sh -- same thing happens
– dana racah
Nov 28 '18 at 11:33
I can't launch it with the actual command because I need the exit code and run doesn't return it.. Only exec_run
– dana racah
Nov 28 '18 at 11:34
1
If you try withsh
you also need to use the-t
and-i
options of thedocker run
command. Not using them will makesh
exit immediately too.
– tgogos
Nov 28 '18 at 11:43
|
show 1 more comment
Your container stops because the command you use, i.e.echo starting
doesn't keep running. Why don't you try with a command likebash
orsh
?
– tgogos
Nov 28 '18 at 11:11
1
...or just launching the container with the actual command you want to run?
– David Maze
Nov 28 '18 at 11:28
Tried running sh -- same thing happens
– dana racah
Nov 28 '18 at 11:33
I can't launch it with the actual command because I need the exit code and run doesn't return it.. Only exec_run
– dana racah
Nov 28 '18 at 11:34
1
If you try withsh
you also need to use the-t
and-i
options of thedocker run
command. Not using them will makesh
exit immediately too.
– tgogos
Nov 28 '18 at 11:43
Your container stops because the command you use, i.e.
echo starting
doesn't keep running. Why don't you try with a command like bash
or sh
?– tgogos
Nov 28 '18 at 11:11
Your container stops because the command you use, i.e.
echo starting
doesn't keep running. Why don't you try with a command like bash
or sh
?– tgogos
Nov 28 '18 at 11:11
1
1
...or just launching the container with the actual command you want to run?
– David Maze
Nov 28 '18 at 11:28
...or just launching the container with the actual command you want to run?
– David Maze
Nov 28 '18 at 11:28
Tried running sh -- same thing happens
– dana racah
Nov 28 '18 at 11:33
Tried running sh -- same thing happens
– dana racah
Nov 28 '18 at 11:33
I can't launch it with the actual command because I need the exit code and run doesn't return it.. Only exec_run
– dana racah
Nov 28 '18 at 11:34
I can't launch it with the actual command because I need the exit code and run doesn't return it.. Only exec_run
– dana racah
Nov 28 '18 at 11:34
1
1
If you try with
sh
you also need to use the -t
and -i
options of the docker run
command. Not using them will make sh
exit immediately too.– tgogos
Nov 28 '18 at 11:43
If you try with
sh
you also need to use the -t
and -i
options of the docker run
command. Not using them will make sh
exit immediately too.– tgogos
Nov 28 '18 at 11:43
|
show 1 more comment
1 Answer
1
active
oldest
votes
When you use containers.run() method to start a ontainer,you should use tty parameter and set tty=True and then it will keep the container alive.
import docker
client = docker.from_env()
container = client.containers.run('xxxx', command="/bin/bash", tty=True,detach=True)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53518025%2fkeeping-container-alive-with-docker-python-sdk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you use containers.run() method to start a ontainer,you should use tty parameter and set tty=True and then it will keep the container alive.
import docker
client = docker.from_env()
container = client.containers.run('xxxx', command="/bin/bash", tty=True,detach=True)
add a comment |
When you use containers.run() method to start a ontainer,you should use tty parameter and set tty=True and then it will keep the container alive.
import docker
client = docker.from_env()
container = client.containers.run('xxxx', command="/bin/bash", tty=True,detach=True)
add a comment |
When you use containers.run() method to start a ontainer,you should use tty parameter and set tty=True and then it will keep the container alive.
import docker
client = docker.from_env()
container = client.containers.run('xxxx', command="/bin/bash", tty=True,detach=True)
When you use containers.run() method to start a ontainer,you should use tty parameter and set tty=True and then it will keep the container alive.
import docker
client = docker.from_env()
container = client.containers.run('xxxx', command="/bin/bash", tty=True,detach=True)
answered Feb 11 at 2:50
lwzhuolwzhuo
14614
14614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53518025%2fkeeping-container-alive-with-docker-python-sdk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your container stops because the command you use, i.e.
echo starting
doesn't keep running. Why don't you try with a command likebash
orsh
?– tgogos
Nov 28 '18 at 11:11
1
...or just launching the container with the actual command you want to run?
– David Maze
Nov 28 '18 at 11:28
Tried running sh -- same thing happens
– dana racah
Nov 28 '18 at 11:33
I can't launch it with the actual command because I need the exit code and run doesn't return it.. Only exec_run
– dana racah
Nov 28 '18 at 11:34
1
If you try with
sh
you also need to use the-t
and-i
options of thedocker run
command. Not using them will makesh
exit immediately too.– tgogos
Nov 28 '18 at 11:43