docker / docker-py

A Python library for the Docker Engine API
https://docker-py.readthedocs.io/
Apache License 2.0
6.83k stars 1.67k forks source link

Support inserting data into running containers #73

Open codedependant opened 11 years ago

codedependant commented 11 years ago

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- commented 11 years ago

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 commented 10 years ago

:+1:

shin- commented 10 years ago

See #239

j-bennet commented 9 years ago

+1

itsafire commented 9 years ago

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

pacoxu commented 7 years ago

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

pbeart commented 3 years ago

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 commented 1 year ago

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.