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

Support inserting data into running containers #73

Open
codedependant opened this issue Oct 30, 2013 · 8 comments
Open

Support inserting data into running containers #73

codedependant opened this issue Oct 30, 2013 · 8 comments

Comments

@codedependant
Copy link

Hi

I'm trying to replicate the following command using the docker-py api. I understand that the -a option is not supported. Do you have any suggestions for achieving the same result?

cat myapp.tar | docker run -i -a stdin progrium/buildstep /bin/bash -c "mkdir -p /app && tar -xC /app"

Thanks in advance!

@shin-
Copy link
Contributor

shin- commented Nov 1, 2013

Back when I started the project, writing to a running container was really complicated/weird and since then noone really requested or seemed to need it, so I don't think there's any way to do it with docker-py in its current status. Might add it on the pile of things I want to do, but if you feel up to the challenge, I'd gladly accept a pull request that implements it! ;)

@nikicat
Copy link

nikicat commented Jun 6, 2014

👍

@shin-
Copy link
Contributor

shin- commented Jun 21, 2014

See #239

@j-bennet
Copy link

j-bennet commented Jun 5, 2015

+1

@itsafire
Copy link

+1
Are there any obstacles to be resolved for this feature to be implemented ? exec_create being able to stream stdin into a running container and this be processed by a given command would greatly simplify configuration. Right now this use case is to be solved quite awkwardly.

@shin- shin- removed this from the Future milestone Jan 28, 2016
@pacoxu
Copy link
Contributor

pacoxu commented Jan 25, 2017

In docker 1.13.0, does the secret feature work as you expected ?

@pbeart
Copy link

pbeart commented Feb 11, 2021

This feature seems to have gone from working to not working again, the method outlined in this StackOverflow answer no longer works as the SocketIO object returned by attach_socket is not writeable. (The socket also only gives empty bytestrings as the output to read so maybe it's just broken)

@traverseda
Copy link

traverseda commented Feb 10, 2023

Really quite frustrating for anyone trying to make any sort of interactive docker stuff. I've resorted to building my container like this:

@qaurtApp.websocket("/image/run-interactive.sock")
@login_required
async def image_interactive_socket():         
    csrf_token=websocket.args.get('csrf_token')
    validate_csrf(csrf_token)      
                                                       
    image = docker_client.images.get(websocket.args.get("imageId"))
    #Not a unix socket, actually an io.RawIoBase implementation
    container = docker_client.containers.run(image, stdin_open = True, tty = True, auto_remove=True, detach=True)                                         
    _exitcode, socket = container.exec_run(websocket.args.get('command','/bin/sh'),socket=True,stdin=True, tty=True)
    socket = socket._sock
    socket.setblocking(0)
    async def sending():
        while True:                            
            try:                    
                data = socket.recv(4096)                      
                await websocket.send(data)
            except BlockingIOError: pass                                     
            await asyncio.sleep(0.05)
                                                        
    async def receiving():
        while True:
            data = await websocket.receive()
            socket.send(data.encode("utf-8"))

    producer = asyncio.create_task(sending())
    consumer = asyncio.create_task(receiving())
    try:
        await asyncio.gather(producer, consumer)
    except asyncio.CancelledError:    
        print("Shutting down interactive session")
        container.stop()

The main draw back is that there is no auto cleanup at all, which is really frustrating. It will work alright most of the time, but may have some unexpected results. I'd also much prefer to be able to keep the container running even after the user navigates away from it, and to be able to re-attach to an existing container. Overall this is pretty frustrating.

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

8 participants