I'm trying to convert a Python script from executing command line statements to using docker-py.
I have a running container with MongoDB, and I want to create another container with Mongo shell.
This command-line works for me as expected:
docker run -i -t --entrypoint="mongo" mongo-image mongo-url:27017
Also succeeded converting it to separate docker create, docker start and docker attach commands, but I fail to get an interactive Mongo shell using docker-py. This is what I'm trying to do:
container = c.create_container(
image=MONGO_IMAGE,
entrypoint='mongo',
command='%s:27017' % ip,
detach=False,
stdin_open=True,
tty=True
)
c.start(container=container['Id'])
c.attach(container=container['Id'])
but it doesn't work (container is created, but not attached). How can I make it happen?
see #73 #239
tl;dr doesn't exist yet, pull request welcome =) I'll close as this is essentially a duplicate.
hey!
did this ever get implemented?
i could really do with this now :)
also the issues don't directly discuss the feature of being equivalent to passing '-it' to the container creation step, so it's a bit of a shame that this is closed as duplicate
aha, using stdin_open = True, tty = True working here
@elliotwoods Would you mind showing an example of how you managed to open an interactive container?
my run command looks like this:
client.containers.run(imageName
, stdin_open = True
, tty = True
, name = "hacnode"
, detach = True
, volumes = {
'/root' : {
'bind' : '/mnt',
'mode' : 'rw'
}
}
, privileged = True
, restart_policy = {
"MaximumRetryCount": 0,
"Name": "unless-stopped"
}
, network_mode = 'host'
)
you just need to check those first lines about stdin_open and tty
please note that the container does not become interactive within the python terminal, it just means that i can attach to the container later (this is what i want). if you want to attach to the container, then i would suggest using a system command to docker to perform this for you. trying to do this from within python sounds very messy, as the remote terminal should essentially have full control of the local terminal, which would mean a lot of actions would have to be passed through python, and the docker executable would do a much more consistent job of this.
Ah, I see; I'll stick with subprocess in that case. Thanks!
Is this really impossible or just harder? I am asking because this adds docker cli as an additional runtime requirement if you want to shell into the container, as this comes with greats costs and portability challenges.
For example you can easily make use of docker-py to control remote docker hosts, start stop build container without needing to install docker itself locally. The onlypart that does not work is the interactive shell, which can prove useful for some debugging steps in.
There are lots of handy libraries that allow execution of interactive processes from within python by emulating terminals.
Does anyone have some code where this works, even if only partially?
Most helpful comment
my run command looks like this:
you just need to check those first lines about stdin_open and tty